我一直在遵循有关如何移动和成像的教程,当我运行代码时,出现此错误:
回溯(最近通话最近): 在第142行中输入文件“ C:\ Users \ krzys \ Desktop \ Dawid Grubba \ Private \ Game creation \ Hero Jump \ Hero Jump.py” 菜单() 菜单中的文件“ C:\ Users \ krzys \ Desktop \ Dawid Grubba \ Private \ Game creation \ Hero Jump \ Hero Jump.py”,第135行 按钮(“播放”,120、350、200、100,绿色,亮绿色,“开始”) 文件“ C:\ Users \ krzys \ Desktop \ Dawid Grubba \ Private \ Game creation \ Hero Jump \ Hero Jump.py”,第65行,在按钮中 在游戏中() In_Game中的文件“ C:\ Users \ krzys \ Desktop \ Dawid Grubba \ Private \ Game creation \ Hero Jump \ Hero Jump.py”,第110行 y + = y_change UnboundLocalError:赋值之前引用了本地变量'y_change'
我用x替换了y,但仍然显示相同的消息,但用x。 这是网站: https://pythonprogramming.net/pygame-tutorial-moving-images-key-input/
这是我的代码的主要部分,我希望您查看以回答此问题:
def In_Game_Earth_Image(x,y):
gameDisplay.blit(Earth_Image, (x,y))
def Title():
message("Hero Jump")
Character1=pygame.image.load("Normal Character.png")
Character1=pygame.transform.scale(Character1, (100,150))
def Starting_Charcter(x,y):
gameDisplay.blit(Character1, (x,y))
y_change = 0
def things(thingx, thingy, thing_width, thing_height):
gameDisplay.blit(RockImg,[thingx, thingy, 20, 20])
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y +h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x, y, w, h) )
if click[0]==1 and action != None:
if action=="Start":
In_Game()
elif action=="quit":
pygame.quit()
quit()
else:
pygame.draw.rect(gameDisplay, ic,(x, y, w, h) )
smallText=pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect=text_objects(msg, smallText)
textRect.center=( (x+(w/2)), (y+(h/2)))
gameDisplay.blit(textSurf, textRect)
pygame.display.update()
Earth_Image=pygame.image.load("Ground_Image.png")
Earth_Image=pygame.transform.scale(Earth_Image, (900, 600))
def In_Game():
x=50
y=250
In_Game_Earth_Image(0,0)
thing_starty =265
thing_startx= 1200
thing_speed=-7
thing_width=10
thing_height=10
Crashed=False
while not Crashed:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
y_change=-20
if event.type==pygame.KEYUP:
if event.key==pygame.K_SPACE:
y_change=0
y += y_change
In_Game_Earth_Image(0,0)
Starting_Charcter(x, y)
things(thing_startx, thing_starty, thing_width, thing_height)
thing_startx += thing_speed
if thing_startx < -300:
thing_startx = 1000
thing_starty = 265
pygame.display.update()
clock.tick(60)
def Menu():
Menu=True
while Menu:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
Title()
button("Play", 120, 350, 200, 100, green, bright_green, "Start")
button("Quit", 570, 350, 200, 100, bright_red, red, "quit")
pygame.display.update()
clock.tick(15)
gameDisplay.fill(white)
Menu()
pygame.quit()
quit()
我可能犯了一个愚蠢的错误,但是我十岁的时候我对pygame有点傻。 谢谢您的帮助。