implement visual movement

This commit is contained in:
eugenep 2021-04-14 15:30:36 +02:00
parent 100308694c
commit 0e0a59b6ab
3 changed files with 95 additions and 62 deletions

View File

@ -250,7 +250,7 @@ class BFS:
result = BFS.breadth_first_graph_search(problem)
print(result)
#return result
return result
#print(BFS.print_node_state(result))
@staticmethod

41
main.py
View File

@ -43,13 +43,13 @@ class Game:
if tile == "#":
Wall(self, col, row)
if tile == '>':
self.player = Player(self, col, row)
self.player = Player(self, col, row, Direction.Right.name)
if tile == '^':
self.player = Player(self, col, row)
self.player = Player(self, col, row, Direction.Up.name)
if tile == '<':
self.player = Player(self, col, row)
self.player = Player(self, col, row, Direction.Left.name)
if tile == 'v':
self.player = Player(self, col, row)
self.player = Player(self, col, row. Direction.Down.name)
def run(self):
@ -90,21 +90,40 @@ class Game:
if event.key == pg.K_ESCAPE:
self.quit()
if event.key == pg.K_LEFT:
self.player.move(dx=-1, direction='Left')
self.player.move(dx=0, move='Left')
if event.key == pg.K_RIGHT:
self.player.move(dx=1, direction='Right')
self.player.move(dx=0, move='Right')
if event.key == pg.K_UP:
self.player.move(dy=-1, direction='Up')
if event.key == pg.K_DOWN:
self.player.move(dy=1, direction='Down')
self.player.move(dy=0, move='Forward')
if event.key == pg.K_F2 and self.wentyl_bezpieczenstwa == 0:
self.player.maze.run()
self.player.parse_maze_moves()
self.i_like_to_move_it()
self.wentyl_bezpieczenstwa = 1
if event.key == pg.K_F3:
BFS.run()
if event.key == pg.K_F3 and self.wentyl_bezpieczenstwa == 0:
player_moves = BFS.run()
self.graph_move(player_moves)
self.wentyl_bezpieczenstwa = 1
def graph_move(self, moves):
for i in moves:
if i == 'Right':
self.player.move(dx=0, move='Right')
self.update()
self.draw()
pg.time.delay(250)
if i == 'Left':
self.player.move(dx=0, move='Left')
self.update()
self.draw()
pg.time.delay(250)
if i == 'Forward':
self.player.move(dx=0, move='Forward')
self.update()
self.draw()
pg.time.delay(250)
def i_like_to_move_it(self):

View File

@ -23,63 +23,72 @@ class Player(pg.sprite.Sprite):
self.maze = Maze()
self.moves = ''
def move(self, dx=0, dy=0, direction = ''):
if direction == self.direction:
def set_direction(self, direction):
self.direction = direction
def move(self, dx=0, dy=0, move = ''):
if move == Moves.Right.name:
if self.direction == Direction.Right.name:
if dx > 0:
if self.check_border(dx):
if not self.collide_with_walls(dx):
if self.collide_with_mines(dx):
self.direction = Direction.Down.name
elif self.direction == Direction.Down.name:
self.direction = Direction.Left.name
elif self.direction == Direction.Left.name:
self.direction = Direction.Up.name
elif self.direction == Direction.Up.name:
self.direction = Direction.Right.name
elif move == Moves.Left.name:
if self.direction == Direction.Right.name:
self.direction = Direction.Up.name
elif self.direction == Direction.Down.name:
self.direction = Direction.Right.name
elif self.direction == Direction.Left.name:
self.direction = Direction.Down.name
elif self.direction == Direction.Up.name:
self.direction = Direction.Left.name
elif move == Moves.Forward.name:
if self.direction == Direction.Right.name:
if self.check_border(1):
if not self.collide_with_walls(1):
if self.collide_with_mines(1):
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
print("Mine Ahead!")
self.x += dx
self.x += 1
else:
self.x += dx
self.x += 1
if self.direction == Direction.Up.name:
if dy < 0:
if self.check_border(0, dy):
if not self.collide_with_walls(0, dy):
if self.collide_with_mines(0, dy):
if self.check_border(0, -1):
if not self.collide_with_walls(0, -1):
if self.collide_with_mines(0, -1):
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
print("Mine Ahead!")
self.y += dy
self.y += -1
else:
self.y += dy
self.y += -1
if self.direction == Direction.Down.name:
if dy > 0:
if self.check_border(0, dy):
if not self.collide_with_walls(0, dy):
if self.collide_with_mines(0, dy):
if self.check_border(0, 1):
if not self.collide_with_walls(0, 1):
if self.collide_with_mines(0, 1):
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
print("Mine Ahead!")
self.y += dy
self.y += 1
else:
self.y += dy
self.y += 1
if self.direction == Direction.Left.name:
if dx < 0:
if self.check_border(dx):
if not self.collide_with_walls(dx):
if self.collide_with_mines(dx):
if self.check_border(-1):
if not self.collide_with_walls(-1):
if self.collide_with_mines(-1):
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
print("Mine Ahead!")
self.x += dx
self.x += -1
else:
self.x += dx
self.x += -1
print("I move: " + str(self.direction))
print("My direction is: " + str(self.direction))
elif direction != self.direction:
self.direction = direction
print(self.direction)
"""if self.direction == Direction.Up.name:
self.image = pg.transform.rotate(self.baseImage, 90)
if self.direction == Direction.Right.name:
self.image == pg.transform.rotate(self.baseImage, 360)
if self.direction == Direction.Down.name:
self.image == pg.transform.rotate(self.baseImage, -90)
if self.direction == Direction.Left.name:
self.image == pg.transform.rotate(self.baseImage, -180)"""
def check_border(self, dx=0, dy=0):
if (self.x + dx) < 0 or (self.y + dy) < 0 or (self.x + dx) >= MAP_SIZE or (self.y + dy) >= MAP_SIZE :
@ -187,6 +196,11 @@ class Direction(enum.Enum):
Down = 3;
Right = 4;
class Moves(enum.Enum):
Left = 1
Right = 2
Forward = 3
class Mine(pg.sprite.Sprite):
def __init__(self, game, x, y):