fix queue events

This commit is contained in:
matixezor 2021-04-12 16:49:43 +02:00
parent 6d34b95f8f
commit 5ac5fd9aea

View File

@ -8,10 +8,32 @@ from BFS import breadth_first_search
from tilesFactory import TilesFactory
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:
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)):
game_ui.move()
elif event.key == pg.K_SPACE:
if env.field[agent.y][agent.x].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')
pg.display.set_icon(pg.image.load(ICON))
pg.fastevent.init()
env = Environment()
agent = Agent()
@ -31,44 +53,24 @@ def main():
# 2 - down -> 2
while running:
for event in pg.event.get():
for event in pg.fastevent.get():
if event.type == pg.QUIT:
running = False
elif 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:
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)):
game_ui.move()
elif event.key == pg.K_SPACE:
if env.field[agent.y][agent.x].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()
elif event.key == pg.K_t:
# TODO
# petla while dopóki env.mine_count != 0
# uwzglednienie czy bfs zwraca False i wtedy break
# for i in range(2):
# while env.mine_count:
pg.time.delay(50)
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("Nie ma więcej min!")
break
for action in actions:
print(action)
pg.event.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
if event.key == pg.K_t:
while env.mine_count:
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:
break
for action in actions:
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
pg.event.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
handle_keys(env, agent, game_ui, factory)
else:
pg.fastevent.post(event)
handle_keys(env, agent, game_ui, factory)
if __name__ == "__main__":