0

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

zacfifi
  • 3
  • 3
  • first you could use `print()` to see which part of code is executed and what you have in variables. It is called `"print debuging"` and it helps to see what code is doing. – furas Apr 07 '22 at 10:24
  • when player press space then it create `Laser()`, not `Projectile()` - so you should show also class `Laser` – furas Apr 07 '22 at 10:26
  • `Laser` is created in `shoot()` only when `self.cool_down_counter == 0:` - so first you could use `print()` to see if `self.cool_down_counter` can be `0`. Maybe `self.cool_down_counter` never is `0` and it can't create `Laser()` – furas Apr 07 '22 at 10:29
  • frankly, we can't run this code so you will have to debug it on your own. – furas Apr 07 '22 at 10:30
  • This was very useful information i've been screening the code with the print and its very helpful to see which parts are not working – zacfifi Apr 08 '22 at 08:47
  • `Projectile.off_screen` is actually returning whether the laser is on the screen or not. As it returns `True` when it shouldn't, the laser is immediately deleted when it is spawned. You should therefore change `return self.y <= height and self.y >= 0` into `return not (self.y <= height and self.y >= 0)` – The_spider Apr 08 '22 at 12:12
  • The_spider thank you so much that instantly fixed it. – zacfifi Apr 21 '22 at 08:28

0 Answers0