Pygame中跳跃

Pygame中跳跃

问题描述:

我想在pygame中制作一个像几何冲刺一样的游戏。除了跳跃部分之外,我已经完成了一切。我需要它,这样当角色在一个方块上时,他可以跳起来,但是不能在半空跳跃。现在我拥有它,这样角色就可以跳到地面上,但只要角色碰到一组块上的跳跃,他就会开始上下跳动,并且无法在滑块上跳跃。任何人都可以帮忙吗?Pygame中跳跃

onblock = False 
for i in squares_list: 
     if player_rect.bottom <= 560 and player_rect.colliderect(i): 
      onblock = True 
      player_rect.bottom = i.top + 1 
     if player_rect.collidepoint((i.topleft[0], i.topleft[1]+1)): 
      print ('Game Over') 
    if event.type == KEYDOWN:  # if space is pressed the character jumps 
      if event.key == K_SPACE: 
       print(onblock) 
       if onblock or player_rect.bottom == screen.get_rect().bottom : # prevents double jumps 
        vel_y = -20 # Makes the character jump up 
        player_rect.y -= 1 
    if onblock: 
     gravity = 0 
     vel_y = 0 
     current_angle = 0 
    else: 
     gravity = 1 
     vel_y += gravity 

    onblock = False 
+0

在跳跃时可能会将onblock设置为True?如'player_rect.y - = 1'后面的行 –

我不知道你是否缩进是错的只是在这个岗位还是在你的游戏,但根据这个帖子,onblock永远是每一个循环后假。这可能是问题所在。

另一个可能的问题可能是当碰撞发生时,您将角色放置在平台上方1个像素。这意味着下一次检查碰撞时,角色将不会停留在块上,因此onblock将为False。

第一个问题很容易通过删除最后一行onblocks = False来解决。第二个问题可以通过player_rect.bottom = i.top修正,不带+1。

看到这个talk欲了解更多信息,特别是约this时间戳。