Space 在 Python 中与 pygame 的 Invaders 游戏的屏障盾牌碰撞检测
Barrier shield collision detection for Space Invaders game in Python with pygame
我目前正在 Python 和 pygame 制作 Space Invaders 游戏。
我目前有 3 个基本障碍,它们都是用 1 x 1 像素块构建的('extend' pygame.sprite.Sprite class
)。对于碰撞检测,我检查导弹是否与障碍物发生碰撞。
现在,一切正常,当我开火并击中其中一个障碍物时,被击中的像素将被消除。
现在困扰我的是,在最初的 Space Invaders 中,当飞船的导弹(或外星人的导弹)
碰到障碍物,它会导致 'explosion' 影响障碍物的多个像素。我想实现这个
在我的 python/pygame 代码中。我该怎么做?
这是我的导弹 class 中的碰撞检测代码(即 'extends' pygame.sprite.Sprite):
baseBarrier_hit_list = pygame.sprite.spritecollide(self, all_baseBarrier_group, True)
for pixel in baseBarrier_hit_list:
self.kill() # I kill the missile after collision so that the other barrier pixels are unaffected.
break #so that the other pixels in the column are unaffected.
我想 'artificially' 向 baseBarrier_hit_list 添加 'random' 像素,但我无法向 baseBarrier_hit_list 添加元素。
这是原始 space 入侵者的视频 link 以了解我的意思:
https://www.youtube.com/watch?v=axlx3o0codc
这里还有一个 link 到 python/pygame 版本的 Space Invaders,显示当导弹和基地屏障发生碰撞时只有一个像素受到影响。 (请注意,这不是我的游戏版本)。 https://www.youtube.com/watch?v=_2yUP3WMDRc
编辑:(图片说明见评论)
编辑:TemporalWolf 的建议奏效了。这是我添加到函数中的代码。
shield_hit_list_random = pygame.sprite.spritecollide(self, all_blockShields_group, False, pygame.sprite.collide_rect_ratio(2))
将 False 传递给函数可防止精灵被杀死。然后,我随机杀了shield_hit_list_random
组的一些精灵,像这样:
for shield in shield_hit_list_random:
pourcentage =randint(0,3)
if pourcentage == 2:
shield.kill()
我会尝试使用 scaled collision rect 或等效的圆圈:
pygame.sprite.collide_rect_ratio() Collision detection between two
sprites, using rects scaled to a ratio.
collide_rect_ratio(ratio) -> collided_callable
A callable class that checks for collisions between
two sprites, using a scaled version of the sprites rects.
Is created with a ratio, the instance is then intended to be passed as
a collided callback function to the *collide functions.
A ratio is a floating point number - 1.0 is the same size, 2.0 is
twice as big, and 0.5 is half the size.
New in pygame 1.8.1
这将作为您的 spritecollide
函数的第 4 个参数
我目前正在 Python 和 pygame 制作 Space Invaders 游戏。
我目前有 3 个基本障碍,它们都是用 1 x 1 像素块构建的('extend' pygame.sprite.Sprite class
)。对于碰撞检测,我检查导弹是否与障碍物发生碰撞。
现在,一切正常,当我开火并击中其中一个障碍物时,被击中的像素将被消除。
现在困扰我的是,在最初的 Space Invaders 中,当飞船的导弹(或外星人的导弹) 碰到障碍物,它会导致 'explosion' 影响障碍物的多个像素。我想实现这个 在我的 python/pygame 代码中。我该怎么做?
这是我的导弹 class 中的碰撞检测代码(即 'extends' pygame.sprite.Sprite):
baseBarrier_hit_list = pygame.sprite.spritecollide(self, all_baseBarrier_group, True)
for pixel in baseBarrier_hit_list:
self.kill() # I kill the missile after collision so that the other barrier pixels are unaffected.
break #so that the other pixels in the column are unaffected.
我想 'artificially' 向 baseBarrier_hit_list 添加 'random' 像素,但我无法向 baseBarrier_hit_list 添加元素。
这是原始 space 入侵者的视频 link 以了解我的意思: https://www.youtube.com/watch?v=axlx3o0codc
这里还有一个 link 到 python/pygame 版本的 Space Invaders,显示当导弹和基地屏障发生碰撞时只有一个像素受到影响。 (请注意,这不是我的游戏版本)。 https://www.youtube.com/watch?v=_2yUP3WMDRc
编辑:(图片说明见评论)
编辑:TemporalWolf 的建议奏效了。这是我添加到函数中的代码。
shield_hit_list_random = pygame.sprite.spritecollide(self, all_blockShields_group, False, pygame.sprite.collide_rect_ratio(2))
将 False 传递给函数可防止精灵被杀死。然后,我随机杀了shield_hit_list_random
组的一些精灵,像这样:
for shield in shield_hit_list_random:
pourcentage =randint(0,3)
if pourcentage == 2:
shield.kill()
我会尝试使用 scaled collision rect 或等效的圆圈:
pygame.sprite.collide_rect_ratio() Collision detection between two sprites, using rects scaled to a ratio.
collide_rect_ratio(ratio) -> collided_callable
A callable class that checks for collisions between two sprites, using a scaled version of the sprites rects.
Is created with a ratio, the instance is then intended to be passed as a collided callback function to the *collide functions.
A ratio is a floating point number - 1.0 is the same size, 2.0 is twice as big, and 0.5 is half the size.
New in pygame 1.8.1
这将作为您的 spritecollide
函数的第 4 个参数