Return to repo list

gatemender

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

commit a418086b44281bf041b83cf68977287596aa9e20
parent 9abae80cee0bfaad77e9691d629cba4eef692598
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sun,  6 Dec 2020 17:15:14 -0600

split into multiple files

Diffstat:
Asrc/constants.py | 22++++++++++++++++++++++
Msrc/game.py | 133+++----------------------------------------------------------------------------
Asrc/sprites.py | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 130 insertions(+), 128 deletions(-)

diff --git a/src/constants.py b/src/constants.py @@ -0,0 +1,22 @@ +import pygame, os + +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"), + "Asteroid_Particle" : os.path.join("data", "asteroid_particle.png"), + "Space_Background" : os.path.join("data", "space_background.png") +} +TURN_LEFT_KEY = pygame.K_a +TURN_RIGHT_KEY = pygame.K_d +BRAKE_KEY = pygame.K_s +SHOOT_KEY = pygame.K_SPACE +THRUST_KEY = pygame.K_w diff --git a/src/game.py b/src/game.py @@ -1,126 +1,6 @@ -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"), - "Asteroid_Particle" : os.path.join("data", "asteroid_particle.png"), - "Space_Background" : os.path.join("data", "space_background.png") -} -TURN_LEFT_KEY = pygame.K_a -TURN_RIGHT_KEY = pygame.K_d -BRAKE_KEY = pygame.K_s -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 - self.braking = False - self.turnining_left = False - self.turning_right = False - - def thrust(self): - if self.speed < SHIP_MAX_SPEED: - self.speed += self.thrust_factor - - def decelerate(self, amount = 1): - self.speed -= amount - if self.speed < 0: - self.speed = 0 - - def turn(self, amount): - self.rotate(self.angle + amount) - - def act(self): - - # Turning - if self.turning_left: - self.turn(2) - if self.turning_right: - self.turn(-2) - - #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 - - 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() +import pygame, os +from . import sprites +from .constants import * class Game(object): @@ -136,7 +16,7 @@ class Game(object): # Game objects self.drawables = pygame.sprite.Group() - self.ship = Ship(self, self.images["Ship"], self.screen.get_rect().center) + self.ship = sprites.Ship(self, self.images["Ship"], self.screen.get_rect().center) self.drawables.add(self.ship) def shift_frames(self): @@ -144,10 +24,6 @@ class Game(object): 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.turning_left = self.keys[TURN_LEFT_KEY] self.ship.turning_right = self.keys[TURN_RIGHT_KEY] @@ -168,6 +44,7 @@ class Game(object): def update_screen(self): self.screen.fill((0, 0, 0)) + self.screen.blit(self.images["Space_Background"], (0, 0)) self.drawables.update(self.screen) pygame.display.update() diff --git a/src/sprites.py b/src/sprites.py @@ -0,0 +1,103 @@ +import pygame, math +from .constants import * + +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 + self.braking = False + self.turnining_left = False + self.turning_right = False + + def thrust(self): + if self.speed < SHIP_MAX_SPEED: + self.speed += self.thrust_factor + + def decelerate(self, amount = 1): + self.speed -= amount + if self.speed < 0: + self.speed = 0 + + def turn(self, amount): + self.rotate(self.angle + amount) + + def shoot(self): + self.manager.drawables.add(Bullet(self.manager, self.manager.images["Bullet"], self.rect.center, self.angle)) + + def act(self): + + # Turning + if self.turning_left: + self.turn(2) + if self.turning_right: + self.turn(-2) + + #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 + +class Bullet(CustomSprite): + + def act(self): + self.direction_move(BULLET_SPEED) + if not self.rect.colliderect(self.manager.screen_rect): + self.kill() +