【发布时间】: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
【问题讨论】:
-
对这里的完整代码感到抱歉-pastebin.com/YjU8KiuV
-
通过编辑您的问题在此处发布您的 MCVE。确保它是最小的。
-
@ImaGonnaDie 不是minimal reproducible example
-
我知道如果出现其他问题,我决定展示我的完整代码。
标签: python variables global-variables