Python 移动对象不工作Pygame代码检查
我一直在遵循一个关于如何移动和成像的教程,当我运行代码时,我发现以下错误: 回溯(最近一次呼叫最后一次): 文件“C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py”,第142行,在 菜单() 文件“C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py”,菜单第135行 按钮(“播放”,120350200100,绿色,亮绿色,“开始”) 文件“C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py”,第65行,在按钮中 在游戏中() 文件“C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py”,第110行,在游戏中 y+=y_变化 UnboundLocalError:赋值前引用的局部变量“y_change” 我已将y替换为x,但它仍显示相同的按摩效果,但使用了x。 这是网站: 这是我的代码的主要部分,我想让您查看以回答此问题:Python 移动对象不工作Pygame代码检查,python,pygame,Python,Pygame,我一直在遵循一个关于如何移动和成像的教程,当我运行代码时,我发现以下错误: 回溯(最近一次呼叫最后一次): 文件“C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py”,第142行,在 菜单() 文件“C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py”,菜单第135行 按钮(“播
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()
游戏地球图像中的定义(x,y):
blit(地球图像,(x,y))
def Title():
信息(“英雄跳跃”)
Character1=pygame.image.load(“Normal Character.png”)
Character1=pygame.transform.scale(Character1,(100150))
def起始字符(x,y):
游戏显示.blit(字符1,(x,y))
y_变化=0
定义对象(对象X、对象Y、对象宽度、对象高度):
blit(RockImg,[thingx,thingy,20,20])
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(游戏显示,ac,(x,y,w,h))
如果单击[0]==1并执行操作!=无:
如果操作==“开始”:
在游戏中()
elif操作==“退出”:
pygame.quit()
退出
其他:
pygame.draw.rect(游戏显示,ic,(x,y,w,h))
smallText=pygame.font.font(“freesansbold.ttf”,20)
textSurf,textRect=text\u对象(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
blit(textSurf,textRect)
pygame.display.update()
Earth\u Image=pygame.Image.load(“Ground\u Image.png”)
Earth_Image=pygame.transform.scale(Earth_Image,(900600))
def在游戏()中:
x=50
y=250
游戏中的地球图像(0,0)
事物开始=265
事物开始=1200
速度=-7
东西宽度=10
物体高度=10
崩溃=错误
虽然没有崩溃:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_空间:
y_变化=-20
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_空间:
y_变化=0
y+=y_变化
游戏中的地球图像(0,0)
起始字符(x,y)
事物(事物起点、事物起点、事物宽度、事物高度)
物体启动时间+=物体速度
如果事物开始Tx<-300:
事物\u startx=1000
事物开始=265
pygame.display.update()
时钟滴答(60)
def菜单():
菜单=真
while菜单:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
标题()
按钮(“播放”,120350200100,绿色,亮绿色,“开始”)
按钮(“退出”,570350200100,鲜红色,红色,“退出”)
pygame.display.update()
时钟滴答(15)
游戏显示。填充(白色)
菜单()
pygame.quit()
退出
我可能犯了一个愚蠢的错误,但我觉得我对pygame有点不屑一顾。
谢谢您的帮助。看来您可能需要为您的更改提供一个默认值,以防未到达指定的分支。关于
UnboundLocalError
s:,有很多问题。只需在游戏中的函数中定义一个局部变量y\u change
。你知道如何让我的角色在释放空格键后回到原来的位置吗?