基于pygam的pygam中的空间入侵者屏障-盾牌碰撞检测游戏

2024-04-30 02:09:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用pygame制作Python中的太空入侵者游戏。在

我目前有3个基本屏障,都是用1×1像素块构建的(这“扩展”了pygame.sprite.Sprite class)。对于碰撞检测,我检查是否有导弹与障碍物相撞。 现在,一切正常,当我发射并击中其中一个障碍物时,被击中的像素就会消失。在

现在让我困扰的是,在最初的太空入侵者中,当飞船的导弹(或者说外星人的导弹)时 碰到障碍物,它会引起“爆炸”,影响屏障的多个像素。我想实施这个 在我的python/pygame代码中。我该怎么做?在

这是我在导弹类中的碰撞检测代码(它“扩展”了游戏精灵精灵精灵)公司名称:

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.  

我想“人为地”将“随机”像素添加到baseBarrier_hit_列表中,但是我无法向baseBarrier_hit_列表中添加元素。在

以下是原始太空入侵者的视频链接,看看我的意思: https://www.youtube.com/watch?v=axlx3o0codc

这里还有一个指向python/pygame版本的空间入侵者的链接,显示当导弹和基地屏障发生碰撞时,只有一个像素受到影响。(注意这不是我的游戏版本)。https://www.youtube.com/watch?v=_2yUP3WMDRc

编辑:(图片说明见注释) enter image description here

编辑:临时沃尔夫的建议奏效了。这是我添加到函数中的代码。在

^{pr2}$

向函数传递一个False可以防止精灵被杀死。然后,我随机杀死shield_hit_list_random组中的一些精灵,如下所示:

for shield in shield_hit_list_random:
    pourcentage =randint(0,3)
    if pourcentage == 2:
        shield.kill()

Tags: the代码游戏像素太空pygamelist精灵
1条回答
网友
1楼 · 发布于 2024-04-30 02:09:23

我会尝试使用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函数的第四个参数

相关问题 更多 >