使用Pygame的Python作用域
我有点困惑,为什么在调用redraw_game_window()函数时,右变量没有正确执行,当我按右箭头键时返回False。在,if键[pygame.K_RIGHT]:下,它返回True。我是否需要调用代码中较高的重画游戏窗口。我想这应该是代码中的最后一件事。为了便于阅读,我省略了代码的开头 因此,当我按下向右箭头键时,它实际上处于激活状态,但即使我提供了全局标志,它也不会继续。这里发生了什么使用Pygame的Python作用域,python,pygame,Python,Pygame,我有点困惑,为什么在调用redraw_game_window()函数时,右变量没有正确执行,当我按右箭头键时返回False。在,if键[pygame.K_RIGHT]:下,它返回True。我是否需要调用代码中较高的重画游戏窗口。我想这应该是代码中的最后一件事。为了便于阅读,我省略了代码的开头 因此,当我按下向右箭头键时,它实际上处于激活状态,但即使我提供了全局标志,它也不会继续。这里发生了什么 # Moving Variables moving_x = 100 moving_y
# Moving Variables
moving_x = 100
moving_y = 350
walk = 1
vertical = 1
jump = False
jump_count = 10
left = False
right = False
walkCount = 10
# Christmas Background scaled to fit the size of the full display
christmas = pygame.image.load('winter.jpg')
christmas = pygame.transform.scale(christmas, (650, 600))
def redraw_game_window():
global right
screen.blit(christmas, (0, 0))
print(right)
# ///////////////////////////////////////////////////
# MAIN LOOP
# ///////////////////////////////////////////////////
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
moving_x += walk
right = True
left = False
print(right)
if moving_x >= 615:
first_screen = False
second_screen = True
moving_x = 0
if keys[pygame.K_LEFT]:
moving_x -= walk
left = True
right = False
if moving_x == -50:
moving_x = -10
else:
right = False
true = False
walkCount = 0
if not jump:
if keys[pygame.K_SPACE]:
jump = True
right = False
left = False
walkCount = 0
else:
if jump_count >= -10:
neg = 1
if jump_count < 0:
neg = -1
moving_y -= (jump_count ** 2) * 0.5 * neg
jump_count -= 1
else:
pygame.time.delay(200)
jump = False
jump_count = 10
redraw_game_window()
# ////////////////////////////////////////////////
# PYGAME CLOSING UTILITIES
# ////////////////////////////////////////////////
clock.tick(120)
pygame.display.flip()
pygame.quit()
#移动变量
移动_x=100
移动_y=350
步行=1
垂直=1
跳转=错误
跳转计数=10
左=假
右=假
步行次数=10
#圣诞节背景经过缩放,以适应整个屏幕的大小
圣诞节=pygame.image.load('winter.jpg')
圣诞节=pygame.transform.scale(圣诞节,(650600))
def重新绘制游戏窗口():
全球权利
屏幕。blit(圣诞节,(0,0))
打印(右)
# ///////////////////////////////////////////////////
#主回路
# ///////////////////////////////////////////////////
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
keys=pygame.key.get_pressed()
如果键[pygame.K_RIGHT]:
移动x+=行走
右=真
左=假
打印(右)
如果移动_x>=615:
第一屏=假
第二屏=真
移动_x=0
如果键[pygame.K_左]:
移动x-=行走
左=真
右=假
如果移动_x==-50:
移动_x=-10
其他:
右=假
真=假
步行次数=0
如果没有跳转:
如果键[pygame.K_SPACE]:
跳转=真
右=假
左=假
步行次数=0
其他:
如果跳转计数>=-10:
负=1
如果跳转计数<0:
负=-1
移动y-=(跳跃计数**2)*0.5*负
跳转计数-=1
其他:
pygame.时间延迟(200)
跳转=错误
跳转计数=10
重新绘制游戏窗口()
# ////////////////////////////////////////////////
#PYGAME关闭实用程序
# ////////////////////////////////////////////////
时钟滴答(120)
pygame.display.flip()
pygame.quit()
如果未按下LEFT,则将right
和LEFT
设置为False
:
if键[pygame.K_左]:
# [...]
其他:
右=假
真=假
您必须使用if
-elif
-else
语句:
if键[pygame.K_RIGHT]:
右=真
左=假
# [...]
elif keys[pygame.K_左]:
左=真
右=假
# [...]
其他:
右=假
真=假
但是,您可以简化代码:
right=False
真=假
如果键[pygame.K_RIGHT]:
右=真
# [...]
elif keys[pygame.K_左]:
左=真
# [...]
其他:
# [...]
是的,就是这样!谢谢拉比!没有看左边的选项。@DOBS谢谢。不客气。