繁体   English   中英

pygame 中定义的变量抛出错误

[英]defined variable throwing error in pygame

我正在制作我的第一个 pygame 项目,这是一个迷宫项目。 当精灵触及蓝色时,它应该打印出“win”。 但是当它触及蓝色时,它会打印出许多胜利。 所以,我添加了一个 won 变量来记录玩家已经赢了。 但它是说没有定义赢。 请帮忙。 我在下面给出我的代码。

import pygame
import keyboard
pygame.init()
img = pygame.image.load('maze.jpg')
spr=pygame.image.load('C:/Users/abhijato chatterjee.LAPTOP-F4VMRK00/Desktop/my 
folder/programs/python/pygame/Mushroom expansion/PNG/tallShroom_red.png')
screen = pygame.display.set_mode((640, 480))
screen.fill((255,255,255))
pygame.mixer.init()
q=False
won=False
class Players(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=spr
        self.image.set_colorkey((255,255,255))
        self.rect=self.image.get_rect()
        self.rect.bottomleft=(30,460)
    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.rect.x -= 2
        if keys[pygame.K_RIGHT]:
            self.rect.x += 2
        if keys[pygame.K_UP]:
            self.rect.y -= 2
        if keys[pygame.K_DOWN]:
            self.rect.y += 2
        r,g,b = screen.get_at((self.rect.bottomright[0], self.rect.bottomright[1]))[:3]
        listz=[r,g,b]
        r,g,b = screen.get_at((self.rect.bottomleft[0], self.rect.bottomleft[1]))[:3]
        listy=[r,g,b]
        r,g,b = screen.get_at((self.rect.center[0], self.rect.center[1]))[:3]
        listx=[r,g,b]
        r,g,b = screen.get_at((self.rect.right, self.rect.y))[:3]
        listw=[r,g,b]
        r,g,b = screen.get_at((self.rect.topleft[0], self.rect.topleft[1]))[:3]
        listv=[r,g,b]
        r,g,b = screen.get_at((self.rect.topright[0], self.rect.topright[1]))[:3]
        listu=[r,g,b]
    if listz==[0,0,0] or listy==[0,0,0] or listx==[0,0,0] or listw==[0,0,0] or listv==[0,0,0] or 
listu==[0,0,0]:
            self.rect.bottomleft=(30,460)
        if won==False:
            if listz[0]<21 and listz[1]>142 and listz[1]<184 and listz[2]>212 and listz[2]<254:
                print("you win!!!")
            elif listx[0]<21 and listx[1]>142 and listx[1]<184 and listx[2]>212 and listx[2]<254:
                print("you win!!!")
            elif listw[0]<21 and listw[1]>142 and listw[1]<184 and listw[2]>212 and listw[2]<254:
                print("you win!!!")
            elif listv[0]<21 and listv[1]>142 and listv[1]<184 and listv[2]>212 and listv[2]<254:
                print("you win!!!")
            elif listy[0]<21 and listy[1]>142 and listy[1]<184 and listy[2]>212 and listy[2]<254:
                print("you win!!!")
            elif listu[0]<21 and listu[1]>142 and listu[1]<184 and listu[2]>212 and listu[2]<254:
                print("you win!!!")
            won=True
player=Players()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
clock = pygame.time.Clock()
while q==False:
    screen.blit(img,(0, 0))
    pygame.display.update() 
    all_sprites.update()
    all_sprites.draw(screen)
    pygame.display.flip()
    clock.tick(30)
    for event in pygame.event.get() :
        if event.type == pygame.QUIT : 
            q=True
            pygame.quit()

然后它给了我以下错误:

Traceback (most recent call last):

  File "C:\Users\abhijato chatterjee.LAPTOP-F4VMRK00\Desktop\my folder\programs\python\pygame\maze.py", line 68, in <module>
    all_sprites.update()

  File "C:\Users\abhijato chatterjee.LAPTOP-F4VMRK00\anaconda3\lib\site-packages\pygame\sprite.py", line 463, in update
    s.update(*args)

  File "C:\Users\abhijato chatterjee.LAPTOP-F4VMRK00\Desktop\my folder\programs\python\pygame\maze.py", line 42, in update
    if won==False:

UnboundLocalError: local variable 'won' referenced before assignment

wow是一个全局变量。 您必须使用global语句来访问全局命名空间中的变量:

won=False

class Players(pygame.sprite.Sprite):
    # [...]

    def update(self):
        global won

        # [...]

        if won==False:
            # [...]

            won=True

实际上global关键字并不是为了让您可以读取全局范围变量的值。 没有它它会很好地阅读它。 只有当您尝试编写它时,您才会遇到范围问题。 如果您尝试写入全局范围变量,它将创建一个局部范围变量,该变量隐藏更高范围的变量并且不会更改全局变量。 global告诉它不要这样做并更新全局变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM