I am trying to make a python space shooter game with pygame.
The error I am getting is: local variable 'event' referenced before assignment.
It is mysterious, because sometimes it appears and breaks my program, and sometimes it doesn't. There is no pattern to the error occurrences, it just happens randomly out of nowhere.
I could run the program 2 times, and it would work fine, but then I run it again, and it gives this error.
The code that I am showing is a very simplified version of the real program, but it still produces the same error.
import pygame
WIDTH = 640
HEIGHT = 660
FPS = 30
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
BLUE = (0,0,255)
GREEN = (0,255,0)
pygame.init()
pygame.mixer.init()
gamedisplay = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game!")
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group()
def quit_game():
pygame.quit()
def message_to_screen(msg, colour, x, y):#This function is used to display messages to the game display.
font = pygame.font.SysFont(None, 25)
text = font.render(msg, True, colour)
gamedisplay.blit(text, (x,y))
def start_screen():
global intro_running
intro_running = True
while intro_running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quit_game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit_game()
if event.key == pygame.K_a:
main_game_loop()
gamedisplay.fill(WHITE)
message_to_screen("start screen: press \"a\" to start", RED, 200, 200)
pygame.display.update()#Updating the display.
def main_game_loop():
intro_running = False#I make intro_running equal False here so that when this subprogram starts, the start_screen subprogram will end
running = True
while running:
clock.tick(FPS)
pygame.mouse.set_visible(0)#Makes the mouse invisible.
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quit_game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit_game()
gamedisplay.fill(GREEN)
message_to_screen("Main game will be here", RED, 200, 200)
all_sprites.draw(gamedisplay)
all_sprites.update(event)#The event parameter needs to be passed, because some sprites in the group need it to check for events.
pygame.display.update()
start_screen()
The full error is:
Traceback (most recent call last):
File "C:/Users/Home/Documents/Python/pygame/test game/re-creation.py", line 106, in <module>
start_screen()
File "C:/Users/Home/Documents/Python/pygame/test game/re-creation.py", line 64, in <module>
main_game_loop()
File "C:/Users/Home/Documents/Python/pygame/test game/re-creation.py", line 98, in <module>
all_sprites.update(event)#All sprites will be updated, each frame.
builtins.UnboundLocalError: local variable 'event' referenced before assignment
All help will be greatly appreciated.
Thank you.
Your sprites probably doesn't need the event argument but it's your game...you can easily fix this by placing it inside the event loop, since your error was caused because event was never defined during the first iteration of the while loop since there wasn't any event therefore the variable event was never generated.
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quit_game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit_game()
all_sprites.update(event) # this will guarantee that there's an event
But you can also consider doing this to make sure the sprite will always get updated:
class mysprite(pygame.sprite.Sprite): # example sprite class
def __init__(self, event = None): # allow the event to have a default None argument
if event is None:
# do stuff without the event
return
# do stuff with the event
# the event loop snippet
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quit_game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit_game()
all_sprites.update(event) # this will guarantee that there's an event
all_sprites.update() # without the event outside of the event loop in the while loop, where the all_sprites.update(event) used to be in your example, so the sprites will always get updated
In addition to what was provided in the other answer (you have to apply what is suggested there) there are also other causes of trouble with Pygame:
Pygame mysterious errors could have something to do with what else is going on the computer. I run your script thousand times without any error, BUT ... I witnessed while using a screenshot utility to capture the Pygame window a freezing display to a degree that I had to reboot the computer (but only if special timing of closing the Pygame window and doing the screenshot came together). From this experience I suggest in case the advice given in the other answer doesn't prevent your game from crash for what reason ever:
CHECK which of the applications running in background or foreground may have interfere with Pygame.
It could maybe be easy to detect as there are times the error occurs and times at which it doesn't, but if in addition a special timing of events of the application which interfere with Pygame is necessary for the error to occur it could be very hard to find.
There is no guarantee that this will solve your problem, but maybe it can.
If you want to witness yourself that Pygame handling of events and refreshing the display is not well designed just run a very simple Pygame application doing nothing else as showing an empty window with FPS of one frame per second (FPSclock = pygame.time.Clock() -> FPSclock.tick(1)). If on your box happens the same as on mine you will see the CPU being extremely busy with Pygame. Pygame is just a TOY - don't expect from it too much ...
Maybe you can find out how to modify your code to avoid that the special error occurs, BUT this is with high probability a kind of lottery game. I suppose there is no way to make Pygame run 100% safe from errors and problems under any circumstances. If you want that, switch to another known as stable and mature gaming engine.
It's life - if you gain something on one side you loose some on the other side. There is sure a price to pay for the ease of programming games with Pygame - maybe you can arrange yourself with accepting that it is not a problem if occasionally a mystic error comes out of nowhere?
Related
I posted this question yesterday, but I accidentally posted the wrong code. The program still doesn't work though. I am trying to make a game using pygame and my pictures come from an app called Pixel Art Studio. When I run the code though, I don't get any error messages, just a blank, white screen. I'm following a python programming book called Hello World but this is an original program. Please tell me what I've done wrong, here's my code.
import pygame, sys
guyimages = ['swordsmanguy.pxm','swordsmanstabup.pxm','swordsmanstabstraight.pxm','swordsmanstabdown.pxm']
legsimages = ['swordsman.legs1.pxm','swordsman.legs2.pxm','swordsman.legs3.pxm','swordsman.legs2.3.pxm','swordsman.legs2.2.pxm']
class avatar(pygame.sprite.Sprite):
def __init__(self):
pygame.sprit.Sprite.__init__(self)
self.image = pygame.image.load(guyimages["swordsmanguy.pxm"])
self.rect = self.image.get_rect()
self.rect.center = [250, 250]
self.angle = 0
def animate():
screen.fill([255,255,255])
screen.blit(avatar.image, avatar.rect)
pygame.display.flip
pygame.init()
screen = pygame.display.set_mode([960, 640])
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
animate()
pygame.quit()
When you set self.image you reference the guyimages list like it's a dictionary. Additionally, there seem to be some parentheses missing from your code. Do you get an error in the terminal / command prompt window from which you run this script?
You are missing parentheses after animate in the code you posted.
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
animate #HERE MISSING PARENTHESES
Here it is, I dont know what is wrong, I looked at other answers but I still dont know what is wrong?
import pygame
pygame.init()
gameWindow = pygame.display.set_mode((1000,600));
pygame.display.set_caption("Practice")
#game starts
gameActive = True
while gameActive:
for event in pygame.event.get():
#print event
if event.type == pygame.QUIT:
gameActive = False
pygame.quit()
quit()
You have pygame.quit() in your main loop, so after one iteration through the loop you are calling pygame.quit(), which causes pygame to no longer be initialized which creates the error of not having a display surface.
Moving pygame.quit() out of the main while loop fixes the issue.
This program infinite loops. Does nothing. Won't take input or print anything. Ideas?
import pygame
pygame.init()
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print "hi"
running = 0
The problem with your script is solely that there's no window that could capture the events.
You have to create and initialize a window with pygame.display.set_mode first.
import pygame
pygame.init()
# create a window that will capture the events
pygame.display.set_mode((200, 200))
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print "hi"
running = 0
Try the following:
import pygame, sys
pygame.init()
pygame.mixer.init(22050, -16, 2, 3072)
pygame.mixer.music.load("Kundara_Lake-of-Dust-320.mp3")
pygame.mixer.music.play(1, 0.0)
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.mixer.music.stop()
pygame.quit()
running = 0
From http://www.pygame.org/docs/ref/pygame.html
pygame.quit
Uninitialize all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue. It is safe to call this function more than once: repeated calls have no effect.
Note, that pygame.quit will not exit your program. Consider letting your program end in the same way a normal python program will end.
You are looping infinitely calling pygame.quit() due to while(1).
You need to update the screen. Try it with
screen = pygame.display.set_mode((640,360),0,32)
and in the loop, write
pygame.dispay.flip()
to close the window completely, you can use
sys.exit()
just be sure to include sys in your imports
pygame does not recognize the term 'input' or 'print'. It would be a lot simpler if it did!! Instead, to get text onto the screen, you must use 'drawText('message',font,surface,(xpos,ypos) putting your own text in 'message', your own surface name in 'surface' and the x and y co-ordinates in 'xpos' and 'ypos'.
I am learning to use Pygame, and when I use sys.exit(), I run into a problem. Here is the code:
import pygame, sys,os
from pygame.locals import *
pygame.init()
window = pygame.display.set_mode((468, 60))
pygame.display.set_caption('Game')
screen = pygame.display.get_surface()
file_name = os.path.join("data","image.bmp")
surface = pygame.image.load(file_name)
screen.blit(surface, (0,0))
pygame.display.flip()
def input(events):
for event in events:
if event.type == QUIT:
sys.exit(0)
else:
print event
while True:
input(pygame.event.get())
It's really just the code from the pygame tutorial. The problem occurs when I actually try to exit, regardless of what event I try to use to sys.exit().
Traceback (most recent call last):
File "C:/Python27/Lib/site-packages/pygame/examples/test.py", line 25, in <module>
input(pygame.event.get())
File "C:/Python27/Lib/site-packages/pygame/examples/test.py", line 20, in input
sys.exit(0)
SystemExit: 0
... And then the program doesn't exit. What am I doing wrong here? Because I did notice that this code was for an antiquated version of Python.
sys.exit()
alone is a bit unholy with pygame.. the proper way to exit a pygame app is to first break out of the mainloop then quit pygame then quit the program. ie.
while running == True:
# catch events
if event_type == quit:
running = False # breaks out of the loop
pygame.quit() # quits pygame
sys.exit()
also it seems to me that you aren't catching the event properly.. it should be
if event.type == pygame.QUIT:
you can read more about events in pygame here.
sys.exit just throws an exception (a SystemExit exception). This has two unusual effects:
It only exits the current thread in a multithreaded application
The exception could have been caught.
I solved this problem and the right code is below:
running = True
while running == True:
for event in pygame.event.get():
if event.type == QUIT:
running = False # Exiting the while loop
screen.blit(background, (0,0))
pygame.display.update()
pygame.quit() # Call the quit() method outside the while loop to end the application.
I have read in some sources there is a conflict between the mainloop() in Tkinter which runs the Python shell and Pygame.init() which the sys.exit() command follows.
The suggestion was to run the game from the command line to get round the problem rather than load the game using run (F5) from the shell.
A good side effect of this was that in my space invaders game, which does a lot of variable updating: 35 times a second, was that the animation ran correctly whereas from the shell it ran poorly and was jerky.
If i use the following code:
if event.type == QUIT:
pygame.quit()
sys.exit()
the game exits correctly but leaves an error message in the shell which has no effect on the game and is largely redundant. It is just a little ugly. This does not happen from the command line.
Summary: try running the game from the command line to avoid Tkinter problems
If you still have the issue, (after breaking the loop) try using sys.exit(0) instead of sys.exit(). Hope it'll help. It worked for me. It seems pygame expects the 'status' argument (i.e. 0 here) to be passed in explicitly.
See the below example:
isRunning = True
while isRunning:
# Catch events
if event.type == pygame.QUIT:
isRunning = False # Breaks the loop
pygame.quit()
sys.exit(0)
I've planning on writing a code in Pygame and I was just getting started with the basics and found that the executing code was really slow. When I press a key it takes a while for it to print it in the terminal (there doesn't seem to be any pattern to it).
I'm running Python 2.6, I downgraded after coming across this problem. With further testing I've found that the whole system slows down. Has anyone come across this or got a solution so it runs faster or/and prevents the system from slowing down?
OS - Ubuntu
Hardware - Macbook Pro
import pygame
import pygame.locals
pygame.mixer.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("bla")
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(pygame.Color("green"))
screen.blit(background, (0, 0))
looping = True
while looping:
for event in pygame.event.get():
if event.type == pygame.QUIT:
looping = False
elif event.type == pygame.KEYDOWN:
keyName = pygame.key.name(event.key)
print "key pressed:", keyName
if event.key == pygame.K_SPACE:
print "Loading Music"
pygame.mixer.music.load("born.mp3")
elif event.key == pygame.K_ESCAPE:
looping = False
pygame.display.flip()
If there's any further information I can provide I would be happy to help.
pyGame is based on SDL which is internally based on threads.
When you have threading, print messages are basically a no-no. Because often times because of the scheduler slices (which are large in SDL), the print messages get delayed. Its not that pygame is slow (it is some situations, but, not in this one), its just that the print statement is in a seperate event thread.
Try doing this in pygame, it'll run pretty well.