Return to repo list

gatemender

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

commit 18b8609fd20ba4096a8df39eff1879e1b5ab9ce3
parent 2ba5d0b5d43a510a3cfeafaba2b81f49ba396810
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sun,  6 Dec 2020 16:23:43 -0600

Bring in the work I have done thus far

Diffstat:
A.gitignore | 10++++++++++
Adata/asteroid_big.png | 0
Adata/asteroid_medium.png | 0
Adata/asteroid_small.png | 0
Adata/bullet1.png | 0
Adata/ship1.png | 0
Adata/space_background.png | 0
Amain.py | 11+++++++++++
Asrc/game.py | 160+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 181 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,10 @@ +__pycache__/ +build +*.zip +*.swp + +# For those who use PyCharm +.idea/ + +# For venv +env diff --git a/data/asteroid_big.png b/data/asteroid_big.png Binary files differ. diff --git a/data/asteroid_medium.png b/data/asteroid_medium.png Binary files differ. diff --git a/data/asteroid_small.png b/data/asteroid_small.png Binary files differ. diff --git a/data/bullet1.png b/data/bullet1.png Binary files differ. diff --git a/data/ship1.png b/data/ship1.png Binary files differ. diff --git a/data/space_background.png b/data/space_background.png Binary files differ. diff --git a/main.py b/main.py @@ -0,0 +1,11 @@ +import pygame +from src import game + +pygame.init() + +def main(): + g1 = game.Game() + g1.run() + +if __name__ == "__main__": + main() diff --git a/src/game.py b/src/game.py @@ -0,0 +1,160 @@ +import pygame, os, math + +# Constants +SCREEN_DIM = (640, 480) +FRAMERATE = 60 +COLORKEY = (255, 0, 255) +SHIP_MAX_SPEED = 5 +BULLET_SPEED = 8 +DEGREE_CORRECTION = 90 +IMAGE_FILES = { + "Ship": os.path.join("data", "ship1.png"), + "Bullet" : os.path.join("data", "bullet1.png"), + "Small_Asteroid" : os.path.join("data", "asteroid_small.png"), + "Medium_Asteroid" : os.path.join("data", "asteroid_medium.png"), + "Big_Asteroid" : os.path.join("data", "asteroid_big.png"), + "Space_Background" : os.path.join("data", "space_background.png") +} +SHOOT_KEY = pygame.K_SPACE +THRUST_KEY = pygame.K_w + +class CustomSprite(pygame.sprite.Sprite): + def __init__(self, manager, image, pos, angle = 0): + + # Constructor + pygame.sprite.Sprite.__init__(self) + + # Values + self.manager = manager + self.image = image + self.image.set_colorkey(COLORKEY) + self.base_image = image + self.rect = self.image.get_rect() + self.rect.center = pos + self.position = pygame.Vector2(pos) + self.angle = angle + if angle != 0: + self.rotate(angle) + + def rotate(self, angle): + self.angle = angle + self.image = pygame.transform.rotate(self.base_image, angle).convert() + self.image.set_colorkey((255, 0, 255)) + self.rect = self.image.get_rect(center = self.rect.center) + + def direction_move(self, speed): + rad = math.radians(self.angle + DEGREE_CORRECTION) + self.position += pygame.Vector2(math.sin(rad), math.cos(rad)) * speed + self.rect.center = tuple(self.position) + + def act(self): + pass + + def update(self, surface = None): + self.act() + if surface != None: + surface.blit(self.image, self.rect) + +class Ship(CustomSprite): + + def __init__(self, manager, image, pos, angle = 0): + super().__init__(manager, image, pos, angle) + self.speed = 0 + self.thrust_factor = 0.01 + self.shoot_delay = 10 + self.shoot_cooldown = 0 + + # Bools + self.shooting = False + self.thrusting = False + + def thrust(self): + if self.speed < SHIP_MAX_SPEED: + self.speed += self.thrust_factor + + def decelerate(self): + self.speed -= 2 * self.thrust_factor + if self.speed < 0: + self.speed = 0 + + def act(self): + + #Thrusting + if self.thrusting: + self.thrust() + elif self.speed > 0: + self.decelerate() + 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 + + def shoot(self): + self.manager.drawables.add(Bullet(self.manager, self.manager.images["Bullet"], self.rect.center, self.angle)) + +class Bullet(CustomSprite): + + def act(self): + self.direction_move(BULLET_SPEED) + if not self.rect.colliderect(self.manager.screen_rect): + self.kill() + +class Game(object): + + def __init__(self): + self.on = True + self.screen = pygame.display.set_mode(SCREEN_DIM) + self.screen_rect = self.screen.get_rect() + self.frameclock = pygame.time.Clock() + + # Loading + self.images = { i : pygame.image.load(IMAGE_FILES[i]).convert() for i in IMAGE_FILES } + self.keys = [ False for k in range(0, 256) ] + + # Game objects + self.drawables = pygame.sprite.Group() + self.ship = Ship(self, self.images["Ship"], self.screen.get_rect().center) + self.drawables.add(self.ship) + + def shift_frames(self): + self.frameclock.tick(FRAMERATE) + + def handle_input(self): + + # Mouse handling + mx, my = pygame.mouse.get_pos() + self.ship.rotate(math.degrees(math.atan2(-(my - self.ship.rect.centery), mx - self.ship.rect.centerx))) + + # Keyboard handling + self.ship.shooting = self.keys[SHOOT_KEY] + self.ship.thrusting = self.keys[THRUST_KEY] + + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.on = False + elif event.type == pygame.KEYDOWN: + if event.key < len(self.keys): + self.keys[event.key] = True + elif event.type == pygame.KEYUP: + if event.key < len(self.keys): + self.keys[event.key] = False + + def update_screen(self): + self.screen.fill((0, 0, 0)) + self.drawables.update(self.screen) + pygame.display.update() + + def run(self): + while self.on: + self.shift_frames() + self.handle_events() + self.handle_input() + self.update_screen() + + pygame.quit()