简体   繁体   English

为什么我的代码有时只注意到冲突?

[英]Why does my code only sometimes notice collision?

This game is a space shooter and I'm having a weird problem where my lasers only sometimes hit the enemy, and other times pass right through.这个游戏是一款太空射击游戏,我遇到了一个奇怪的问题,我的激光有时只会击中敌人,而其他时候会直接穿过。

for lasers in amtLasers:
    if lasers.y < invaders.y:
        if lasers.x > invaders.x-56 and lasers.x < invaders.x+28:
            amtLasers.pop(amtLasers.index(lasers))
            amtInvaders.pop(amtInvaders.index(invaders))
            score += 20

I have classes for the lasers and invaders and amtLasers is a list that stores the onscreen lasers until they get deleted.我有激光和入侵者的课程,amtLasers 是一个列表,用于存储屏幕上的激光,直到它们被删除。 I don't have it set to destroy the enemy on contact, so I was able to notice that if I continue shooting an enemy, the same enemy will get hit sometimes and not get hit other times.我没有设置在接触时摧毁敌人,所以我注意到如果我继续射击敌人,同一个敌人有时会被击中,而其他时候不会被击中。 Why does this happen and how can I fix it?为什么会发生这种情况,我该如何解决?

Here's an edit: The lasers only hit the most recently spawned enemy, and I think I know why这是一个编辑:激光只击中最近生成的敌人,我想我知道为什么

class invader(object):
    def __init__(self):
        self.x = randint(30, 1020)
        self.y = 0
        color = randint(1, 3)
        if color == 1:
            self.col = (225,0,0)
        elif color == 2:
            self.col = (0,225,0)
        elif color == 3:
            self.col = (0,115,255)
        self.vel = randint(1, 2)

When a new enemy is spawned, invaders.x and invaders.y track that specific enemy and none of the other ones because this class has multiple enemies onscreen at once.当一个新的敌人产生时,invenders.x 和invancers.y 会跟踪那个特定的敌人,而不会跟踪其他的,因为这个 class 会同时在屏幕上显示多个敌人。 How would I track each enemy separately?我将如何分别跟踪每个敌人?

You will need to test to see if any of the lasers hit any of the enemies.您将需要测试是否有任何激光击中任何敌人。 Use 2 nested loops to iterate the enemies and the lasers:使用 2 个嵌套循环来迭代敌人和激光:

for lasers in amtLasers[:]:
    for invaders in amtInvaders[:]:

        if lasers.y < invaders.y:
            if lasers.x > invaders.x-56 and lasers.x < invaders.x+28:
                amtLasers.pop(amtLasers.index(lasers))
                amtInvaders.pop(amtInvaders.index(invaders))
                score += 20

Note that you need to iterate over shallow copies of the list.请注意,您需要遍历列表的浅表副本。 See How to remove items from a list while iterating?请参阅如何在迭代时从列表中删除项目? . .

I recommend to use pygame.Rect objects and colliderect for the collision test:我建议使用pygame.Rect对象和colliderect进行碰撞测试:

laser_rect = pygame.Rect(laser.x, laser.y, laser_width, laser_height)
invader_rect = pygame.Rect(invaders.x, invaders.y, invaders_width, invaders_height)

if laser_rect .colliderect(invader_rect):
    amtLasers.pop(amtLasers.index(lasers))
    amtInvaders.pop(amtInvaders.index(invaders))
    score += 20

See How do I detect collision in pygame?请参阅如何检测 pygame 中的碰撞? and How to detect collisions between two rectangular objects or images in pygame .以及如何检测 pygame 中两个矩形对象或图像之间的碰撞

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM