From 8d29c7078f4316f88f1458614ce8be6395d7454d Mon Sep 17 00:00:00 2001 From: Jakub Danilewicz Date: Sun, 14 Mar 2021 11:32:44 +0100 Subject: [PATCH] Added simple movement with usage of arrow keys. Those functions have been declared in Minefield class (temporarily) --- event_interpreter.py | 13 +++++++++---- main.py | 16 ++++++++++++++-- minefield.py | 19 ++++++++++++++++++- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/event_interpreter.py b/event_interpreter.py index b272cf9..8eea54f 100644 --- a/event_interpreter.py +++ b/event_interpreter.py @@ -1,5 +1,7 @@ import pygame - +import minefield as mf +import project_constants as pc +import main as mai def interpret( event ): @@ -17,9 +19,12 @@ def interpret( event ): # # # this part is true, when you press a key down # # there's also a KEYUP for letting go -# if event.type == pygame.KEYDOWN: + #if event.type == pygame.KEYDOWN: # # # this part is true, when a key u pressed down is the left arrow -# if event.key == pygame.K_LEFT: -# # here should be a call to sapper's movement function + #if event.key == pygame.K_RIGHT: + # mf.sapper_position[0] += 10 + + + # diff --git a/main.py b/main.py index ec91641..4677634 100644 --- a/main.py +++ b/main.py @@ -57,8 +57,20 @@ def main(): if event.type == pygame.QUIT: running = False - else: event_interpreter.interpret( event ) - + # else: event_interpreter.interpret( event ) + # Assigning all input from keyboard as variables into an array + keys = pygame.key.get_pressed() + + # Depending on what key we press, the agent will move in that direction + # DISCRETION : The only keys that are available are arrow keys + if keys[pygame.K_RIGHT]: + minefield.go_right() + elif keys[pygame.K_LEFT]: + minefield.go_left() + elif keys[pygame.K_UP]: + minefield.go_up() + elif keys[pygame.K_DOWN]: + minefield.go_down() if __name__ == "__main__": main() diff --git a/minefield.py b/minefield.py index cb27cf5..641c675 100644 --- a/minefield.py +++ b/minefield.py @@ -22,6 +22,9 @@ mine_asset_options = { } + + + def calculate_screen_position(x, y): coords = ( consts.V_SCREEN_PADDING + consts.V_TILE_SIZE * x, @@ -82,4 +85,18 @@ class Minefield: # draw the sapper sapper_screen_coords = calculate_screen_position(self.sapper_position[0], self.sapper_position[1]) - window.blit(consts.ASSET_SAPPER, sapper_screen_coords) \ No newline at end of file + window.blit(consts.ASSET_SAPPER, sapper_screen_coords) + + # Here are defined functions that move our agent. They are being called in main when certain key is pressed + def go_right(self): + if self.sapper_position[0] != 9: + self.sapper_position = (int(self.sapper_position[0])+1, int(self.sapper_position[1])) + def go_left (self): + if self.sapper_position[0] != 0: + self.sapper_position = (int(self.sapper_position[0])-1, int(self.sapper_position[1])) + def go_up (self): + if self.sapper_position[1] != 0: + self.sapper_position = (int(self.sapper_position[0]), int(self.sapper_position[1])-1) + def go_down (self): + if self.sapper_position[1] != 9: + self.sapper_position = (int(self.sapper_position[0]), int(self.sapper_position[1])+1) \ No newline at end of file