1

Hi I would be grateful for some help with why the bullets are not being fired, i followed all of the steps in the book and it doesn't work.

the bullet.py file is newly written, so is the "#bullet settings" in settings.py, and some parts have been added to alien_invasion.py. and in alien_invasion.py The "for bullet in self.bullets.sprites(): bullet.draw_bullet". isnt drawing any bullets?

alien_invasion.py

import sys

import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet



class AlienInvasion:

    def __init__(self):
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        #self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        self.bg_color = (0, 0, 50)
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()


    def _check_keyup_events(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False


    def _fire_bullet(self):
        """ Create a new bullet and add it to the bullets group."""
        new_bullet = Bullet(self)
        self.bullets.add(new_bullet)


    def _check_keydown_events(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.rect2.x += 10
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_ESCAPE:
            sys.exit()
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()




     def _update_screen(self):
         """update images on the screen, and flip to the new screen"""

     def _check_events(self):
         for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 sys.exit()


             if event.type == pygame.KEYDOWN:
                 self._check_keydown_events(event)


             if event.type == pygame.KEYUP:
                 self._check_keyup_events(event)
            
             for bullet in self.bullets.sprites():
                 bullet.draw_bullet()




     def run_game(self):
         while True:

             self._check_events()
             self.ship.update()
             self.bullets.update()

             self._update_screen()
             for bullet in self.bullets.sprites():
                 bullet.draw_bullet()
             pygame.display.flip()

             self.screen.fill(self.bg_color)
             self.ship.blitme()
             self.ship.blitme2()








                    

            
        
            
    
    
        






if __name__ == '__main__':
     ai = AlienInvasion()
     ai.run_game()

settings.py

class Settings:
    """A class to store all settings for Alien Invasion."""

    def __init__(self):
        """Initialize the game's settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)

        #Ship settings
        self.ship_speed = 15

        #Bullet settings
        self.bullet_speed = 1.0
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = (60, 60, 60)
    
        

bullet.py

import pygame 
from pygame.sprite import Sprite

class Bullet(Sprite):
    """A class to manage blullets fired from the ship"""

    def __init__(self, ai_game):
        """Create a bullet object at the ships current position."""
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color



        # Create a bullet rect at (0, 0) and then the correct position.

        self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height)
        self.rect.midtop = ai_game.ship.rect.midtop

        # Store the bullets position as a decimal value.

        self.y = float(self.rect.y)

    def update(self):
        """ Move the bullet up the screen."""
        # Update the decimal position of the bullet.
        self.y -= self.settings.bullet_speed
        #Update the rect position.
        self.rect.y = self.y 

    def draw_bullet(self):
        """ Draw the bullet to the screen"""
        pygame.draw.rect(self.screen, self.color, self.rect)

ship.py


import pygame


class Ship:
    def __init__(self, ai_game):
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings = ai_game.settings

        self.image = pygame.image.load('C:/Users/Documents/Python Learning/python crash course lessons/Alien Invasion/ship.bmp')
        self.image2 = pygame.image.load('C:/Users/Documents/Python Learning/python crash course lessons/Alien Invasion/ship1.bmp')
        self.rect = self.image.get_rect()
        self.rect2 = self.image2.get_rect()
        self.rect.midbottom = self.screen_rect.midbottom
        self.moving_right = False
        self.moving_left = False
        self.x = float(self.rect.x)
        self.x2 = float(self.rect2.x)

    def update(self):
        """Update the ships position based on the movement flagg."""
        if self.moving_right == True:
            self.rect2.x += 5
            self.rect.x +=2
        if self.moving_right and self.rect2.right < self.screen_rect.right:
            self.x += self.settings.ship_speed
        if self.moving_left == True:
            self.rect2.x -= 5
            self.rect.x -= 2
        if self.moving_left and self.rect2.left > 0:
            self.x -= self.settings.ship_speed


        #Update rect object from self.x
        self.rect2.x = self.x
        #self.rect.x = self.x


    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def blitme2(self):
        self.screen.blit(self.image2, self.rect2.move(0, 670))
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
student789
  • 23
  • 2

1 Answers1

0

You have to draw the bullets in the _update_screen method:

class AlienInvasion:
    # [...]

    def _update_screen(self):
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()

You have to call _update_screen after clearing the screen and before updating the screen:

class AlienInvasion:
    # [...]

    def run_game(self):
         while True:

             self._check_events()
             self.ship.update()
             self.bullets.update()
  
             self.screen.fill(self.bg_color)
             self.ship.blitme()
             self.ship.blitme2()
             self._update_screen()
             pygame.display.flip()

The start position of the bullets is wrong. Change the position in Bullet.__init__:

self.rect.midtop = ai_game.ship.rect.midtop

class Bullet(Sprite):
    def __init__(self, ai_game):
        # [...]

        self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height)
        self.rect.midtop = ai_game.ship.rect2.move(0, 670).midtop
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • Hi thanks for getting back to me, I've made the changes you said and the bullets still are not firing. – student789 Jun 04 '21 at 08:04
  • @student789 No that is not true. The code works fine. However, the starting position of the bullet is wrong, it starts at the bottom of the screen. The bullet is hard to see, since its color is dark gray and the background is dark blue. Change the color of the bullet for debugging (`self.bullet_color = (255, 60, 60)`) – Rabbid76 Jun 04 '21 at 08:23
  • I changed the color of the bullet and I still cant see anything unfortunately, I also changed the background color, im not sure how to change the starting position, I tried to change the coordinates in the self.rect variable in bullet.py, still nothings changed – student789 Jun 04 '21 at 09:11
  • @student789 It works fine for me. You did something wrong copying the code. – Rabbid76 Jun 04 '21 at 09:12