Return to repo list

gatemender

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

commit 6cdb2c88fb5612b428f1ccb2dbefa2e5cc69bd57
parent a7c16af9fa4b6dc64bc10976be905ed4d4f0a358
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Mon, 14 Dec 2020 12:10:35 -0600

Redid controls to be more configurable

Diffstat:
Msrc/constants.py | 28++++++++++++++++++++--------
Msrc/game.py | 20++++++++++----------
2 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/src/constants.py b/src/constants.py @@ -1,13 +1,25 @@ import pygame, os, enum, json -# KEYS -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 -REPAIR_KEY = pygame.K_e -BURST_KEY = pygame.K_k +# Controls +CONTROLS = { + "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, + "REPAIR_KEY" : pygame.K_e, + "BURST_KEY" : pygame.K_k +} +# Example alternative config from glw@linuxmail.org from here: https://leagueh.xyz/mail/hyperkitty/list/librejam@leagueh.xyz/message/64Y5OV2BSFRSTZTEORBDI4ZMEYDQ6346/ +#CONTROLS = { +# "TURN_LEFT_KEY" : pygame.K_LEFT, +# "TURN_RIGHT_KEY" : pygame.K_RIGHT, +# "BRAKE_KEY" : pygame.K_DOWN, +# "SHOOT_KEY" : pygame.K_SPACE, +# "THRUST_KEY" : pygame.K_UP, +# "REPAIR_KEY" : pygame.K_RETURN, +# "BURST_KEY" : pygame.K_k +#} # HIGHSCORE with open("hs.json") as j: HIGHSCORES = json.load(j) diff --git a/src/game.py b/src/game.py @@ -19,7 +19,7 @@ class Game(object): # 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) ] + self.keys = { CONTROLS[k] : False for k in CONTROLS } self.sounds = { s : pygame.mixer.Sound(SOUND_FILES[s]) for s in SOUND_FILES } self.channels = [pygame.mixer.Channel(x) for x in range(0, pygame.mixer.get_num_channels() - 1)] @@ -217,23 +217,23 @@ class Game(object): if event.type == pygame.QUIT: self.on = False elif event.type == pygame.KEYDOWN: - if event.key < len(self.keys): + if event.key in self.keys.keys(): self.keys[event.key] = True elif event.type == pygame.KEYUP: - if event.key < len(self.keys): + if event.key in self.keys.keys(): self.keys[event.key] = False def handle_input(self): 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] - self.ship.bursting = self.keys[BURST_KEY] + self.ship.turning_left = self.keys[CONTROLS["TURN_LEFT_KEY"]] + self.ship.turning_right = self.keys[CONTROLS["TURN_RIGHT_KEY"]] + self.ship.braking = self.keys[CONTROLS["BRAKE_KEY"]] + self.ship.shooting = self.keys[CONTROLS["SHOOT_KEY"]] + self.ship.thrusting = self.keys[CONTROLS["THRUST_KEY"]] + self.ship.fixing = self.keys[CONTROLS["REPAIR_KEY"]] + self.ship.bursting = self.keys[CONTROLS["BURST_KEY"]] elif self.mode == MODES.Menu: if pygame.mouse.get_pressed()[0]: mousepos = pygame.mouse.get_pos()