import pygame
from pygame.locals import *
from pygame.color import THECOLORS
def main():
WINSIZE = 640, 480
pygame.init()
screen = pygame.display.set_mode(WINSIZE, 0, 8)
pygame.display.set_caption('Bounce')
screen.fill(THECOLORS["black"])
if __name__ == "__main__":
main()
Python game freezes
Hi, I'm just starting out with python, and I'm having problems with this small app. It freezes right after I press the "x" close button. It does not respond the the keys I designated as exit keys (escape) and does not go to full screen when it should. Can anyone give me some advice or tell me what I'm doing wrong, here's the code:
Here is my code for escape-ing and x-ing out the window in Pygame:
And here is my screen setup:
So tuples need to be enclosed in parenthesis and perhaps you need to account for the QUIT event:
Just some thoughts.
def GetInput(Player): key = pygame.key.get_pressed() #Get the state of all the keyboard buttons for event in pygame.event.get(): #For each event: if event.type == QUIT or key[K_ESCAPE]: #if the 'X' on the window was pressed or the user pressed ESCAPE then: pygame.quit(); sys.exit() #Uninitialize PyGame, close the program
And here is my screen setup:
#This will be the size of the screen in pixelsXSCREEN = 1024YSCREEN = 768Screen = (XSCREEN, YSCREEN)pygame.display.set_caption("Space Game")Surface = pygame.display.set_mode(Screen)
So tuples need to be enclosed in parenthesis and perhaps you need to account for the QUIT event:
WINSIZE = (640, 480)
and if event.type == QUIT or key[K_ESCAPE]: pygame.quit(); sys.exit()
Just some thoughts.
Just added the event code and corrected the tuple:
Still freezes
import pygamefrom pygame.locals import *from pygame.color import THECOLORSdef main(): WINSIZE = (640, 480) pygame.init() screen = pygame.display.set_mode(WINSIZE, 0, 8) pygame.display.set_caption('Bounce') screen.fill(THECOLORS["black"]) if event.type == QUIT or key[K_ESCAPE]: pygame.quit(); sys.exit()if __name__ == "__main__": main()
Still freezes
import pygamefrom pygame.locals import *import sysfrom pygame.color import THECOLORSdef main(): WINSIZE = (640, 480) pygame.init() screen = pygame.display.set_mode(WINSIZE, 0, 8) pygame.display.set_caption('Bounce') screen.fill(THECOLORS["black"]) play = True while play == True: key = pygame.key.get_pressed() #Get the state of all the keyboard buttons for event in pygame.event.get(): #For each event: if event.type == QUIT or key[K_ESCAPE]: #if the 'X' on the window was pressed or the user pressed ESCAPE then: play = False pygame.quit(); sys.exit() #Uninitialize PyGame, close the program if __name__ == "__main__": main()
Does this work for you? It does for me.
Quote:Original post by VanillaSnake21
Yes, thanks that works perfectly, I will compare it to my code to see where I went wrong.
No problem. I think all that I added was
import sys
for sys.exit()
and put the key event polling inside a loop; usually the keyboard is polled once each cycle thru the game loop.
Right, thanks again. I just thought of another question and don't want to start a separate thread for it. Even with this little app, debugging and testing my app is a considerable chore. When it freezes I must stop the process pythonw.exe which closes IDLE entirely. Also when debugging the error messages are very general. As you have seen I was using sys.exit() without including system and it silently failed all along without giving me any sorts of heads up. I come from the world of c++ where compilation rules are more or less strict and errors are verbose. I was just wondering is IDLE just a beginners IDE? How are the larger games written in python maintained/debugged?
Quote:I was just wondering is IDLE just a beginners IDE? How are the larger games written in python maintained/debugged?
I don't use IDLE all that much so I don't know its full capabilities/drawbacks. The first tutorial I ever looked at for Python used Eclipse IDE with the PyDev Plugin so I just stuck with that.
Quote:Original post by signal_Quote:I was just wondering is IDLE just a beginners IDE? How are the larger games written in python maintained/debugged?
I don't use IDLE all that much so I don't know its full capabilities/drawbacks. The first tutorial I ever looked at for Python used Eclipse IDE with the PyDev Plugin so I just stuck with that.
From a brief description of Eclipse it seems like its got all the features that IDLE is wanting. Thanks i will definitely try it [smile]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement