From c8ebcc71564fc38d86a01c973e3b667af0a637b0 Mon Sep 17 00:00:00 2001 From: Chris Williams Date: Thu, 18 Apr 2024 22:38:07 -0400 Subject: [PATCH] up to pg 259 --- alien.py | 22 ++++++++++++++++++++++ alien_invasion.py | 37 ++++++++++++++++++++++++++++++++++++- bullet.py | 2 +- images/alien.bmp | Bin 0 -> 10494 bytes settings.py | 1 + 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 alien.py create mode 100644 images/alien.bmp diff --git a/alien.py b/alien.py new file mode 100644 index 0000000..a6d4f0e --- /dev/null +++ b/alien.py @@ -0,0 +1,22 @@ +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): + """Init the alien & set it's starting position""" + super().__init__() + self.screen = ai_game.screen + self.screen = ai_game.screen + + # Load the alien image and set it's rect attrib + self.image = pygame.image.load("images/alien.bmp") + self.rect = self.image.get_rect() + + # Start each new alien near the top left of screen + 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) diff --git a/alien_invasion.py b/alien_invasion.py index 01f88e7..9eba583 100644 --- a/alien_invasion.py +++ b/alien_invasion.py @@ -2,6 +2,8 @@ import pygame from settings import Settings from ship import Ship +from bullet import Bullet +from alien import Alien class AlienInvasion: @@ -19,6 +21,10 @@ def __init__(self) -> None: pygame.display.set_caption("Alien Invasion") self.ship = Ship(self) + self.bullets = pygame.sprite.Group() + self.aliens = pygame.sprite.Group() + + self._create_fleet() # Set the background color. self.bg_color = (230, 230, 230) @@ -28,6 +34,7 @@ def run_game(self): while True: self._check_events() self.ship.update() + self._update_bullets() self._update_screen() self.clock.tick(60) @@ -51,6 +58,8 @@ def _check_keydown_events(self, event): self.ship.moving_left = True elif event.key == pygame.K_q: sys.exit() + elif event.key == pygame.K_SPACE: + self._fire_bullet() def _check_keyup_events(self, event): """Respond to key releases""" @@ -59,14 +68,40 @@ def _check_keyup_events(self, event): elif event.key == pygame.K_LEFT: self.ship.moving_left = False + def _fire_bullet(self): + """Create a new bullet and add it to the bullets group""" + if len(self.bullets) < self.settings.bullets_allowed: + new_bullet = Bullet(self) + self.bullets.add(new_bullet) + + def _update_bullets(self): + """Update position of bullets & delete old bullets.""" + # Update bullet positions. + self.bullets.update() + + # Delete bullets at top of screen + for bullet in self.bullets.copy(): + if bullet.rect.bottom <= 0: + self.bullets.remove(bullet) + # print(len(self.bullets)) + + def _create_fleet(self): + """Create the fleet of aliens""" + # Make an alien + alien = Alien(self) + self.aliens.add(alien) + + def _update_screen(self): """Update images on the screen, and flip to the new screen.""" self.screen.fill(self.settings.bg_color) + for bullet in self.bullets.sprites(): + bullet.draw_bullet() self.ship.blitme() + self.aliens.draw(self.screen) pygame.display.flip() - if __name__ == "__main__": # Make a game instance & run the game. ai = AlienInvasion() diff --git a/bullet.py b/bullet.py index 70965ce..69335b3 100644 --- a/bullet.py +++ b/bullet.py @@ -1,5 +1,5 @@ import pygame -from pygame.sprite import _Group, Sprite +from pygame.sprite import Sprite class Bullet(Sprite): """A class to manage bullets fired from the ship""" diff --git a/images/alien.bmp b/images/alien.bmp new file mode 100644 index 0000000000000000000000000000000000000000..dbe01a05de0f6f904459b92fd2d99193aea0bbad GIT binary patch literal 10494 zcmdU!y-piJ5P+Qui3kc(kVi-hMFdd_paP*(JPSRdMNlJX;RUb|FF=tpT|$xqTi!fI zln*8ujb``u?)J{cL1$}jc6R35+1Ve9oln2t4W9qr^Zg`#@EJbF22U zKx_zYDi9RUI#eY>fzP-L&T&P_#Z7HBr{PrbY)Vzx7cPnVpi?<_?pVHVMc{o94>JZ-1xEPMl+=iMvO0pRuJD2Ch1m^upH#ow7W07wn60 z;i430AO^;>q$tj_1SNW$!@LGO2Q04z*h%18W(??Vg|!bnHE%$3s#EGxGI0HC)NXI%S=H`tuRT3T_d zgRLqa7ad4Y?Y7_y3SRLEd_!P2d(~zq~={}L&S@V%CGW2ZCV=_avrM@ zIR=&@aXVEPRA!v%E~}y#y9(IVb`xOPC2qpa)@aM_6|zgfygk~sdbPwL&ZN^Ej1QK- zFQdedvSox}7&;I7N;nCPkY57YB^Kyt{lo^M!7Kx5o05BogdlIloEb3Ok_SS>Hnmj& zx!ZFfB9e^BRKMS!3^&vds6=R)EjLIgg#t47PM^NLxi> zfkh_giX;LWPKV4%3i)-oW`4C}awSR`W0qNfcwj{K%q1b}Rs?6@iVYip?YKA>s)moN z+i)vjbhN#Y=}40`a3uQbI`XPb4FyP?6LOyEt7&wpKAao^oarb{B*cZ&B-Hk`25v5D zonUYu?(txw$1`X{SebAb^TbtZ&88vWg}X_Z2(}xhodO7_5xv%@X2$J%@_XlKa%4TY zA3?KEroh-5aMdnNIu^I0?RIU)H3Qk*leKiU2G}PBgBj5Qs@V((lOY_o*eD*tOVw?X zR+SV0spAnmO|p<@F8u1@Wd&OUn(g9ZVAJN3%oUounY}HmtW8{riw90t71auE)2sCl z17KArZ?d~|NnSgX_M&vPZJCi9ViUAW(wJyGEIlk`IJF5R2N}QCV(*XlbMyn z=q1pmnk+RWjLq`l`f4S~654J@$-OZqbQ(MGzVMQMSScF13wHA}E{V}rX@A1lTJ0X@GI;3) L9{J~)(n;|LYGj&< literal 0 HcmV?d00001 diff --git a/settings.py b/settings.py index 541560b..bc8f07e 100644 --- a/settings.py +++ b/settings.py @@ -16,3 +16,4 @@ def __init__(self) -> None: self.bullet_width = 3 self.bullet_height = 15 self.bullet_color = (60, 60, 60) + self.bullets_allowed = 5