Finished Chapter 13 source code programming

master
Jason Zhu 2020-09-12 16:30:48 +10:00
parent db5b9b9a0f
commit 3e8aa34427
6 changed files with 185 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -0,0 +1,36 @@
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
"""A class to represent a single alien in the fleet"""
def __init__(self, ai_game):
"""initialize the alien and set its starting position"""
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
# Load the alien image and set its rect attribute
self.image = pygame.image.load('alien.bmp')
self.rect = self.image.get_rect()
# Start each new alien near the top left of the screen
# Add a space to the left of alien (which equals to the alien's width)
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# Store the alien's exact horizontal position
self.x = float(self.rect.x)
def check_edges(self):
"""Return True if alien is at edge of screen"""
screen_rect = self.screen.get_rect()
if self.rect.right >= screen_rect.right or self.rect.left <= 0:
return True
else:
return False
def update(self):
"""Move the alien to the right"""
self.x += self.settings.alien_speed * self.settings.fleet_direction
self.rect.x = self.x # track the alien's exact position with self.x attribute.

View File

@ -1,8 +1,14 @@
import sys import sys
from time import sleep
import pygame import pygame
from settings import Settings from settings import Settings
from game_stats import GameStats
from ship import Ship from ship import Ship
from bullet import Bullet from bullet import Bullet
from alien import Alien
class AlienInvasion: class AlienInvasion:
"""Overall class to manage game assets and behaviour""" """Overall class to manage game assets and behaviour"""
@ -20,16 +26,25 @@ class AlienInvasion:
self.settings.screen_height = self.screen.get_rect().height self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion") pygame.display.set_caption("Alien Invasion")
# Create an instance to store game statistics
self.stats = GameStats(self)
self.ship = Ship(self) self.ship = Ship(self)
self.bullets = pygame.sprite.Group() # self.bullets = pygame.sprite.Group() #
self.aliens = pygame.sprite.Group()
self._create_fleet()
def run_game(self): def run_game(self):
"""Start the main loop for the game""" """Start the main loop for the game"""
while True: while True:
self._check_events() self._check_events()
if self.stats.game_active:
self.ship.update() self.ship.update()
self._update_bullets() self._update_bullets()
self._update_aliens()
self._update_screen() self._update_screen()
@ -77,6 +92,42 @@ class AlienInvasion:
if bullet.rect.bottom <= 0: if bullet.rect.bottom <= 0:
self.bullets.remove(bullet) self.bullets.remove(bullet)
print(len(self.bullets)) print(len(self.bullets))
self._check_bullet_alien_collisions()
def _check_bullet_alien_collisions(self):
"""Respond to the bullet-alien collisions"""
# Check for any bullets that have hit aliens.
# If so, get rid of the bullet and the alien
collisions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True)
# If alien fleet is destroyed, repopulate another one
if not self.aliens:
self.bullets.empty()
self._create_fleet()
def _check_aliens_bottom(self):
"""Check if any aliens have reached the bottom of screen"""
screen_rect = self.screen.get_rect()
for alien in self.aliens.sprites():
if alien.rect.bottom >= screen_rect.bottom:
# Treat this the same as if the ship got hit
self._ship_hit()
break
def _update_aliens(self):
"""
Check if the fleet is at an edge,
Update the positions of all aliens in the fleet
"""
self._check_fleet_edges()
self.aliens.update()
# Look for alien-ship collisions
if pygame.sprite.spritecollideany(self.ship, self.aliens):
self._ship_hit()
# Look for aliens hitting the bottom of the screen
self._check_aliens_bottom()
def _update_screen(self): def _update_screen(self):
"""Update images on the screen, and flip to the new screen""" """Update images on the screen, and flip to the new screen"""
@ -84,9 +135,75 @@ class AlienInvasion:
self.ship.blitme() self.ship.blitme()
for bullet in self.bullets.sprites(): for bullet in self.bullets.sprites():
bullet.draw_bullet() bullet.draw_bullet()
self.aliens.draw(self.screen)
pygame.display.flip() pygame.display.flip()
def _create_fleet(self):
"""
Create the fleet of aliens.
"""
# Create an alien and find the number of alines in a row
# Spacing between each alien is equal to one alien width.
alien = Alien(self)
alien_width, alien_height = alien.rect.size
availalbe_space_x = self.settings.screen_width - (2 * alien_width)
number_aliens_x = availalbe_space_x // (2 * alien_width)
# Determine the number of rows of aliens that fit on the screen
ship_height = self.ship.rect.height
availalbe_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height)
number_rows = availalbe_space_y // (2 * alien_height)
# Create the full fleet of aliens
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
# Create an alien and place it in the row
self._create_alien(alien_number, row_number)
def _create_alien(self, alien_number, row_number):
"""Create an alien and place it in the row in correct place"""
alien = Alien(self)
alien_width, alein_height = alien.rect.size
alien.x = alien_width + 2 * alien_width * alien_number
alien.rect.x = alien.x
alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
self.aliens.add(alien)
def _change_fleet_direction(self):
"""Drop the entire fleet and change the fleet's direction"""
for alien in self.aliens.sprites():
alien.rect.y += self.settings.fleet_drop_speed
self.settings.fleet_direction *= -1
def _check_fleet_edges(self):
"""Respond appropriately if any aliens have reached an edge"""
for alien in self.aliens.sprites():
if alien.check_edges():
self._change_fleet_direction()
break # Break out of the loop if an alien is at edge
def _ship_hit(self):
"""Respond to the ship being hit by an alien"""
if self.stats.ships_left > 0:
# Decrement ships_left
self.stats.ships_left -= 1
# Get rid of any remaining aliens and bullets
self.aliens.empty()
self.bullets.empty()
# Create a new fleet and center the ship
self._create_fleet()
self.ship.center_ship()
# Pause
sleep(0.5)
else:
self.stats.game_active = False
if __name__ == '__main__': if __name__ == '__main__':
ai = AlienInvasion() ai = AlienInvasion()
ai.run_game() ai.run_game()

View File

@ -0,0 +1,14 @@
class GameStats:
"""Track statistics for Alien Invasion"""
def __init__(self, ai_game):
"""Initialize statistics"""
self.settings = ai_game.settings
self.reset_stats()
# Start Alien Invasion in an active state
self.game_active = True
def reset_stats(self):
"""Initialize statics that can change during the game"""
self.ships_left = self.settings.ship_limit

View File

@ -11,10 +11,18 @@ class Settings:
# Ship settings # Ship settings
self.ship_speed = 1.5 self.ship_speed = 1.5
self.ship_limit = 3
# Bullet settings # Bullet settings
self.bullet_speed = 1.0 self.bullet_speed = 1.5
self.bullet_width = 3 self.bullet_width = 3
self.bullet_height = 15 self.bullet_height = 15
self.bullet_color = (60,60,60) self.bullet_color = (60,60,60)
self.bullets_allowed = 3 self.bullets_allowed = 3
# Alien settings
self.alien_speed = 1.0
self.fleet_drop_speed = 10
# fleet_direction of 1 represents right; -1 represent left
self.fleet_direction = 1

View File

@ -34,3 +34,8 @@ class Ship:
def blitme(self): def blitme(self):
"""Draw the ship at its current location""" """Draw the ship at its current location"""
self.screen.blit(self.image,self.rect) self.screen.blit(self.image,self.rect)
def center_ship(self):
"""Center the ship on the screen"""
self.rect.midbottom = self.screen_rect.midbottom
self.x = float(self.rect.x)