“NoneType”没有属性“colliderect”

2024-04-29 23:14:31 发布

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

我的第一个问题和Python的乞丐

我想做一个简单的射击游戏(鸡入侵者类型)。长方形(敌人)和较小的矩形(弹药)的列表,在碰撞时应该“杀死”敌人。我在屏幕上的任意位置创建了矩形列表。在

enemies = []
for i in range(ENEMYCOUNT):
        enemySize = random.randint(ENEMYMINSIZE, ENEMYMAXSIZE)
        newEnemy = pygame.Rect(random.randint(0, WINDOWWIDTH - enemySize),
            random.randint(0, WINDOWWIDTH-200), enemySize, enemySize)
        enemies.append(newEnemy)

我的“弹药”是一个不可见的矩形列表,这些矩形在鼠标单击时会出现,然后它们会向上移动

^{pr2}$

我是为单一的敌人准备的。 现在我想制造更多的敌人,检查是否有敌人被弹药击中

for e in enemies:
        for a in ammo:
            if e.colliderect(a):
                enemies.remove(e)

但我总是

如果e.colliderect(a): AttributeError:“NoneType”对象没有属性“collidect”

我正在学习《inventwithpython》一书,复制了我需要的部分代码,但我无法解决这个问题。我看不出问题,因为我的两个列表都是由Rect对象组成的。 任何帮助都将不胜感激


Tags: 对象inrect列表forrandomrandint矩形
1条回答
网友
1楼 · 发布于 2024-04-29 23:14:31

{you can be remove in which Problem from the Problem.}因为你可以使用^问题列表中的语句。在

解决方案:用未删除的元素创建新列表,然后将其分配给enemies

但是Pygame有类Group来保留{}和

pygame.sprite.spritecollide(sprite, group, dokill, collided = None)

其中:

Return a list containing all Sprites in a Group that intersect with another Sprite. Intersection is determined by comparing the Sprite.rect attribute of each Sprite.

The dokill argument is a bool. If set to True, all Sprites that collide will be removed from the Group.

相关问题 更多 >