• Create Account

## [java] Game loop

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Stefpet  Members

122
Like
Likes
Like

Posted 05 April 2000 - 12:27 AM

I just put together my first java-applet, a little pong game. I think it turned out quite nicely. Comments welcome. Check it out at: http://www.stefan-pettersson.nu/java/pong.html Anyway, something that I felt was not the most slick solution in the game was the inner game loop. It all worked out nicely for such a small game as pong, but with larger projects it feels like it will get out of hand. What I''m talking about is run() method in the thread that draws the graphics. It loops forever with while(true) {}. My solution today is something like this (pseudocode):
while (true)
{
switch(game_state)
{
// draw all main menu stuff in the buffer
break;

case IN_GAME:
// perform all game logic (bouncing balls etc)
// draw all game stuff in the buffer

case GAME_OVER:
// wait for mouse click
// draw game over graphics in the buffer
}

// draw the buffer to the screen
}

This gets all very messy when adding more stuff such as different states in the game itself and so on. May anyone suggest a better solution how to build this? Thanks in advance.

### #2Jono  Members

122
Like
Likes
Like

Posted 05 April 2000 - 12:56 AM

I''ve just played your pong game. And is it just me or does the ball seem to move a bit to slowly.
And the screen a bit to small.

But besides that the games brilliant.

If it''s ok I would like to put the game on my website, with you permission. And I will also give you credit for the game. By putting a link to your website. And mensherning your name.

If the answers yes then could you please e-mail me at : jono_subaru@hotmail.com Thnks From Jono

### #3Spiff  Members

122
Like
Likes
Like

Posted 05 April 2000 - 12:56 AM

(I'm mostly using VB, but the idea should be the same, so consider this pseudo too)

I'm quite fond of writing ENUMs (hopefully that exists in Java too) for every possible gamestate, like:

=====================
Enum GAMESTATE
GAME
OPTIONS
MULTIPLAYER
CONFIRMQUIT
End Enum
=====================

Then I create a main loop something like this:

=====================
Sub Main

Do while bRunning = True

Call GetInput

Select Case GAMESTATE
Case InGAME
Call DrawInGame
End Select

RenderWhateverIsLeft
Loop

End Sub
=====================

I.e. I create a function for every gamestate that executes all code needed for that state, maybe draw interface, buttons and so on.
When getting the input, I also check what GAMESTATE I'm in, so I know what buttons are visible.

============================
Daniel Netz, Sentinel Design
"I'm not stupid, I'm from Sweden" - Unknown

Edited by - Spiff on 4/5/00 6:58:38 AM

### #4Speuger  Members

122
Like
Likes
Like

Posted 05 April 2000 - 01:01 AM

That's not a bad solution really. I'd put function calls after your case statements instead of the actual code (you may have done this)...
while(true)
{
switch(game_state)
{
break;
// etc...
}
}

{
int return_state;
while(true)
{
// stuff
if (player_chooses_quit)
{
return_state = GAME_OVER;
break;
}
}
return return_state;
}

-Steve
(looks like my indents got killed)

Edited by - Speuger on 4/5/00 7:03:51 AM

### #5Jim_Ross  Members

122
Like
Likes
Like

Posted 05 April 2000 - 03:03 AM

Yes, calling methods in place of the code is a good way to maintain your switch statement''s managability.

case GAME_START:
game.init(); break;
case CHANGE_SCORE:
....
....

You don''t need an Enumeration for that. As for the game itself, you might want to make the ball faster of the computer paddle slower & start the ball off with some vertical movement to make the first couple of hits not extremely boring.

### #6Stefpet  Members

122
Like
Likes
Like

Posted 05 April 2000 - 03:39 AM

Regarding the game. The starting vertical movement is randomized, so it may be 0 or something else.

And yes, it''s small. Maybe a little bit too small. :-) Somehow I tend to like small things (like the pong logo).

A problem with pong games in general (I think) is that the only way to beat the cpu is to get a very big angle so the veritcal speed is faster than the max speed of the enemy pad. I''ve been thinking of increasing the horizontal speed after n bounces so it doesn''t take an eternity to win the ball.

About the code it seems that my idea was about the same as yours. So it seems that the way to go is actually to use that infinite loop in run(). Currently I use a switch statement to branch into different functions depending on the game state.

### #7Captain Goatse  Banned

100
Like
Likes
Like

Posted 05 April 2000 - 05:29 AM

Aww, my eyes are hurting... Is there any change to win that computer? I played until my eyes said no and couldn''t get that ball behinds enemy''s bat? After 5 minutes I had just bounched that ball between my bat and red''s bat?

### #8Jim_Ross  Members

122
Like
Likes
Like

Posted 05 April 2000 - 08:17 AM

Most people put the game loop in a seperate thread, since the new way of controlling threads is very similar to what you have with the while (running == true) {...} I like seperating the engine from the rest of the game this way because it feels easier to do other things at the same time.

About randomizing the vertical speed, randomize it between two constants both of which are greater than 0. I agree that random starting velocities are advantageous, but a starting vertical velocity of 0 produces a game in which neither opponent must move their paddles. Similarly, very small initial vertical velocities requires no paddle movement for the first few bounces.

### #9 Anonymous Poster_Anonymous Poster_*   Guests

Likes

Posted 06 April 2000 - 10:36 AM

hey all actually I just finished all i am doing on a pong game based on a jap anime cartoon I like. I at first thought about the AI and it was impossible to beat the computer but i came up with a few ways to fix it. First off my pong has six levels and you have to score 7 points to pass each level. well I basically use a vision algorithim to change difficulty. So say for the first level the computer paddle can only see the ball when it is within 20 pixels of it, so while it cant see the ball it randomly moves the computer paddle up or down. then on each higher level the computer view field grows so by the last level the computer can see over half the screen which makes it really hard but feasible to beat. This game has some bugs but unless I get interest in it Im not going to fix it. The applet also loads all the graphics and sound files from a simple txt file so they can be changed. if you want to check it out go to http://www.lemonyfresh.com/rayearthpong
one note is the intro song is 700K so it might take a little while for the applet to start. let me know what you think or any questions.

Tim Holwig
fabel@mindspring.com

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.