From 1fa264cc2f26b68f199bf8f4bf8abfa2b3c05863 Mon Sep 17 00:00:00 2001 From: 206530235 <1828358204@qq.com> Date: Thu, 24 Jun 2021 22:08:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alien.bmp | Bin 0 -> 2365 bytes alien.py | 41 +++++++++++++++++++++++++++++++++++ alien_invasion.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ bullet.py | 34 +++++++++++++++++++++++++++++ button.py | 34 +++++++++++++++++++++++++++++ 5 files changed, 163 insertions(+) create mode 100644 alien.bmp create mode 100644 alien.py create mode 100644 alien_invasion.py create mode 100644 bullet.py create mode 100644 button.py diff --git a/alien.bmp b/alien.bmp new file mode 100644 index 0000000000000000000000000000000000000000..e91ef54fd55ad2738d00e5624159de4e82cb1654 GIT binary patch literal 2365 zcmb`GdojoyQ4#b0rdG@Odrk8R|kM^`VaB{=8_Hzk0(P(AD~T-gD!`f4TD%Y@&_l0IrIm!#2mMS=m_PU zg%}t4HxB(9C(>i+P|jU(J~<{%%tDAa)2IybU1EtlmJN^g_k(O8v{!<7Km@M90pkCw zmfz?85&$;L0)WU__JtM#P=6Qzb-}XF@GJld=>XLA#gO7i-;nMM!9N>aKD+!a{==-g(3zFcoco~_38)|(=mIwo4amR@A`4&+2*6r21RMcULJ}z{ zfka9oQ7EJoS`m$wmPRYfE66IUDXXiiDXXe#Xc_5fXd0|kRn;}oHCSbgUyWa(V`^@S zGdIHFapEE{6bglwLSxZrEKWmJ1NT2mbPHglfFH0!!1Mte14Ce7qIPHi0Fyw#evS`W zB#=Z&!4MK~DC(pLU(p&qXD(WzR5dS*M`d$Qz=wSdH4&5%^KLmpVgak%O4@*$i zKWHEl09kPugTR0-fafz{M&@i@M_~wKhSu1|Ad>@zWUaO(%US0IYzJD`%qId$-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|j#U)Y#ZH6kwa`&tJ?mAelMRf}0{Y=ZK%3Eg9*q`W#C6D0EuAS>Qh(OuK V$)(qGQ`~a0X7|`Ls8Xe9@Za>JWBvdD literal 0 HcmV?d00001 diff --git a/alien.py b/alien.py new file mode 100644 index 0000000..865b4b8 --- /dev/null +++ b/alien.py @@ -0,0 +1,41 @@ +import pygame +from pygame.sprite import Sprite + + +class Alien(Sprite): + """A class to represent a single alien in the fleet.""" + + def __init__(self, ai_settings, screen): + """Initialize the alien, and set its starting position.""" + super(Alien, self).__init__() + self.screen = screen + self.ai_settings = ai_settings + + # Load the alien image, and set its rect attribute. + self.image = pygame.image.load(r'alien.bmp') + self.rect = self.image.get_rect() + + # Start each new alien near the top left of the screen. + self.rect.x = self.rect.width + self.rect.y = self.rect.height + + # Store the alien's exact 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: + return True + elif self.rect.left <= 0: + return True + + def update(self): + """Move the alien right or left.""" + self.x += (self.ai_settings.alien_speed_factor * + self.ai_settings.fleet_direction) + self.rect.x = self.x + + def blitme(self): + """Draw the alien at its current location.""" + self.screen.blit(self.image, self.rect) \ No newline at end of file diff --git a/alien_invasion.py b/alien_invasion.py new file mode 100644 index 0000000..a6e69bc --- /dev/null +++ b/alien_invasion.py @@ -0,0 +1,54 @@ +import pygame +from pygame.sprite import Group + +from settings import Settings +from game_stats import GameStats +from scoreboard import Scoreboard +from button import Button +from ship import Ship +import game_functions as gf + + +def run_game(): + # Initialize pygame, settings, and screen object. + pygame.init() + ai_settings = Settings() + screen = pygame.display.set_mode( + (ai_settings.screen_width, ai_settings.screen_height)) + pygame.display.set_caption("Alien Invasion") + + # Make the Play button. + play_button = Button(ai_settings, screen, "Play") + + # Create an instance to store game statistics, and a scoreboard. + stats = GameStats(ai_settings) + sb = Scoreboard(ai_settings, screen, stats) + + # Set the background color. + bg_color = (230, 230, 230) + + # Make a ship, a group of bullets, and a group of aliens. + ship = Ship(ai_settings, screen) + bullets = Group() + aliens = Group() + + # Create the fleet of aliens. + gf.create_fleet(ai_settings, screen, ship, aliens) + + # Start the main loop for the game. + while True: + gf.check_events(ai_settings, screen, stats, sb, play_button, ship, + aliens, bullets) + + if stats.game_active: + ship.update() + gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, + bullets) + gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, + bullets) + + gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, + bullets, play_button) + + +run_game() \ No newline at end of file diff --git a/bullet.py b/bullet.py new file mode 100644 index 0000000..c26ab2c --- /dev/null +++ b/bullet.py @@ -0,0 +1,34 @@ +import pygame +from pygame.sprite import Sprite + + +class Bullet(Sprite): + """A class to manage bullets fired from the ship.""" + + def __init__(self, ai_settings, screen, ship): + """Create a bullet object, at the ship's current position.""" + super(Bullet, self).__init__() + self.screen = screen + + # Create bullet rect at (0, 0), then set correct position. + 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 + + # Store a decimal value for the bullet's position. + self.y = float(self.rect.y) + + self.color = ai_settings.bullet_color + self.speed_factor = ai_settings.bullet_speed_factor + + def update(self): + """Move the bullet up the screen.""" + # Update the decimal position of the bullet. + self.y -= self.speed_factor + # Update the rect position. + self.rect.y = self.y + + def draw_bullet(self): + """Draw the bullet to the screen.""" + pygame.draw.rect(self.screen, self.color, self.rect) \ No newline at end of file diff --git a/button.py b/button.py new file mode 100644 index 0000000..8250882 --- /dev/null +++ b/button.py @@ -0,0 +1,34 @@ +import pygame.font + + +class Button(): + + def __init__(self, ai_settings, screen, msg): + """Initialize button attributes.""" + self.screen = screen + self.screen_rect = screen.get_rect() + + # Set the dimensions and properties of the button. + self.width, self.height = 200, 50 + self.button_color = (0, 255, 0) + self.text_color = (255, 255, 255) + self.font = pygame.font.SysFont(None, 48) + + # Build the button's rect object, and center it. + self.rect = pygame.Rect(0, 0, self.width, self.height) + self.rect.center = self.screen_rect.center + + # The button message only needs to be prepped once. + self.prep_msg(msg) + + def prep_msg(self, msg): + """Turn msg into a rendered image, and center text on the button.""" + self.msg_image = self.font.render(msg, True, self.text_color, + self.button_color) + self.msg_image_rect = self.msg_image.get_rect() + self.msg_image_rect.center = self.rect.center + + def draw_button(self): + # Draw blank button, then draw message. + self.screen.fill(self.button_color, self.rect) + self.screen.blit(self.msg_image, self.msg_image_rect) \ No newline at end of file