我正在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
原则上,要读取函数内部的全局变量,不需要将其声明为global
(函数内部(。只有当您想修改函数中全局对象的值时,才需要这样做。此外,我还看到了一些不一致的缩进(在声明函数之后(-首先解决这个问题。不过,请尝试在函数中添加global currentsprite
,以防您在其他地方修改它:
def ChooseCharacter():
global currentsprite
...