[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Getting unknown error



I've never heard of this error before: UnboundLocalError: local variable 'rect' referenced before assignment

[code]
import os, sys
import pygame
from pygame.locals import *
import pickle

rect = {}
       
class LockandStock():
       
    def __init__(self):
        pygame.init()
        self.window = pygame.display.set_mode((640, 480))
        self.surface = pygame.Surface(self.window.get_size ())
        self.surface.convert()
        #self.pictureglass = pygame.Surface((640, 480))
        #self.pictureglass.convert()
        self.surface.fill((255, 255, 255))
        #self.pictureglass.fill((255, 255, 255))

    def MainLoop(self):
        while True:
            for self.event in pygame.event.get():
        if self.event.type == QUIT:
                    return
               
        elif self.event.type == pygame.KEYDOWN:
                    if self.event.key == K_n:
                        self.surface.fill((118, 118, 118))
                   
            elif self.event.key == K_o:
                    try:
                            self.image_name = raw_input("Name of Pciture: ")
                self.image = pygame.image.load(self.image_name)
                self.image = self.image.convert()
                self.surface.blit(self.image, (0, 0))
                self.last = self.image_name[(len(self.image_name) - 4): len(image_name)]

                       except:
                        print "*** Picture not found! ***"

                    elif self.event.key == K_i:
                        try:
                            self.in_file = open((self.image_name.strip(self.last)) + ".las", "r")
                            rect = pickle.load(self.in_file)
                            self.in_file.close()

                            for x in rect.keys():
                                pygame.draw.rect(self.surface, ((255, 255, 0)), rect[x], 1)

                            self.numRect = x

                        except:
                            print "*** No .las file to load! ***"

                elif self.event.type == pygame.MOUSEBUTTONDOWN :
                    if self.event.button == 1:
                        # If point_draw = 1, get x,y of mouse
            if self.point_draw == 1:
                            self.mouseX1, self.mouseY1 = pygame.mouse.get_pos ()
                self.point_draw = 2
            else:
                            # If point_draw = 2, get x,y of mouse, make a rect and throw it in a dictionary
                self.mouseX2, self.mouseY2 = pygame.mouse.get_pos()
                self.numRect += 1
                self.newRect = pygame.Rect(self.mouseX1, self.mouseY1, self.mouseX2 - self.mouseX1, self.mouseY2 - self.mouseY1)
                rect[self.numRect ] = self.newRect
                self.point_draw = 1
            elif self.event.button == 3:
            self.mouseXTEMP, self.mouseYTEMP = pygame.mouse.get_pos()
            for x in rect.keys():
                            if rect[x].collidepoint( self.mouseXTEMP, self.mouseYTEMP):
                del rect[x]
                self.numRect -= 1

            # draws the rectangles in the rect{}
            if rect.keys() <> 0:
                for i in rect.keys():
                    pygame.draw.rect(self.surface, ((255, 255, 0)), rect[i], 1)

            self.window.blit(self.surface, (0, 0))
            pygame.display.flip()
           
       
if __name__ == "__main__":
    app = LockandStock()
    app.MainLoop()

[/code]

any help is great. thanks in advance.

-Eric