Question:
Can you fix this code so the enemies shoot at me?
Current behavior:
The enemies go down the screen and won’t shoot.
Desired behavior
I want the enemies to shoot at me.
Repl link:
https://replit.com/@StevenBrixie/space-shooter-2
code snippet
Try to modify the laser class to accept a direction:
class Laser:
def __init__(self, x, y, img, direction=1):
self.x = x
self.y = y
self.img = img
self.direction = direction
self.mask = pygame.mask.from_surface(self.img)
def move(self, vel):
self.y += vel * self.direction
And try to modify the shoot
method of the Enemy
class to adjust the starting position of the laser and set the direction to downwards:
def shoot(self):
if self.cool_down_counter == 0:
laser = Laser(self.x + self.get_width()//2 - self.laser_img.get_width()//2, self.y + self.get_height(), self.laser_img, direction=1)
self.lasers.append(laser)
self.cool_down_counter = 1
1 Like
that didn’t work. any other suggestions?
Hmmm.
I think that the shoot
method of the Ship
class does not consider the direction of the laser.
Try to modify the shoot
method of the Ship
class to accept a direction:
def shoot(self, direction=1):
if self.cool_down_counter == 0:
laser = Laser(self.x, self.y, self.laser_img, direction)
self.lasers.append(laser)
self.cool_down_counter = 1
And since we are changing one we need to modify the shoot
method of the Enemy
class to pass the correct direction:
def shoot(self):
if self.cool_down_counter == 0:
laser = Laser(self.x + self.get_width()//2 - self.laser_img.get_width()//2, self.y + self.get_height(), self.laser_img, direction=1)
self.lasers.append(laser)
self.cool_down_counter = 1
And don’t forget to modify the part where the player shoots to pass the correct direction:
if keys[pygame.K_SPACE]:
player.shoot(direction=-1)
Try this and tell me if it works
1 Like
thank you! it sorta’ worked.
1 Like
If the problem is solved please mark the answer that help you most as the “Solution” so other people may be aware
system
Closed
7
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.