Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
J
JTankTrouble
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
9831111
JTankTrouble
Commits
7e825443
Commit
7e825443
authored
Jul 24, 2020
by
nargessalehi98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add GameLoop class
parent
daec45a6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
+61
-0
GameLoop.java
src/GameLoop.java
+61
-0
No files found.
src/GameLoop.java
0 → 100644
View file @
7e825443
/*** In The Name of Allah ***/
/**
* A very simple structure for the main game loop.
* THIS IS NOT PERFECT, but works for most situations.
* Note that to make this work, none of the 2 methods
* in the while loop (update() and render()) should be
* long running! Both must execute very quickly, without
* any waiting and blocking!
*
* Detailed discussion on different game loop design
* patterns is available in the following link:
* http://gameprogrammingpatterns.com/game-loop.html
*
* @author Seyed Mohammad Ghaffarian
*/
public
class
GameLoop
implements
Runnable
{
/**
* Frame Per Second.
* Higher is better, but any value above 24 is fine.
*/
public
static
final
int
FPS
=
30
;
private
GameFrame
canvas
;
private
GameState
state
;
public
GameLoop
(
GameFrame
frame
)
{
canvas
=
frame
;
}
/**
* This must be called before the game loop starts.
*/
public
void
init
()
{
state
=
new
GameState
();
canvas
.
addKeyListener
(
state
.
getKeyListener
());
canvas
.
addMouseListener
(
state
.
getMouseListener
());
canvas
.
addMouseMotionListener
(
state
.
getMouseMotionListener
());
}
@Override
public
void
run
()
{
boolean
gameOver
=
false
;
while
(!
gameOver
)
{
try
{
long
start
=
System
.
currentTimeMillis
();
//
state
.
update
();
canvas
.
render
(
state
);
gameOver
=
state
.
gameOver
;
//
long
delay
=
(
1000
/
FPS
)
-
(
System
.
currentTimeMillis
()
-
start
);
if
(
delay
>
0
)
Thread
.
sleep
(
delay
);
}
catch
(
InterruptedException
ex
)
{
}
}
canvas
.
render
(
state
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment