From de85e5cbeae6467cec96fe5d3079155f3b2008b3 Mon Sep 17 00:00:00 2001 From: PAlexanderFranklin Date: Wed, 17 May 2023 00:05:33 -0700 Subject: [PATCH] Add individual movement --- monster-masher-clone.py | 63 +++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/monster-masher-clone.py b/monster-masher-clone.py index ae7ce16..0948e46 100644 --- a/monster-masher-clone.py +++ b/monster-masher-clone.py @@ -19,16 +19,60 @@ screen = pygame.display.set_mode((screen_width,screen_height)) pygame.display.set_caption('monster-masher-clone') class Player: - def __init__(self, x, y, color): + def __init__(self, x, y, color, keys): self.x = x self.y = y self.color = color + self.keys = keys self.sprite = pygame.Rect((tile*x)+2,(tile*y)+2,tile-2,tile-2) + def move(self, x, y): + self.x += x + self.y += y + + def keyPress(self, key): + try: + command = self.keys[key] + if command == "n": + self.move(0,-1) + if command == "ne": + self.move(1,-1) + if command == "e": + self.move(1,0) + if command == "se": + self.move(1,1) + if command == "s": + self.move(0,1) + if command == "sw": + self.move(-1,1) + if command == "w": + self.move(-1,0) + if command == "nw": + self.move(-1,-1) + finally: + return True + players = [] -players.append(Player(25, 20, red)) -players.append(Player(20, 15, blue)) -players.append(Player(30, 15, green)) +players.append(Player(25, 20, red, { + pygame.K_w: "n", + pygame.K_e: "ne", + pygame.K_d: "e", + pygame.K_c: "se", + pygame.K_x: "s", + pygame.K_z: "sw", + pygame.K_a: "w", + pygame.K_q: "nw" +})) +players.append(Player(25, 20, green, { + pygame.K_KP_8: "n", + pygame.K_KP_9: "ne", + pygame.K_KP_6: "e", + pygame.K_KP_3: "se", + pygame.K_KP_2: "s", + pygame.K_KP_1: "sw", + pygame.K_KP_4: "w", + pygame.K_KP_7: "nw" +})) game_font = pygame.font.Font("freesansbold.ttf",32) @@ -42,16 +86,11 @@ while True: sys.exit() if event.type == pygame.KEYDOWN: + print(event) for player in players: - if event.key == pygame.K_s: - player.y += 1 - if event.key == pygame.K_w: - player.y -= 1 - if event.key == pygame.K_a: - player.x -= 1 - if event.key == pygame.K_d: - player.x += 1 + player.keyPress(event.key) + keys = pygame.key.get_pressed() # Rendering screen.fill(bg_color) for player in players: