全局变量使赋值前的局部变量被刷新

时间:2018-12-21 04:36:03

标签: python variables global-variables

我正在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放在那儿,但这是行不通的。如果有帮助 我在Windows 10上使用IDLE 3.7.0。 TIA

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

1 个答案:

答案 0 :(得分:0)

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

def ChooseCharacter():
    global currentsprite
    ...