【发布时间】: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 != 0和vspeed != 0吗?现在你只在vspeed为零时检查hspeed。
标签: python pygame game-physics