diff --git a/images/sapper_idle/agent0.png b/images/sapper_idle/agent0.png new file mode 100644 index 0000000..c3e84ad Binary files /dev/null and b/images/sapper_idle/agent0.png differ diff --git a/images/sapper_idle/agent1.png b/images/sapper_idle/agent1.png new file mode 100644 index 0000000..c634870 Binary files /dev/null and b/images/sapper_idle/agent1.png differ diff --git a/images/sapper_idle/agent.png b/images/sapper_idle/agent2.png similarity index 100% rename from images/sapper_idle/agent.png rename to images/sapper_idle/agent2.png diff --git a/images/sapper_idle/agent3.png b/images/sapper_idle/agent3.png new file mode 100644 index 0000000..f1b16bb Binary files /dev/null and b/images/sapper_idle/agent3.png differ diff --git a/src/agent.py b/src/agent.py index 03801f4..d9e860c 100644 --- a/src/agent.py +++ b/src/agent.py @@ -1,9 +1,11 @@ from const import SAPPER_IDLE +import pygame as pg class Agent: def __init__(self, x: int = 0, y: int = 0): self.name = "Kapitan Bomba" - self.img = SAPPER_IDLE + self.img = pg.image.load(SAPPER_IDLE[1]) + self.dir = 1 self.x = x self.y = y diff --git a/src/const.py b/src/const.py index 4b0208c..5212b40 100644 --- a/src/const.py +++ b/src/const.py @@ -37,7 +37,13 @@ for name, val in { ) ICON = main_path + '/images/mine_icon.png' -SAPPER_IDLE = pg.image.load(main_path + '/images/sapper_idle/agent.png') +SAPPER_IDLE = [main_path + '/images/sapper_idle/agent0.png', + main_path + '/images/sapper_idle/agent1.png', + main_path + '/images/sapper_idle/agent2.png', + main_path + '/images/sapper_idle/agent3.png'] + + +#pg.image.load(main_path + '/images/sapper_idle/agent1.png') #empty space diff --git a/src/game_ui.py b/src/game_ui.py index fa09081..04dc057 100644 --- a/src/game_ui.py +++ b/src/game_ui.py @@ -1,4 +1,6 @@ import pygame as pg +import os +main_path = os.path.dirname(os.getcwd()) from agent import Agent from environment import Environment @@ -12,7 +14,17 @@ 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) + test = int(getattr(self.agent, coord) + shift) + if coord == 'x': + if self.env.field[self.agent.y][test].number in (2, 3): + return + elif coord == 'y': + if self.env.field[test][self.agent.x].number in (2, 3): + return for x in range(8): setattr( self.agent, @@ -22,7 +34,6 @@ class GameUi: ) self.update() self.clock.tick(15) - self.agent.img = SAPPER_IDLE self.update() def update(self): @@ -31,3 +42,11 @@ 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) + self.agent.img = pg.image.load(SAPPER_IDLE[self.agent.dir]) + self.update() + + diff --git a/src/main.py b/src/main.py index 5149a0d..e7175e5 100644 --- a/src/main.py +++ b/src/main.py @@ -21,19 +21,25 @@ def main(): running = True + #ruchy agenta: + #1 - right + #2 - up + #3 - left + #0 - down + 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(