Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pong Tutorial

Started by Gallivan Jul 20, 2007 at 11:53 PM 1 replies 900+ views
Original Post
Gallivan
Gallivan
I've been following a certain pyGame Pong tutorial (actually I copy+pasted the source code and have been tweaking some things - although I just started this morning). What I'd like to do now, is if the ball should hit a left-right wall, the game will restart. Later, I will add a counter (although I think I know how to do this). My question, derived in some mutant pseudo-code, is would I perform this action like this:

while HITSIDE = FALSE
     continue
else
     RESTARTGAME
Or would I not put it in my main game loop? (Here's the source code, minus the ball and bat images: http://rafb.net/p/EadR0167.html) EDIT: To elaborate a bit, would it look something like:

                if dx == 0 or 640:
                        raise SystemExit
With SystemExit to be replaced later on with the game restart?
Geometrian
Geometrian
I made a pygame pong game once, (http://www.geometrian.com/Ping%20Pang%20Pong.html). It includes the source code, though I think it's a little buggy.

The way I did it was to keep track of the ball's location and restart the game if it was less than 0 or greater than the x screen dimension, similar to your second way.
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]
umbrae
umbrae
I presume your question is whether to bluntly exit in the piece of code where the game discovers the ball hitting the edge, or set a flag and have it checked in your loop's condition later?

My prefered method is to reverse out of any method calls and exit gracefully. An interesting solution would be to have a message queue - a quit game message is put into the queue and different parts of your code check this message system. In this case the part of the code to work out if to stop the game would check the messages for any 'quit game' messages and would quit the game at this point.

I hope this is an answer to your question, if not further details would be appreciated.

TL;DR: I would go with option 1.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.