Return to repo list

gatemender

Arcade game about fixing warp gates. LibreJam Dec 2020 Entry.
Return to HMagellan.com

commit 63097b9279e965c46c5df04d2e4c13333b8a370a
parent f0dc85bfcd3026d55a2cc0f3042055299ef3af28
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Mon,  7 Dec 2020 09:30:00 -0600

level transition and game over

Diffstat:
Msrc/game.py | 74+++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/sprites.py | 149++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
2 files changed, 140 insertions(+), 83 deletions(-)

diff --git a/src/game.py b/src/game.py @@ -27,19 +27,29 @@ class Game(object): # Others self.level_factor = 1 + self.lives = 0 + self.transition_timer_max = 200 + self.transition_timer = self.transition_timer_max self.asteroid_spawn_timer = 100 self.gate_defs = [] + self.game_over_text = self.font.render("GAE OVER", False, (255, 255, 255)) + self.lives_text = self.font.render(str(self.lives), False, (255, 255, 255)) + self.level_text = self.font.render("GATE " + str(self.level_factor), False, (255, 255, 255)) self.change_mode(MODES.Play) - def load_level(self): - - # First, restore to default + def clear_level(self): self.screen_draw_group = pygame.sprite.Group() self.camera_draw_group = pygame.sprite.Group() self.asteroids = pygame.sprite.Group() self.bullets = pygame.sprite.Group() + self.ship = None self.asteroid_spawn_timer = 100 + + def load_level(self): + + # First, restore to default + self.clear_level() self.gate_defs = [ [ bool(random.getrandbits(1)), MATERIALS(random.randint(1, 3)), 0, (416, 200) ], [ bool(random.getrandbits(1)), MATERIALS(random.randint(1, 3)), 0, (480, 200) ], @@ -90,6 +100,12 @@ class Game(object): self.mode = mode if mode == MODES.Play: self.load_level() + elif mode == MODES.Transition: + self.lives_text = self.font.render(str(self.lives), False, (255, 255, 255)) + self.level_text = self.font.render("GATE " + str(self.level_factor), False, (255, 255, 255)) + self.transition_timer = self.transition_timer_max + elif mode == MODES.GameOver: + self.transition_timer = self.transition_timer_max def shift_frames(self): self.frameclock.tick(FRAMERATE) @@ -107,23 +123,47 @@ class Game(object): def handle_input(self): - # Keyboard handling - self.ship.turning_left = self.keys[TURN_LEFT_KEY] - self.ship.turning_right = self.keys[TURN_RIGHT_KEY] - self.ship.braking = self.keys[BRAKE_KEY] - self.ship.shooting = self.keys[SHOOT_KEY] - self.ship.thrusting = self.keys[THRUST_KEY] - self.ship.fixing = self.keys[REPAIR_KEY] + if self.mode == MODES.Play: + if self.ship != None and not self.ship.dead: + self.ship.turning_left = self.keys[TURN_LEFT_KEY] + self.ship.turning_right = self.keys[TURN_RIGHT_KEY] + self.ship.braking = self.keys[BRAKE_KEY] + self.ship.shooting = self.keys[SHOOT_KEY] + self.ship.thrusting = self.keys[THRUST_KEY] + self.ship.fixing = self.keys[REPAIR_KEY] def update_screen(self): - self.screen.fill((44, 44, 44)) - self.camera_surface.fill((0, 0, 0)) - self.camera_surface.blit(self.images["Space_Background"], (0, 0)) - self.camera_draw_group.update(self.camera_surface) - self.screen.blit(self.camera_surface, self.camera_surface_rect) - self.screen_draw_group.update(self.screen) + if self.mode == MODES.Play: + self.screen.fill((44, 44, 44)) + self.camera_surface.fill((0, 0, 0)) + self.camera_surface.blit(self.images["Space_Background"], (0, 0)) + self.camera_draw_group.update(self.camera_surface) + self.screen.blit(self.camera_surface, self.camera_surface_rect) + self.screen_draw_group.update(self.screen) + elif self.mode == MODES.Transition: + self.screen.fill((0, 0, 0)) + self.screen.blit(self.lives_text, (100, 100)) + self.screen.blit(self.level_text, (100, 200)) + elif self.mode == MODES.GameOver: + self.screen.fill((0, 0, 0)) + self.screen.blit(self.game_over_text, (100, 100)) pygame.display.update() + def update_logic(self): + if self.mode == MODES.Play: + self.spawn_asteroids() + if self.mode == MODES.Transition: + if self.transition_timer > 0: + self.transition_timer -= 1 + else: + self.change_mode(MODES.Play) + elif self.mode == MODES.GameOver: + if self.transition_timer > 0: + self.transition_timer -= 1 + else: + #self.change_mode(MODES.Play) + self.on = False + def spawn_asteroids(self): if self.asteroid_spawn_timer <= 0: size = random.randint(0, 2) @@ -146,7 +186,7 @@ class Game(object): self.shift_frames() self.handle_events() self.handle_input() - self.spawn_asteroids() + self.update_logic() self.update_screen() pygame.quit() diff --git a/src/sprites.py b/src/sprites.py @@ -55,19 +55,21 @@ class Ship(CustomSprite): self.shooting = False self.thrusting = False self.braking = False - self.turnining_left = False + self.turning_left = False self.turning_right = False self.fixing = False + self.blinking = False + self.dead = False # Other self.hp = 3 self.hitbox = pygame.Rect((0, 0), (self.rect.width // 4, self.rect.height // 4)) - self.blinking = False self.blinking_timer_max = 120 self.blinking_timer = self.blinking_timer_max self.aura = FixAura(self.manager, self.manager.images["Fix_Aura"], self.rect.center, 0, self) self.manager.camera_draw_group.add(self.aura) self.materials = { m : 0 for m in MATERIALS } + self.be_dead_timer = 60 def thrust(self): if self.speed < SHIP_MAX_SPEED: @@ -88,74 +90,89 @@ class Ship(CustomSprite): def act(self): - # Syncing - self.hitbox.center = self.rect.center - - # Turning - if self.turning_left: - self.turn(3) - if self.turning_right: - self.turn(-3) - - #Thrusting / Braking / Slowing - if self.thrusting: - self.thrust() - elif self.braking: - self.decelerate(5 * self.thrust_factor) - elif self.speed > 0: - self.decelerate(2 * self.thrust_factor) - self.direction_move(self.speed) - - #Shooting - if self.shooting: - if self.shoot_cooldown <= 0: - self.shoot() - self.shoot_cooldown += self.shoot_delay + # If alive + if not self.dead: + # Syncing + self.hitbox.center = self.rect.center + + # Turning + if self.turning_left: + self.turn(3) + if self.turning_right: + self.turn(-3) + + #Thrusting / Braking / Slowing + if self.thrusting: + self.thrust() + elif self.braking: + self.decelerate(5 * self.thrust_factor) + elif self.speed > 0: + self.decelerate(2 * self.thrust_factor) + self.direction_move(self.speed) + + #Shooting + if self.shooting: + if self.shoot_cooldown <= 0: + self.shoot() + self.shoot_cooldown += self.shoot_delay + else: + self.shoot_cooldown -= 1 + + # Fixing + if self.fixing: + self.aura.visible = True else: - self.shoot_cooldown -= 1 + self.aura.visible = False + + # Check for wrapping + if not self.rect.colliderect(self.manager.camera_surface_rect): + x = self.rect.centerx + y = self.rect.centery + if self.rect.centerx > self.manager.camera_surface_rect.width: + x = 0 - (self.rect.width / 2) + elif self.rect.centerx < 0: + x = self.manager.camera_surface_rect.width + (self.rect.width / 2) + if self.rect.centery > self.manager.camera_surface_rect.height: + y = 0 - (self.rect.height / 2) + elif self.rect.centery < 0: + y = self.manager.camera_surface_rect.height + (self.rect.height / 2) + self.rect.center = (x, y) + self.position = pygame.Vector2(self.rect.center) + + + # Damage blink and asteroid col + if self.blinking: + if (self.blinking_timer // 10) % 2 != 0: + self.visible = False + else: + self.visible = True + if self.blinking_timer > 0: + self.blinking_timer -= 1 + else: + self.blinking = False + self.blinking_timer = self.blinking_timer_max - # Fixing - if self.fixing: - self.aura.visible = True - else: - self.aura.visible = False - - # Check for wrapping - if not self.rect.colliderect(self.manager.camera_surface_rect): - x = self.rect.centerx - y = self.rect.centery - if self.rect.centerx > self.manager.camera_surface_rect.width: - x = 0 - (self.rect.width / 2) - elif self.rect.centerx < 0: - x = self.manager.camera_surface_rect.width + (self.rect.width / 2) - if self.rect.centery > self.manager.camera_surface_rect.height: - y = 0 - (self.rect.height / 2) - elif self.rect.centery < 0: - y = self.manager.camera_surface_rect.height + (self.rect.height / 2) - self.rect.center = (x, y) - self.position = pygame.Vector2(self.rect.center) - - - # Damage blink and asteroid col - if self.blinking: - if (self.blinking_timer // 10) % 2 != 0: - self.visible = False else: - self.visible = True - if self.blinking_timer > 0: - self.blinking_timer -= 1 - else: - self.blinking = False - self.blinking_timer = self.blinking_timer_max + for s in self.manager.asteroids.sprites(): + if self.hitbox.colliderect(s.rect): + s.destroy() + self.hp -= 1 + if self.hp > 0: + self.blinking = True + else: + self.dead = True + + # If dead else: - for s in self.manager.asteroids.sprites(): - if self.hitbox.colliderect(s.rect): - s.destroy() - self.hp -= 1 - if self.hp > 0: - self.blinking = True - else: - self.manager.on = False #TMP, obv + self.visible = False + if self.be_dead_timer > 0: + self.be_dead_timer -= 1 + else: + self.manager.lives -= 1 + if self.manager.lives >= 0: + self.manager.change_mode(MODES.Transition) + else: + self.manager.change_mode(MODES.GameOver) # Bullet class class Bullet(CustomSprite):