Sign in to follow this  
VanillaSnake21

Python game freezes

Recommended Posts

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:

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()

Share this post


Link to post
Share on other sites
Here is my code for escape-ing and x-ing out the window in Pygame:


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 pixels
XSCREEN = 1024
YSCREEN = 768
Screen = (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.

Share this post


Link to post
Share on other sites
Just added the event code and corrected the tuple:


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 event.type == QUIT or key[K_ESCAPE]:
pygame.quit(); sys.exit()


if __name__ == "__main__":
main()



Still freezes

Share this post


Link to post
Share on other sites
import pygame
from pygame.locals import *
import sys
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"])

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

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