Added simple movement with usage of arrow keys. Those functions have been declared in Minefield class (temporarily)

This commit is contained in:
Jakub Danilewicz 2021-03-14 11:32:44 +01:00
parent 913d58c517
commit 8d29c7078f
3 changed files with 41 additions and 7 deletions

View File

@ -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
#

16
main.py
View File

@ -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()

View File

@ -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)
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)