add basic console logging, fix rotating

This commit is contained in:
matixezor 2021-04-12 18:50:52 +02:00
parent 995dd6860e
commit 90e11febc5

View File

@ -12,23 +12,28 @@ def handle_keys(env, agent, game_ui, factory):
for event in pg.fastevent.get():
if event.type == pg.KEYDOWN:
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:
print('Turning right')
game_ui.rotate(1)
elif event.key == pg.K_a or event.key == pg.K_LEFT:
print('Turning left')
game_ui.rotate(-1)
elif (event.key == pg.K_w or event.key == pg.K_UP) \
and ((agent.direction == 1 and agent.x * 80 < 700)
or (agent.direction == 3 and agent.x * 80 > 5)
or (agent.direction == 0 and agent.y * 80 > 0)
or (agent.direction == 2 and agent.y * 80 < 700)):
print('Moving forward')
game_ui.move()
elif event.key == pg.K_SPACE:
if env.field[agent.y][agent.x].mine:
print('Defusing mine')
env.mine_count -= 1
env.field[agent.y][agent.x] = factory.create_tile(
IMAGES[env.field[agent.y][agent.x].number].parent
)
game_ui.update()
def main():
pg.init()
pg.display.set_caption('Super Saper')
@ -44,13 +49,11 @@ def main():
running = True
# TODO
# poprawić numeracje stron agenta by szło zgodnie z zegarem
# ruchy agenta:
# 1 - right -> 1
# 0 - up -> 0
# 3 - left -> 3
# 2 - down -> 2
# 0 - up
# 1 - right
# 2 - down
# 3 - left
while running:
for event in pg.fastevent.get():
@ -58,16 +61,21 @@ def main():
running = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_t:
print('Starting to clear the sector')
while env.mine_count:
print('-'*20)
path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
print(path, actions)
if not path and not env.field[agent.y][agent.x].mine:
print('Unable to find path, rocks are in the way')
break
print(f'Path{path}')
print(f'Actions:{actions}')
for action in actions:
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
handle_keys(env, agent, game_ui, factory)
print('Sector clear')
else:
pg.fastevent.post(event)
handle_keys(env, agent, game_ui, factory)