By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,785 Members | 2,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,785 IT Pros & Developers. It's quick & easy.

PyGame, window is not closing, tut not helping

P: n/a
im doing this :
http://www.learningpython.com/2006/0...game-part-one/

and when closing the program the window stays up and doesnt respond. i
tried adding this:
http://www.pygame.org/wiki/FrequentlyAskedQuestions

bu it doesnt work, or maybe im doing it wrong.

heres the code without the added tutorial exit:
import os, sys
import pygame
from pygame.locals import *

if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

class PyManMain:
"""The Main PyMan Class - This class handles the main
initialization and creating of the Game."""

def __init__(self, width=640,height=480):
"""Initialize"""
"""Initialize PyGame"""
pygame.init()
"""Set the window Size"""
self.width = width
self.height = height
"""Create the Screen"""
self.screen = pygame.display.set_mode((self.width
, self.height))
def MainLoop(self):
"""This is the Main Loop of the Game"""
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
class Snake(pygame.sprite.Sprite):
"""This is our snake that will move around the screen"""

def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('snake.png',-1)
self.pellets = 0

if __name__ == "__main__":
MainWindow = PyManMain()
MainWindow.MainLoop()

Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
another program that has the same problem:
import sys, pygame
pygame.init()

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("snake.png")
ballrect = ball.get_rect()

while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom height:
speed[1] = -speed[1]

screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
Jun 27 '08 #2

P: n/a
On May 1, 10:55*am, globalrev <skanem...@yahoo.sewrote:
im doing this :http://www.learningpython.com/2006/0...e-in-python-us...

and when closing the program the window stays up and doesnt respond. i
tried adding this:http://www.pygame.org/wiki/FrequentlyAskedQuestions

bu it doesnt work, or maybe im doing it wrong.

heres the code without the added tutorial exit:

import os, sys
import pygame
from pygame.locals import *

if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

class PyManMain:
* * """The Main PyMan Class - This class handles the main
* * initialization and creating of the Game."""

* * def __init__(self, width=640,height=480):
* * * * """Initialize"""
* * * * """Initialize PyGame"""
* * * * pygame.init()
* * * * """Set the window Size"""
* * * * self.width = width
* * * * self.height = height
* * * * """Create the Screen"""
* * * * self.screen = pygame.display.set_mode((self.width
* * * * * * * * * * * * * * * * * * * * * * * *, self.height))

* * def MainLoop(self):
* * * * """This is the Main Loop of the Game"""
* * * * while 1:
* * * * * * for event in pygame.event.get():
* * * * * * * * if event.type == pygame.QUIT:
* * * * * * * * * * sys.exit()

class Snake(pygame.sprite.Sprite):
* * """This is our snake that will move around the screen"""

* * def __init__(self):
* * * * pygame.sprite.Sprite.__init__(self)
* * * * self.image, self.rect = load_image('snake.png',-1)
* * * * self.pellets = 0

if __name__ == "__main__":
* * MainWindow = PyManMain()
* * MainWindow.MainLoop()
I think the issue is that you're running it from within IDLE. It looks
like pyGame's event loop and Tkinter's event loop interfere with each
other. If you run the scripts from the command line, it works.

Mike
Jun 27 '08 #3

P: n/a
JL
If the game runs normally without IDLE, then, to run it from IDLE, add
pygame.quit() at the end of the script:

if __name__ == "__main__":
MainWindow = PyManMain()
MainWindow.MainLoop()
pygame.quit()

.... and just before the sys.exit():

def MainLoop(self):
"""This is the Main Loop of the Game"""
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.