Replies To: A problem with a pygame project

Re: A problem with a pygame project

Posted 07 November 2013 - 07:16 AM

Few things. Recursion (having your game function call itself) is not how you want to accomplish looping your program. Next; you really shouldn't be trying to use the input builtin (or raw_input in python 2.x) with pygame.

If you could post your whole program (taking care that the indentation is preserved when you post it) I will try to take a look.

Re: A problem with a pygame project

Mekire is right, that's not a great way to loop. You have a running=True var so make a game loop that uses

while running==True:

then inside the running loop you can handle the keypresses with something like

for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_UP:

(I think putting this pygame.event.get()in the game loop solves the main problem you have with keypresses not working with your current game loop.)

when the worm crashes, set running to False, ask the question using raw_input() (I've not heard a reason not to use this with pygame before), then if play again is the answer set running back to True and call any game variable resets you need and tell the loop run again.

Re: A problem with a pygame project

ask the question using raw_input() (I've not heard a reason not to use this with pygame before)

Well now you have. You shouldn't do it because it is the wrong thing to do.

The best case scenario leaves the user forced to click over to the console window; give their input; and then click back over to the pygame window. The worst case scenario locks up your game because it has not been able to make calls to the event queue often enough.

The most legitimate reason, however, is the aformentioned it being the wrong thing to do. If the game should restart on a given input, render this message to the screen and add the necessary code to your pygame event loop to process it.

Re: A problem with a pygame project

Posted 11 November 2013 - 11:07 PM

Alright you make a good point. Even when running pygame I like to print outputs to the console in the background so I wouldn't mind clicking over to type something, but it is pretty sloppy and may freeze something. So check out text rendering, it's pretty simple.

Re: A problem with a pygame project

Posted 12 November 2013 - 12:41 AM

witeboy724, on 12 November 2013 - 06:07 AM, said:

Alright you make a good point. Even when running pygame I like to print outputs to the console

Don't get me wrong. I have nothing against print statements being used in pygame for testing purposes. This works great and does no harm. From a user perspective however you shouldn't be expecting them to do something they wouldn't normally do. You shouldn't use print statements to substitute as your HUD (for more than just testing purposes); and you shouldn't expect a player to tab over to the console window to enter an input prompt.