|
|||
ship.py. settings.pyship. py
import pygame from pygame. sprite import Sprite
class Ship(Sprite):
def __init__(self, ai_settings, screen): " " " Initialize the ship, and set its starting position. " " " super(Ship, self). __init__() self. screen = screen self. ai_settings = ai_settings
# Load the ship image, and get its rect. self. image = pygame. image. load('images/shooter. bmp') self. rect = self. image. get_rect() self. screen_rect = screen. get_rect()
# Start each new ship at the bottom center of the screen. self. rect. centerx = self. screen_rect. centerx self. rect. bottom = self. screen_rect. bottom
# Store a decimal value for the ship's center. self. center = float(self. rect. centerx)
# Movement flags. self. moving_right = False self. moving_left = False
def center_ship(self): " " " Center the ship on the screen. " " " self. center = self. screen_rect. centerx
def update(self): " " " Update the ship's position, based on movement flags. " " " # Update the ship's center value, not the rect. 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
# Update rect object from self. center. self. rect. centerx = self. center
def blitme(self): " " " Draw the ship at its current location. " " " self. screen. blit(self. image, self. rect)
*************************************************************************** settings. py
class Settings(): " " " A class to store all settings for Alien Invasion. " " "
def __init__(self): " " " Initialize the game's static settings. " " " # Screen settings. self. screen_width = 1423 #1200 self. screen_height = 800 self. bg_color = (230, 230, 230)
# Ship settings. self. ship_limit = 3
# Bullet settings. self. bullet_width = 3 self. bullet_height = 15 self. bullet_color = 60, 60, 60 self. bullets_allowed = 3
# Alien settings. self. fleet_drop_speed = 10
# How quickly the game speeds up. self. speedup_scale = 1. 1 # How quickly the alien point values increase. self. score_scale = 1. 5
self. initialize_dynamic_settings()
def initialize_dynamic_settings(self): " " " Initialize settings that change throughout the game. " " " self. ship_speed_factor = 1. 5 self. bullet_speed_factor = 3 self. alien_speed_factor = 1
# Scoring. self. alien_points = 50
# fleet_direction of 1 represents right, -1 represents left. self. fleet_direction = 1
def increase_speed(self): " " " Increase speed settings and alien point values. " " " self. ship_speed_factor *= self. speedup_scale self. bullet_speed_factor *= self. speedup_scale self. alien_speed_factor *= self. speedup_scale
self. alien_points = int(self. alien_points * self. score_scale)
*****************************************************************
|
|||
|