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())
Fooling around in pygame (QUIT event)
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
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?
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:
You can find an event list here (scroll down), or use the Python dir() function to look for likely candidates.
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.
Try sys.exit() instead of sys.exit(0)
Has for the event list try reading the documentation in the pygame site, under event...
Has for the event list try reading the documentation in the pygame site, under event...
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.
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
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?
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?
Quote:
About the pygame.quit() I never use it and the documentation even saysQuote: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.
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)?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement