簡體   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