Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Pygame Crashing
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nathan4102




PostPosted: Wed Apr 10, 2013 2:53 pm   Post subject: Pygame Crashing

I'm trying to practice GUIs with Pygame, but every time I run my program, it crashes. I'm using WING IDE 4.1.12, Python 2.7, and Pygame 1.9.1, for Python 2.7. Anyone know what the error could be? The shell isn't telling me anything, it stays blank. Here's my code, and a picture of the program.

If anyone could let me know what the problem is, that't be great! And yes, I do have cars.jpg in the directory of GUITest.py.

Python:
import pygame
screen = pygame.display.set_mode((600, 400))
car = pygame.image.load('cars.jpg')
screen.blit(car, (50, 100))


Posted Image, might have been reduced in size. Click Image to view fullscreen.
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Wed Apr 10, 2013 3:40 pm   Post subject: RE:Pygame Crashing

Pretty sure it's because it didn't exit cleanly with pygame.quit(). It's also probably because you didn't call pygame.init() either.

http://www.pygame.org/wiki/FrequentlyAskedQuestions#In%20IDLE%20why%20does%20the%20Pygame%20window%20not%20close%20correctly?
Nathan4102




PostPosted: Wed Apr 10, 2013 4:18 pm   Post subject: RE:Pygame Crashing

Alright, I fixed that, and its still not working right. When I hover over the run window, i get the windows 7 loading cursor, and the window seems to be unresponsive. Also, the picture isn't loading onto the screen, yet no error message shows.

CODE:
import pygame, time
pygame.init()
screen = pygame.display.set_mode((600, 400))
car = pygame.image.load('cars.png')
screen.blit(car, (50, 100))
time.sleep(5)
pygame.quit()
ishidon




PostPosted: Wed Apr 10, 2013 5:11 pm   Post subject: Re: Pygame Crashing

When making games, you always want to have everything in a loop.
This should work...
Python:

import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 400))
car = pygame.image.load('cars.png')
while True:
    screen.blit(car, (50, 100))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
           
    pygame.display.update()
Nathan4102




PostPosted: Wed Apr 10, 2013 5:20 pm   Post subject: Re: Pygame Crashing

ishidon @ Wed Apr 10, 2013 6:11 pm wrote:
When making games, you always want to have everything in a loop.
This should work...
Python:

import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 400))
car = pygame.image.load('cars.png')
while True:
    screen.blit(car, (50, 100))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
           
    pygame.display.update()


Awesome, works perfectly! One thing though, in this piece of code:
Python:
  for event in pygame.event.get():
        if event.type == QUIT:

How exactly would I trigger this? Would I have to made a statement that says "If something happens, then this event is a QUIT type event"?

Thanks for the help, this is my first day with python GUIs :p
Nathan
ishidon




PostPosted: Wed Apr 10, 2013 5:23 pm   Post subject: Re: Pygame Crashing

Python:

for event in pygame.event.get():
        if event.type == QUIT:

No all you do is press the Close button in the top right corner and this will return True.
Nathan4102




PostPosted: Wed Apr 10, 2013 5:25 pm   Post subject: RE:Pygame Crashing

Ah, havn't done buttons yet. Ill get to that soon.
Thanks!
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: