Here's the output of the game.
I am taking reference of the ebook "Python Crash Course", I use Pycharm IDE and have also installed python and pygame in it, the game worked fine till the left right movement of the ship. Also I have another problem about loading the alien image in the game I have saved the image in the PYGAME directory inside images folder as an bitmap image as the pygame accept BMP images easily. After running the program the alien ship doesn't seems to appear in the game. I have also added the code for ship(not alien ship). Please do help me about the firing bullets from the ship. I have used five classes till now like ship.py, bullet.py, alien.py, settings.py, game functions.py
Here is the code for firing bullets class
import pygame as pg
from pygame.sprite import Sprite
class Bullet(Sprite):
def __init__(self, ai_settings, screen, ship):
super(Bullet, self).__init__()
self.screen = screen
self.rect = pg.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
self.y -= self.speed_factor
self.rect.y = self.y
def draw_bullet(self):
pg.draw.rect(self.screen, self.color, self.rect)
Here is the code of alien class
import pygame as pg
from pygame.sprite import Sprite
class Alien(Sprite):
def __init__(self, ai_settings, screen):
super(Alien, self).__init__()
self.screen = screen
self.ai_settings = ai_settings
self.image = pg.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
self.rect.x = self.rect.width
self.rect.y = self.rect.height
self.x = float(self.rect.x)
def blitme(self):
self.screen.blit(self.image, self.rect)
Here is the ship.py class code.
import pygame
class Ship:
def __init__(self, ai_settings, screen):
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
self.center = float(self.rect.centerx)
self.moving_right = False
self.moving_left = False
def update(self):
if self.moving_right:
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
self.rect.centerx += 1
if self.moving_left:
if self.moving_left and self.rect.left > 0:
self.center -= self.ai_settings.ship_speed_factor
self.rect.centerx -= 1
self.rect.centerx = self.center
def blitme(self):
self.screen.blit(self.image, self.rect)
Here is the class implementation in the main program Alien Invasion.py
import pygame as pg
from pygame.sprite import Group
import game_functions as gf
from alien import Alien
from settings import Settings
from ship import Ship
def run_game():
pg.init()
ai_settings = Settings()
screen = pg.display.set_mode((ai_settings.scree_width, ai_settings.screen_height))
pg.display.set_caption('Alien Invasion')
ship = Ship(ai_settings, screen)
bullets = Group()
alien = Alien(ai_settings, screen)
while True:
gf.check_events(ai_settings, screen, ship, bullets)
ship.update()
gf.update_bullets(bullets)
gf.update_screen(ai_settings, screen, ship, alien, bullets)
run_game()