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. 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. How would I track each enemy separately?