【问题标题】:Global variable gives local variable refrenced before assignment全局变量给出赋值前引用的局部变量
【发布时间】:2018-12-21 04:36:02
【问题描述】:

我正在 pygame 中制作一个游戏,您可以在其中选择更改菜单中的角色。我有问题的代码如下所示。

import pygame
import time
pygame.init()
screen_w = 1200
screen_h = 700
white = (255, 255, 255)
black = (0, 0, 0)
light_grey = (135, 135, 135)
medium_grey = (100, 100, 100)
grey =  (90, 90, 90)
global currentsprite
currentsprite = 1
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
smalltext = pygame.font.Font("font1.ttf", 20)
normaltext = pygame.font.Font("font1.ttf", 40)
mediumtext = pygame.font.Font("font1.ttf", 30)
largetext = pygame.font.Font("font1.ttf", 60)
window = pygame.display.set_mode((screen_w, screen_h))
importsprite1 = pygame.image.load("CharacterSprite1.png")
importsprite2 = pygame.image.load("CharacterSprite2.png")
importsprite3 = pygame.image.load("CharacterSprite3.png")
importsprite1fireball1 = pygame.image.load("RedFireBall1.png")
importsprite2fireball1 = pygame.image.load("PurpleFireBall1.png")
importsprite3fireball1 = pygame.image.load("YellowFireBall1.png")





def format_image(image, factor, grid_x, grid_y):
    step1 = image.get_size()
    new_image = pygame.transform.scale(image, (int(step1[0] * factor), int(step1[1] * factor)))
    window.blit(new_image, (grid_x, grid_y))


def Button(msg, font, x, y, w, h, c):
    pygame.draw.rect(window, c, (x, y, w, h))
    textsurf, textrect = text_labels(msg, font)
    textrect.center = ((x+(w/2)), (y+(h/2)))
    window.blit(textsurf, textrect)


def ChooseCharacter():
    window.fill(light_grey)
    Button("Sprite 1", largetext, 100, 50, 250, 150, grey)
    Button("Sprite 2", largetext, 100, 280, 250, 150, grey)
    Button("Sprite 3", largetext, 100, 500, 250, 150, grey)
    if currentsprite == 1:
        format_image(importsprite1, 1.35, (screen_w/2+50), 65)
    elif currentsprite == 2:
        format_image(importsprite2, 1.35, (screen_w/2+50), 65)
    elif currentsprite == 3:
        format_image(importsprite3, 1.35, (screen_w/2+50), 65)
    pygame.display.update()
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("WHY")
                quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if onPos(100, 50, 250, 150):
                    currentsprite = 1
                    DrawMainMenu()
                if onPos(100, 200, 250, 150):
                    currentsprite = 2
                    DrawMainMenu()
                if onPos(100, 350, 250, 150):
                    currentsprite = 3
                    DrawMainMenu()
                if onPos(100, 500, 250, 150):
                    DrawMainMenu()


def text_labels(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()
def onPos(x, y, w, h):
    mouse = pygame.mouse.get_pos()
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        return True
    else:
        return False


def DrawMainMenu():
    window.fill(light_grey)
    Button("Start", mediumtext, 100, 50, 250, 100, grey)
    Button("Multiplayer", mediumtext, 100, 200, 250, 100, grey)
    Button("Choose Character", mediumtext, 100, 350, 250, 100, grey)
    Button("Exit", mediumtext, 100, 500, 250, 100, grey)
    if currentsprite == 1:
        format_image(importsprite1, 1.35, (screen_w/2+50), 65)
    elif currentsprite == 2:
        format_image(importsprite2, 1.35, (screen_w/2+50), 65)
    elif currentsprite == 3:
        format_image(importsprite3, 1.35, (screen_w/2+50), 65)
    pygame.display.update()
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("WHY")
                quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if onPos(100, 50, 250, 100):
                    print("clicked start")
                if onPos(100, 200, 250, 100):
                    print("clicked Multiplayer")
                if onPos(100, 350, 250, 100):
                    ChooseCharacter()
                if onPos(100, 500, 250, 100):
                    exit()
DrawMainMenu()

我遇到的问题是它一直声明在分配之前引用了局部变量 currentsprite,我尝试在声明 currentsprite 之前输入global currentsprite,但这不起作用。如果有帮助 我在使用 IDLE 3.7.0 的 Windows 10 上。 TIA

编辑: 我实际上在另一个函数中使用了变量currentsprite,在其中我确实更改了它的值(是的,这是可行的)我将重申更改 currentsprite 的值仅在另一个变量中有效,但即使在 ChooseCharacter 中引用它也会出错。完整代码可以看这里-pastebin.com/YjU8KiuV

【问题讨论】:

  • 您需要提供minimal reproducible example
  • 对这里的完整代码感到抱歉-pastebin.com/YjU8KiuV
  • 通过编辑您的问题在此处发布您的 MCVE。确保它是最小的。
  • @ImaGonnaDie 不是minimal reproducible example
  • 我知道如果出现其他问题,我决定展示我的完整代码。

标签: python variables global-variables


【解决方案1】:

原则上,读取函数内部的全局变量不需要将其声明为global(在函数内部)。仅当您要在函数中修改全局对象的值时才需要这样做。我还看到一些不一致的缩进(在声明函数之后) - 首先解决这个问题。不过,请尝试在函数中添加global currentsprite,以防万一您在其他地方修改它:

def ChooseCharacter():
    global currentsprite
    ...

【讨论】:

  • 对不起,我应该说明这一点,但我实际上在另一个函数中使用了变量 currentsprite,在其中我确实更改了它的值(是的,它有效)
  • 不,更改 currentsprite 的值ONLY 在另一个变量中起作用,但即使在 ChooseCharacter 中引用它也会出现错误完整代码可以在这里看到-pastebin.com/YjU8KiuV
猜你喜欢
  • 1970-01-01
  • 2013-08-02
  • 2011-11-06
  • 2018-01-12
  • 1970-01-01
相关资源
最近更新 更多