Return to repo list

gatemender

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

commit c73bfac2e26b72c8ce0190882a26f2f41e9f73f9
parent a418086b44281bf041b83cf68977287596aa9e20
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sun,  6 Dec 2020 17:41:12 -0600

Added asteroids

Diffstat:
Mdata/space_background.png | 0
Msrc/constants.py | 2+-
Msrc/game.py | 22+++++++++++++++++++++-
Msrc/sprites.py | 26+++++++++++++++++++++++++-
4 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/data/space_background.png b/data/space_background.png Binary files differ. diff --git a/src/constants.py b/src/constants.py @@ -1,6 +1,6 @@ import pygame, os -SCREEN_DIM = (640, 480) +SCREEN_DIM = (1024, 768) FRAMERATE = 60 COLORKEY = (255, 0, 255) SHIP_MAX_SPEED = 5 diff --git a/src/game.py b/src/game.py @@ -1,4 +1,4 @@ -import pygame, os +import pygame, os, random from . import sprites from .constants import * @@ -19,6 +19,10 @@ class Game(object): self.ship = sprites.Ship(self, self.images["Ship"], self.screen.get_rect().center) self.drawables.add(self.ship) + # Others + self.level_factor = 1 + self.asteroid_spawn_timer = 100 + def shift_frames(self): self.frameclock.tick(FRAMERATE) @@ -48,11 +52,27 @@ class Game(object): self.drawables.update(self.screen) pygame.display.update() + def spawn_asteroids(self): + if self.asteroid_spawn_timer <= 0: + size = random.randint(0, 2) + if size == 0: + im = self.images["Small_Asteroid"] + elif size == 1: + im = self.images["Medium_Asteroid"] + else: + im = self.images["Big_Asteroid"] + spawnpos = (random.randint(0, SCREEN_DIM[1]), -65) + self.drawables.add(sprites.Asteroid(self, im, spawnpos, 270, size)) + self.asteroid_spawn_timer = 10 * (10 / self.level_factor) + else: + self.asteroid_spawn_timer -= 1 + def run(self): while self.on: self.shift_frames() self.handle_events() self.handle_input() + self.spawn_asteroids() self.update_screen() pygame.quit() diff --git a/src/sprites.py b/src/sprites.py @@ -1,4 +1,4 @@ -import pygame, math +import pygame, math, random from .constants import * class CustomSprite(pygame.sprite.Sprite): @@ -25,6 +25,9 @@ class CustomSprite(pygame.sprite.Sprite): self.image.set_colorkey((255, 0, 255)) self.rect = self.image.get_rect(center = self.rect.center) + def offset_move(self, offset): + self.rect.center = (self.rect.center[0] + offset[0], self.rect.center[1] + offset[1]) + def direction_move(self, speed): rad = math.radians(self.angle + DEGREE_CORRECTION) self.position += pygame.Vector2(math.sin(rad), math.cos(rad)) * speed @@ -101,3 +104,24 @@ class Bullet(CustomSprite): if not self.rect.colliderect(self.manager.screen_rect): self.kill() +# Gate components +class GatePiece(CustomSprite): + + def __init__(self, manager, image, pos, angle = 0): + super().__init__(manager, image, pos, angle) + self.requirement = None + self.needed = 0 + self.halo = None + +# Asteroids +class Asteroid(CustomSprite): + + def __init__(self, manager, image, pos, angle = 0, size = 0): + super().__init__(manager, image, pos, angle) + self.size = size # 0 = small; 1 = medium; 2 = large + self.spin_speed = random.randint(-7, 7) + self.speed = random.randint(1, manager.level_factor + 1) + + def act(self): + self.offset_move((0, self.speed)) + self.rotate(self.angle + self.spin_speed)