diff --git a/images/wall.bmp b/images/wall.bmp new file mode 100644 index 0000000..9d3c665 Binary files /dev/null and b/images/wall.bmp differ diff --git a/main.py b/main.py index 2bf2716..87ffe77 100644 --- a/main.py +++ b/main.py @@ -39,6 +39,8 @@ class Game: Bomb(self, col, row) if tile == '4': Grenade(self, col, row) + if tile == "#": + Wall(self, col, row) if tile == 'A': self.player = Player(self, col, row) diff --git a/map.txt b/map.txt index 8ac70b3..4fb8aa7 100644 --- a/map.txt +++ b/map.txt @@ -1,7 +1,7 @@ .A..... +###.... ....... ....... -....... -....... -....2.. +...###. +...#2#. ....... diff --git a/maze.py b/maze.py index 3ce671a..b6a5a03 100644 --- a/maze.py +++ b/maze.py @@ -69,6 +69,8 @@ class Maze: if not(0 <= i < len(maze[0]) and 0 <= j < len(maze)): return False + elif (maze[j][i] == "#"): + return False return True diff --git a/sprites.py b/sprites.py index 0ab3e8b..5433ebc 100644 --- a/sprites.py +++ b/sprites.py @@ -28,42 +28,46 @@ class Player(pg.sprite.Sprite): if self.direction == Direction.Right.name: if dx > 0: if self.check_border(dx): - if self.collide_with_mines(dx): - #ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1) - print("Mine Ahead!") - self.x += dx - else: - self.x += dx + if not self.collide_with_walls(dx): + if self.collide_with_mines(dx): + #ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1) + print("Mine Ahead!") + self.x += dx + else: + self.x += dx if self.direction == Direction.Up.name: if dy < 0: if self.check_border(0, dy): - if self.collide_with_mines(0, dy): - #ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1) - print("Mine Ahead!") - self.y += dy - else: - self.y += dy + if not self.collide_with_walls(0, dy): + if self.collide_with_mines(0, dy): + #ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1) + print("Mine Ahead!") + self.y += dy + else: + self.y += dy if self.direction == Direction.Down.name: if dy > 0: if self.check_border(0, dy): - if self.collide_with_mines(0, dy): - #ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1) - print("Mine Ahead!") - self.y += dy - else: - self.y += dy + if not self.collide_with_walls(0, dy): + if self.collide_with_mines(0, dy): + #ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1) + print("Mine Ahead!") + self.y += dy + else: + self.y += dy if self.direction == Direction.Left.name: if dx < 0: if self.check_border(dx): - if self.collide_with_mines(dx): - #ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1) - print("Mine Ahead!") - self.x += dx - else: - self.x += dx + if not self.collide_with_walls(dx): + if self.collide_with_mines(dx): + #ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1) + print("Mine Ahead!") + self.x += dx + else: + self.x += dx elif direction != self.direction: self.direction = direction @@ -90,6 +94,12 @@ class Player(pg.sprite.Sprite): return True return False + def collide_with_walls(self, dx=0, dy=0): + for wall in self.game.walls: + if wall.x == self.x + dx and wall.y == self.y + dy: + return True + return False + """ right = 1 @@ -105,49 +115,60 @@ class Player(pg.sprite.Sprite): def parse_maze_moves(self): self.moves = [char for char in self.maze.moves] - for n, i in enumerate(self.moves): - if i == 'R': - self.moves[n] = 'Right' - if n != len(self.moves)-1: - if i != self.moves[n+1]: - if self.moves[n+1] == 'D': - self.moves.insert(n+1, 'Turn Down') - if self.moves[n+1] == 'U': - self.moves.insert(n+1, 'Turn Up') - if self.moves[n+1] == 'L': - self.moves.insert(n+1, 'Turn Left') - else: - self.move(dx=-1, direction='Right') - if i == 'L': - self.moves[n] = 'Left' - if n != len(self.moves)-1: - if i != self.moves[n+1]: - if self.moves[n+1] == 'D': - self.moves.insert(n+1, 'Turn Down') - if self.moves[n+1] == 'U': - self.moves.insert(n+1, 'Turn Up') - if self.moves[n+1] == 'R': - self.moves.insert(n+1, 'Turn Right') - if i == 'D': - self.moves[n] = 'Down' - if n != len(self.moves)-1: - if i != self.moves[n+1]: - if self.moves[n+1] == 'R': - self.moves.insert(n+1, 'Turn Right') - if self.moves[n+1] == 'U': - self.moves.insert(n+1, 'Turn Up') - if self.moves[n+1] == 'L': - self.moves.insert(n+1, 'Turn Left') - if i == 'U': - self.moves[n] = 'Up' - if n != len(self.moves)-1: - if i != self.moves[n+1]: - if self.moves[n+1] == 'D': - self.moves.insert(n+1, 'Turn Down') - if self.moves[n+1] == 'R': - self.moves.insert(n+1, 'Turn Right') - if self.moves[n+1] == 'L': - self.moves.insert(n+1, 'Turn Left') + + + if self.moves[0] != 'R': + if self.moves[0] == 'D': + self.moves.insert(0, 'Turn Down') + if self.moves[0] == 'U': + self.moves.insert(0, 'Turn Up') + if self.moves[0] == 'L': + self.moves.insert(0, 'Turn Left') + + for n, i in enumerate(self.moves): + if i == 'R': + self.moves[n] = 'Right' + if n != len(self.moves)-1: + if i != self.moves[n+1]: + if self.moves[n+1] == 'D': + self.moves.insert(n+1, 'Turn Down') + if self.moves[n+1] == 'U': + self.moves.insert(n+1, 'Turn Up') + if self.moves[n+1] == 'L': + self.moves.insert(n+1, 'Turn Left') + else: + self.move(dx=-1, direction='Right') + if i == 'L': + self.moves[n] = 'Left' + if n != len(self.moves)-1: + if i != self.moves[n+1]: + if self.moves[n+1] == 'D': + self.moves.insert(n+1, 'Turn Down') + if self.moves[n+1] == 'U': + self.moves.insert(n+1, 'Turn Up') + if self.moves[n+1] == 'R': + self.moves.insert(n+1, 'Turn Right') + if i == 'D': + self.moves[n] = 'Down' + if n != len(self.moves)-1: + if i != self.moves[n+1]: + if self.moves[n+1] == 'R': + self.moves.insert(n+1, 'Turn Right') + if self.moves[n+1] == 'U': + self.moves.insert(n+1, 'Turn Up') + if self.moves[n+1] == 'L': + self.moves.insert(n+1, 'Turn Left') + if i == 'U': + self.moves[n] = 'Up' + if n != len(self.moves)-1: + if i != self.moves[n+1]: + if self.moves[n+1] == 'D': + self.moves.insert(n+1, 'Turn Down') + if self.moves[n+1] == 'R': + self.moves.insert(n+1, 'Turn Right') + if self.moves[n+1] == 'L': + self.moves.insert(n+1, 'Turn Left') + print(self.moves) @@ -220,4 +241,21 @@ class Grenade(pg.sprite.Sprite): self.rect.x = self.x * TILESIZE self.rect.y = self.y * TILESIZE +class Wall(pg.sprite.Sprite): + def __init__(self, game, x, y): + self.groups = game.all_sprites, game.walls + pg.sprite.Sprite.__init__(self, self.groups) + self.game = game + #self.image = pg.Surface((TILESIZE, TILESIZE)) + self.image = pg.image.load('images/wall.bmp') + #self.image.fill(YELLOW) + self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE)) + self.rect = self.image.get_rect() + self.x = x + self.y = y + + def update(self): + self.rect.x = self.x * TILESIZE + self.rect.y = self.y * TILESIZE + \ No newline at end of file