according to the armed spaceship written in the book, it is written that it can move left and right, but when it comes to shooting (loading), it is copied according to the code in the book, and the running time does not show that there is a problem with the code, but it just presses the space bar and there are no bullets. Everything else is normal.
Code:
alien_invasion.py
import sys
import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
-sharppygame
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
-sharp
ship = Ship(ai_settings,screen)
-sharp
bullets = Group()
-sharp
while True:
gf.check_events(ai_settings,screen,ship,bullets)
ship.update()
bullets.update()
gf.update_screen(ai_settings,screen,ship,bullets)
run_game()
**game_functions.py**
import sys
import pygame
from bullet import Bullet
def check_keydown_events(event,ai_settings,screen,ship,bullets):
""""""
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_SPACE:
-sharpbullets
new_bullet = Bullet(ai_settings,screen,ship)
bullets.add(new_bullet)
def check_keyup_events(event,ship):
""""""
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def check_events(ai_settings,screen,ship,bullets):
""""""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event,ai_settings,screen,ship,bullets)
elif event.type == pygame.KEYUP:
check_keyup_events(event,ship)
def update_screen(ai_settings,screen,ship,bullets):
""""""
-sharp
screen.fill(ai_settings.bg_color)
ship.blitme()
-sharp
pygame.display.flip()
-sharp
for bullet in bullets.sprites():
bullet.draw_bullet()
**bullet.py**
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
""""""
def __init__(self,ai_settings,screen,ship):
""""""
super(Bullet,self). __init__()
self.screen = screen
-sharp(0,0)
self.rect = pygame.Rect(0,0,ai_settings.bullet_width,
ai_settings.bullet_height)
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
-sharp
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
""""""
-sharp
self.y -= self.speed_factor
-sharprect
self.rect.y = self.y
def draw_bullet(self):
""""""
pygame.draw.rect(self.screen,self.color,self.rect)
settings.py
class Settings():
""""""
def __init__(self):
""""""
-sharp
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
-sharp
self.ship_speed_factor = 1.5
-sharp
self.bullet_speed_factor = 1
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = 60,60,60
ship.py
import pygame
class Ship():
def __init__(self,ai_settings,screen):
""""""
self.screen = screen
self.ai_settings = ai_settings
-sharp
self.image = pygame.image.load("images/ship.bmp")
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
-sharp
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
-sharpcenter
self.center = float(self.rect.centerx)
-sharp
self.moving_right = False
self.moving_left = False
def update(self):
""""""
-sharpcenterrect
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
if self.moving_left and self.rect.left > 0:
self.center -= self.ai_settings.ship_speed_factor
-sharpself.centerrect
self.rect.centerx = self.center
def blitme(self):
""""""
self.screen.blit(self.image,self.rect)