Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[pygame] Local variable referenced by assignment

Started by MattFrederick Feb 4, 2011 at 7:08 AM 3 replies 2.2k views
Original Post
MattFrederick
MattFrederick
The program boots up but whenever I try to move the sprite the error unbounderror local variable 'data' referenced before assignment.
I do not know how to fix this error. I tried to fix the spacing because for some reason it messes up whenever I post it in the code tags.
Here is a pastebin of the code. http://pastebin.com/xWTsfAbT

import pygame
from pygame.locals import *

pygame.init()
pygame.mixer.init()
size = (640,480)
screen = pygame.display.set_mode(size)

class Ship(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("Ship1.bmp")
self.image = self.image.convert()
self.image.set_colorkey((255,255,255))
self.rect = self.image.get_rect()
self.rect.centerx = 300
self.rect.centery = 400
self.left = -1
self.right = +1
self.up = -1
self.down = +1

def checkkey(data):
(event) = data
if event.key == K_LEFT:
self.rect.centerx = self.rect.centerx + self.left
if event.key == K_RIGHT:
self.rect.centerx = self.rect.centerx + self.right
if event.key == K_UP:
self.rect.centery = self.rect.centery + self.up
if event.key == K_DOWN:
self.rect.centery = self.rect.centery + self.down
data = (event)
return data

def update(self):
if self.rect.centerx > screen.get_width():
self.rect.centerx = screen.get_width()
if self.rect.centerx < 0:
self.rect.centerx = 0
if self.rect.centery > screen.get_height():
self.rect.centery = screen.get_height()
if self.rect.centery < 0:
self.rect.centery = 0

def main():
pygame.display.set_caption("Ship Test")

bg = pygame.Surface(screen.get_size())
bg = bg.convert()
bg.fill((123,200,100))
screen.blit(bg,(0,0))
ship = Ship()
allsprites = pygame.sprite.Group(ship)

ok = True
clock = pygame.time.Clock()

while ok:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
ok = False
if event.type == KEYDOWN:
ship.data = (event)
data = ship.checkkey(data)
(event) = ship.data
if event.key == K_ESCAPE:
ok = False
allsprites.clear(screen, bg)
allsprites.update()
allsprites.draw(screen)
pygame.display.update()
if __name__ == "__main__":
main()

Hollower
Hollower
Change [font="Arial"]def checkkey(data)[/font]: [font="Arial"] to [/font][font="Arial"]def checkkey(self, data):[/font]
MattFrederick
MattFrederick
I did what you said but the error still persists.
Hollower
Hollower
[font="Verdana"]Sorry. I don't have much experience with pygame so I assumed the obvious error was the error. Looking closer I see some weird stuff

[/font][font="Microsoft Sans Serif"](event) = data
data = (event)

ship.data = (event)
(event) = ship.data

[/font]

[font="Verdana"]...etc. None of those lines makes sense to me. [/font][font="Verdana"]They don't do anything, and I can't infer what you are trying to do with them. Get rid of all references to this "data".[/font]

[font="Verdana"]The specific line with the error is:
[/font]

[font="Courier New"]data = ship.checkkey(data)[/font]

[font="Verdana"]You're passing data before it has been assigned. But like I said all of that stuff doesn't appear to do anything.
[/font]

[font="Verdana"](argh, sorry now the forum software is bugging out on me and making my post formatted all weird)[/font]



Captain P
Captain P
Hollower already spotted the problems, but there's something else you should know: 'referenced before assignment' errors are caused by accessing variables before they have been assigned a value. These errors show you a traceback, pointing you at the offending code, and where it was called from. In this case, it points us towards line 66: data = ship.checkkey(data) As Hollower already mentioned, 'data' needs to exist before it can be passed into ship.checkkey() - only after that function has returned will the result be assigned to 'data'.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.