From fc4895fc4ebd45fd2a2e7381d55089d77ddd035f Mon Sep 17 00:00:00 2001 From: Darek Golomski Date: Fri, 9 Apr 2021 18:31:38 +0200 Subject: [PATCH] add agent rotate --- src/agent.py | 1 + src/game_ui.py | 10 +++++++++- src/main.py | 16 ++++++++-------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/agent.py b/src/agent.py index 03801f4..25156b7 100644 --- a/src/agent.py +++ b/src/agent.py @@ -5,5 +5,6 @@ class Agent: def __init__(self, x: int = 0, y: int = 0): self.name = "Kapitan Bomba" self.img = SAPPER_IDLE + self.dir = 1 self.x = x self.y = y diff --git a/src/game_ui.py b/src/game_ui.py index fa09081..8768379 100644 --- a/src/game_ui.py +++ b/src/game_ui.py @@ -12,7 +12,10 @@ class GameUi: self.clock = pg.time.Clock() self.screen = pg.display.set_mode((WIDTH, HEIGHT)) - def move(self, coord: str, shift: int): + def move(self): + coord = 'x' if self.agent.dir in (1, 3) else 'y' + shift = -1 if self.agent.dir in (2, 3) else 1 + # print(coord, shift) for x in range(8): setattr( self.agent, @@ -31,3 +34,8 @@ class GameUi: self.screen.blit(IMAGES[self.env.field[y][x].number].img, (x * 80, y * 80)) self.screen.blit(self.agent.img, (self.agent.x * 80, self.agent.y * 80)) pg.display.update() + + def rotate(self, dir): + self.agent.dir = (self.agent.dir + dir) % 4 + print(self.agent.dir) + diff --git a/src/main.py b/src/main.py index 5149a0d..c30dee4 100644 --- a/src/main.py +++ b/src/main.py @@ -23,17 +23,17 @@ def main(): while running: for event in pg.event.get(): + # print(agent.x*80) if event.type == pg.QUIT: running = False elif event.type == pg.KEYDOWN: - if (event.key == pg.K_d or event.key == pg.K_RIGHT) and agent.x*80 < 700: - game_ui.move('x', 1) - elif (event.key == pg.K_a or event.key == pg.K_LEFT) and agent.x*80 > 5: - game_ui.move('x', -1) - elif (event.key == pg.K_s or event.key == pg.K_DOWN) and agent.y*80 < 700: - game_ui.move('y', 1) - elif (event.key == pg.K_w or event.key == pg.K_UP) and agent.y*80 > 0: - game_ui.move('y', -1) + if (event.key == pg.K_d or event.key == pg.K_RIGHT): + game_ui.rotate(1) + elif (event.key == pg.K_a or event.key == pg.K_LEFT): + game_ui.rotate(-1) + elif (event.key == pg.K_w or event.key == pg.K_UP and ((agent.dir == 1 and agent.x*80 < 700) or (agent.dir == 3 and agent.x*80 > 5) or + (agent.dir == 2 and agent.y*80 > 0) or (agent.dir == 0 and agent.y*80 < 700))): + game_ui.move() elif event.key == pg.K_SPACE: if env.field[agent.y][agent.x].mine: env.field[agent.y][agent.x] = factory.create_tile(