Advertisement

[java] Game loop

Started by April 05, 2000 06:27 AM
7 comments, last by Stefpet 24 years, 8 months ago
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)
    {
        case MAIN_MENU:
            // perform menu logic
            // 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.
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
Advertisement
(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
MENU
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 MENU
Call DrawMenu
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
============================Daniel Netz, Sentinel Design"I'm not stupid, I'm from Sweden" - Unknown
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)
{
case MAIN_MENU:
game_state = MainMenuLoop();
break;
// etc...
}
}

int MainMenuLoop(void)
{
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
-Steve
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:
player.addPoints(x);break;
....
....

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.
Thanks for your comments.

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.
Advertisement
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?
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.
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

This topic is closed to new replies.

Advertisement