I am coding a simple maze game. The maze pattern is inside a .txt file. I read it in python and create two lists in python. One with the coordonate of the walls, the other with the available paths.
I've create a Player class with a move method. But whenever I want to check if the move is in the available paths list, it does run the move method and changes my character position.
from classes import *
maze = Maze ('maze.txt')
maze.create_maze()
mac = Player('mac',[2,2])
keep_playing = True
while keep_playing:
print (mac.position)
direction = input("Where are you heading to?")
if mac.move(direction) in maze.paths:
print ('ok')
else: print('No way!')
class Maze :
def __init__ (self, level):
self.level = level
self.walls = []
self.paths = []
self.choices = []
def create_maze (self):
with open (self.level) as f:
for y, line in enumerate (f,1):
for x, c in enumerate (line,1):
if c == 'x':
self.walls.append([x,y])
elif c == '0':
self.paths.append([x,y])
return self.walls, self.paths
class Player :
def __init__ (self,name, position):
self.name = name
self.position = position
def move (self, direction):
if direction == "right":
self.position [0] += 1
return self.position
elif direction == "left":
self.position [0] -= 1
return self.position
elif direction == "up":
self.position [1] -= 1
return self.position
elif direction == "down":
self.position [1] += 1
return self.position
Aucun commentaire:
Enregistrer un commentaire