1 year ago

#386445

test-img

zacfifi

Having trouble with lasers not spawning in pygame

I am having a lot of issues with this game project mainly with the lasers spawning the affected code is below

class Projectile:
        def __init__(self, x, y, img):
                self.x = x
                self.y = y
                self.img = img
                self.mask = pygame.mask.from_surface(self.img)

        def draw(self, window):
                window.blit(self.img, (self.x, self.y))

        def move(self, vel):
                self.y += vel

        def off_screen(self,height):
                return self.y <= height and self.y >= 0

        def collision(self, obj):
                return collide(obj, self)

This is the projectile class it looks fine to me but the lasers are not spawning when the player hits space

The code below is from a separate class for the main ships which all other ships derive from

    def draw(self, WINDOW):
        WINDOW.blit(self.player_img, (self.x, self.y))
        for laser in self.lasers:
            laser.draw(WINDOW)

    def move_lasers(self, vel, obj):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(HEIGHT):
                self.lasers.remove(laser)
            elif laser.collision(obj):
                obj.health -= 10
                self.lasers.remove(laser)

    def cooldown(self):
        if self.cool_down_counter >= self.CD:
            self.cool_down_counter = 0
        elif self.cool_down_counter > 0:
            self.cool_down_counter += 1

    def shoot(self):
        if self.cool_down_counter == 0:
            laser = Projectile(self.x, self.y, self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter = 1

Below the code calls upon the player to shoot

       keys = pygame.key.get_pressed()
       
        if keys[pygame.K_LEFT] and player.x - player_vel > 0:
            player.x -= player_vel
        if keys[pygame.K_RIGHT] and player.x + player_vel + player.get_width() < WIDTH:
            player.x += player_vel
        if keys[pygame.K_UP] and player.y - player_vel > 0:
            player.y -= player_vel
        if keys[pygame.K_DOWN] and player.y + player_vel + player.get_height() < HEIGHT:
            player.y += player_vel
        if keys[pygame.K_SPACE]:
            player.shoot()
        if keys[pygame.K_LSHIFT]:
            boost = True
            start = time.time()
            

Any help would be greatly appreciated

python

pygame

0 Answers

Your Answer

Accepted video resources