fix queue events
This commit is contained in:
parent
6d34b95f8f
commit
5ac5fd9aea
70
src/main.py
70
src/main.py
@ -8,10 +8,32 @@ from BFS import breadth_first_search
|
|||||||
from tilesFactory import TilesFactory
|
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():
|
def main():
|
||||||
pg.init()
|
pg.init()
|
||||||
pg.display.set_caption('Super Saper')
|
pg.display.set_caption('Super Saper')
|
||||||
pg.display.set_icon(pg.image.load(ICON))
|
pg.display.set_icon(pg.image.load(ICON))
|
||||||
|
pg.fastevent.init()
|
||||||
|
|
||||||
env = Environment()
|
env = Environment()
|
||||||
agent = Agent()
|
agent = Agent()
|
||||||
@ -31,44 +53,24 @@ def main():
|
|||||||
# 2 - down -> 2
|
# 2 - down -> 2
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
for event in pg.event.get():
|
for event in pg.fastevent.get():
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
running = False
|
running = False
|
||||||
elif event.type == pg.KEYDOWN:
|
elif event.type == pg.KEYDOWN:
|
||||||
if event.key == pg.K_d or event.key == pg.K_RIGHT:
|
if event.key == pg.K_t:
|
||||||
game_ui.rotate(-1)
|
while env.mine_count:
|
||||||
elif event.key == pg.K_a or event.key == pg.K_LEFT:
|
path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
|
||||||
game_ui.rotate(1)
|
print(path, actions)
|
||||||
elif (event.key == pg.K_w or event.key == pg.K_UP) \
|
if not path and not env.field[agent.y][agent.x].mine:
|
||||||
and ((agent.direction == 1 and agent.x * 80 < 700)
|
break
|
||||||
or (agent.direction == 3 and agent.x * 80 > 5)
|
for action in actions:
|
||||||
or (agent.direction == 0 and agent.y * 80 > 0)
|
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
|
||||||
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}))
|
|
||||||
|
|
||||||
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__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user