473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PyGame, window is not closing, tut not helping

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
3 4391
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Erik Bethke | last post by:
Hello All, I am trying to clean up some polish bugs with the Shanghai game I am working on and I am currently stuck on trying to get the right event for detecting when the user has changed the...
3
by: Tim Knauf | last post by:
Hi everyone, I'm glad to have found this list. I've written a small script for my own use which, amongst other things, captures mouse click information from a window containing an image. I used...
2
by: Brent W. Hughes | last post by:
I'm just starting to learn pygame. I write what I think is just about the simplest program that should display a window and then quit. #----------------------------------------------- import sys...
3
by: tlviewer | last post by:
hello, The script below is a prototype for loading playing card images from the bitmap resource in cards.dll (WinXP) I like the idea of not keeping copies of the images as files. I'm able...
2
by: kishkin | last post by:
Hello everybody! I just want to make window transparent. ;) At this moment I've found only ideas of taking screenshots to make background... Anything else? Thanks for help!
0
by: Lunpa | last post by:
My project: I'm working on a game, where in the ui, it takes the pygame window, and shoves it into a gtk2 socket widget. (gtk2 widgets are generated with glade, with the exception of the socket...
5
by: Gregory Piñero | last post by:
Hi guys, I was just idley curious on what it would take to make a web plug-in for Pygame. I'm picturing it working the way my browser currently shows flash games. Is such an idea even...
37
by: Jan Tovgaard | last post by:
Hey everyone:) We have a critical problem, which I can see that other people also has ran into. In Internet Explorer 7 it is no longer possible to do a window.close after opening a window,...
5
by: defn noob | last post by:
Im using PyGame to draw images of graphs and trees. Howver right now i am looping using: while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.