0

I am trying to follow the book to complete this exercise, however there is something that I am missing out. I am now building the bullet class in order for the ship to fire bullets upwards, however once I fire the bullets, they won't move up from their original position. I have done some changes but still either the program crashes or remains as it is..any clue of what am I doing wrong?

I have seen a few similar questions but none refer exactly to firing the bullets and having them freeze on the screen.

Here you can see my code:

Main file

import sys

import pygame

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

class AlienInvasion:
    """overall class to initialize the game and its behavior"""
    
    def __init__(self):
        """initialize the game"""
        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
        pygame.display.set_caption("Space Invaders")
        
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        
    def run_game(self):
        """Start the main loop of the game"""
        while True:
            self.check_events()
            self.ship.update()
            self.bullets.update()
            self.update_screen()
            
    def check_events(self):
        """Respond to keypresses and mouse events"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self.check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self.check_keyup_events(event)
                     
    def check_keydown_events(self, event):
        """Check keydown events"""
        if event.key == pygame.K_RIGHT:
            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_SPACE:
            self.fire_bullet()
                
    def check_keyup_events(self, event):
        """Check key up events"""
        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):
        """Crreate a new bullet and add it to the group"""
        new_bullet = Bullet(self)
        self.bullets.add(new_bullet)
        
    def update_bullets(self):
        """Update position of bullets and get rid of old bullets."""
        # Update bullet positions.
        self.bullets.update()
                         
    def update_screen(self):
        """Update the screen after each key event"""
        ##Redraw the screen during each pass through the loop        
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        # Make the most recently drawn screen visible
        pygame.display.flip()
        
if __name__ == "__main__":
    # Make a game instance, and run the game
    ai = AlienInvasion()
    ai.run_game()
        
    

Bullet module

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    """A class to manage bullets fired from the ship
    """
    def __init__(self, ai_game):
        """"Bullet class"""
        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 set 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 bullet position
        self.y = float(self.rect.y)
    
    def bullet(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):
        pygame.draw.rect(self.screen, self.color, self.rect)

Ship module

import pygame

class Ship:
    """Defining the regular ship model"""
    
    def __init__(self, ai_game):
        """Initialize the ship"""
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings = ai_game.settings
        
        # Load the ship image and get its rect
        self.image = pygame.image.load("ship.bmp")
        self.rect = self.image.get_rect()
        
        #Start each new ship at the bottom center of the screen
        self.rect.midbottom = self.screen_rect.midbottom
        
        #Store a decimal value for the ship's horizontal position
        self.x = float(self.rect.x)
        
        # Movement flag
        self.moving_right = False
        self.moving_left = False
        
    def update(self):
        """Update the ship position"""
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.ship_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.ship_speed
    
        #Update rect object from self.x
        self.rect.x = self.x
        
    def blitme(self):
        """Draw the ship at its current location"""
        self.screen.blit(self.image, self.rect)

Settings module

class Settings:
    """Overall game settings"""
    
    def __init__(self):
        """Initialize the games' settings"""
        # Screen settings
        self.screen_width = 800
        self.screen_height= 600
        self.bg_color = (252, 208, 231)
        
        # Ship settings
        self.ship_speed = 1.5
        
        # Bullet settings
        self.bullet_speed = 1
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = (60, 60, 60)
bavver23
  • 1
  • 1
  • StackOverflow is not a debugging service. Please consider [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – Rabbid76 Feb 17 '23 at 15:54

0 Answers0