Sign in to follow this  

Fooling around in pygame (QUIT event)

This topic is 4103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I justs tarted to mess with pygame and created my first program that displays an image of a monkey on the screen. My only problem is that when I exit out of the program, it freezes. Here's the program
import pygame, sys,os
from pygame.locals import *
pygame.init()
window = pygame.display.set_mode((468, 600))
pygame.display.set_caption('Monkey Fever')
screen = pygame.display.get_surface()
monkey_head_file_name = os.path.join("data","chimp.bmp")
monkey_surface = pygame.image.load(monkey_head_file_name)
screen.blit(monkey_surface, (35,0))
pygame.display.flip()

def input(events): 
   for event in events: 
      if event.type == QUIT: 
         sys.exit(0) 
      else: 
         print event

while True: 
   input(pygame.event.get()) 

Here's the error: Traceback (most recent call last): File "E:/Documents and Settings/Kev/Desktop/pygame-1.7.1release/pygame-1.7.1release/examples/monkeyhead.py", line 20, in -toplevel- input(pygame.event.get()) File "E:/Documents and Settings/Kev/Desktop/pygame-1.7.1release/pygame-1.7.1release/examples/monkeyhead.py", line 15, in input sys.exit(0) SystemExit: 0 Also, where can I find a list of acceptable events so I can do a sys.exit(0) when a certain event happens?

Share this post


Link to post
Share on other sites
You need to shut down pygame properly on exit, not doing so has caused my code to freeze in the past. Something like this is recommended:
try:
pygame.init()
main()
finally:
pygame.quit()



You can find an event list here (scroll down), or use the Python dir() function to look for likely candidates.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kainnvin
Try sys.exit() instead of sys.exit(0)


sys.exit() has 0 as a default parameter. The program return value shouldn't affect the program cleanup in any way anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny

sys.exit() has 0 as a default parameter. The program return value shouldn't affect the program cleanup in any way anyway.


Good point.

About the pygame.quit() I never use it and the documentation even says
Quote:

When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue.


So I'am at a loss

Share this post


Link to post
Share on other sites
Just using pygame.quit() gives me this error:

Traceback (most recent call last):
File "E:/Documents and Settings/Kev/Desktop/pygame-1.7.1release/pygame-1.7.1release/examples/monkeyhead.py", line 20, in -toplevel-
input(pygame.event.get())
error: video system not initialized

EDIT: Ok I found that the keydown event is:

KEYDOWN unicode, key, mod

I tested the 's' key and found:

<Event(2-KeyDown {'key': 115, 'unicode': u's', 'mod': 0})>

Whats the proper way to write this type of event in actual code?

Share this post


Link to post
Share on other sites
Quote:

About the pygame.quit() I never use it and the documentation even says
Quote:
When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue.

So I'am at a loss


Not calling pygame.quit() causes problems when you're debugging your code in an IDE.


Quote:
Original post by kevtimc
Just using pygame.quit() gives me this error:


pygame.quit() doesn't terminate the program. It shuts down Pygame.

Quote:
Whats the proper way to write this type of event in actual code?


Test its type and key members.

Share this post


Link to post
Share on other sites
Well I got the KEYDOWN event to work, and I guess everyone is stuck on how to quit the app. [lol] I have a question, an if statement will only let you move the chimp right once, how does one create the program so that you can constantly let the chimp move in the desired direction (more than once)?

Share this post


Link to post
Share on other sites
Quote:
Original post by kevtimc
Well I got the KEYDOWN event to work, and I guess everyone is stuck on how to quit the app. [lol] I have a question, an if statement will only let you move the chimp right once, how does one create the program so that you can constantly let the chimp move in the desired direction (more than once)?


With a while loop. Quit the application by setting the loop control variable to False.

Share this post


Link to post
Share on other sites

This topic is 4103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this