【问题标题】:pygame - Enemy flies off screen when collision with ground?pygame - 与地面碰撞时敌人飞出屏幕?
【发布时间】:2016-01-13 16:03:10
【问题描述】:

第一次在这里发布海报。

我会尽量保持简单。我正在尝试在 pygame 中制作游戏,但似乎我的碰撞检测正在发挥作用。我有一个玩家类,可以检测玩家是否与地面或环境列表中的任何其他对象发生碰撞,并且我有一个与同一列表中的任何对象发生碰撞的敌人类。敌人会计算出他们需要朝哪个方向行进,以便尝试击中玩家。重力很可能在问题中起主要作用。

问题在于,当“苍蝇”被放入并掉到地上时,它们会立即跳出屏幕,即使它们的 X 和 Y 值(根据屏幕上的项目日志)似乎根本没有移动?

作为澄清,两个“苍蝇”被放置在辅助列表中以允许碰撞检测。另外,作为旁注,如果没有左右碰撞检测,则不会发生“故障”......感谢任何可以提供帮助的人:)

def collisions():

#Detection Working as intended
    for fly in Fly.List:
        fly_proj = pygame.sprite.spritecollide(fly, Projectile.List, True)
        if len(fly_proj) > 0:
            for hit in fly_proj:
                fly.health -= 100

        X = pygame.sprite.spritecollide(fly, current_level.object_list, False)
        if len(X) == 0: #Gravity here:
            if (fly.vspeed == 0):
                fly.vspeed = 1
                print("Gravity")
            else:
                fly.vspeed += 0.35
        if len(X) > 1: 
            for hit in X:

                if fly.vspeed > 0:            
                    fly.rect.bottom = hit.rect.top +1
                    fly.vspeed = 0
                elif fly.vspeed < 0:    
                    fly.rect.top = hit.rect.bottom -1
                elif fly.hspeed > 0:
                    fly.rect.right = hit.rect.left
                    fly.hspeed = 0
                elif fly.hspeed < 0:            
                    fly.rect.left = hit.rect.right
                    fly.hspeed = 0

        print(len(X),framecounter, fly.vspeed, fly.hspeed)
#Kill fly if health reaches 0            
        if fly.health <= 0:
            fly.destroy(Fly)

 #Detect where the player is
        if window_rect.contains(fly.rect):
            fly.playersnapshot = player.rect.x
        if fly.rect.x - player.rect.x >= -10:
            #print("LEFTING")
            fly.hspeed = -2
        if fly.rect.x - player.rect.x <= 10:
            fly.hspeed = 2
        fly.truefalse = True
        event = None

        fly.rect.y += fly.vspeed
        fly.rect.x += fly.hspeed

【问题讨论】:

  • 可以同时拥有hspeed != 0vspeed != 0吗?现在你只在vspeed 为零时检查hspeed

标签: python pygame game-physics


【解决方案1】:

我认为您的if/else 不正确。

可能当苍蝇接触地面时,您将vspeed 设置为零,然后if/else 检查hspeed,然后它使用地面left/right 更改苍蝇left/right

我知道一种方法。

  1. 垂直移动飞
  2. 检查碰撞并使用if/elsevspeed
  3. 水平移动飞
  4. 检查碰撞并将if/elsehspeed一起使用

--

编辑:

 # move horizontally only

 fly.rect.x += fly.hspeed

 X = pygame.sprite.spritecollide( ... )

 for hit in X:
     if fly.hspeed > 0:
         fly.rect.right = hit.rect.left
     else:
         fly.rect.left = hit.rect.right

 # move vertically only

 fly.rect.y += fly.vspeed

 X = pygame.sprite.spritecollide( ... )

 for hit in X:
     if fly.vspeed > 0:
         fly.rect.bottom = hit.rect.top
     else:
         fly.rect.top = hit.rect.bottom

     # on_ground = True

我在ProgramArcadeGames.com的“Platform Jumper”源代码中找到了这个方法

查看页面:platform_jumper.py

【讨论】:

  • 谢谢,很快就会试试这个并更新你的结果:)
  • 请参阅页面源代码中的update()programarcadegames.com/python_examples/… - 它使用重力。
  • 已将其排序@furas :) 检查我的帖子以获取更新的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
相关资源
最近更新 更多