In order to implement a 'maze game' I need to move a character 'X' around a pre-built maze that exists as a .txt file. I have been trying to achieve this via various read/write/append methods but thus far have had no luck. The best I've been able to do is to read and print the coordinates of the 'X' in the text, but not manipulate it in any way. Any help in sorting out a means by which to move it would be greatly appreciated.
class game():
def __init__(self):
self.score = 0
self.exit = False
self.board = []
filepath = 'maze.txt'
with open(filepath,'r') as fp:
line = fp.readline()
while(line):
self.board.append(list(line))
line = fp.readline()
def show_board (self):
CSI="\x1b["
print ("----------")
print(CSI+"38;40m" + 'score:'+ str(self.score) + CSI + "0m")
for i in range(len(self.board)):
for j in range(len(self.board[i])):
if self.board[i][j] == "X":
CSI="\x1b["
print(CSI+"33;91m" + ''.join(self.board[i][j])+ CSI + "0m", end='', flush=True)
elif self.board[i][j] == "*":
CSI="\x1b["
print(CSI+"36;40m" + ''.join(self.board[i][j])+ CSI + "0m", end='', flush=True)
elif self.board[i][j] == "@":
CSI="\x1b["
print(CSI+"32;40m" + ''.join(self.board[i][j])+ CSI + "0m", end='', flush=True)
else:
CSI="\x1B["
print(CSI+"31;40m" + ''.join(self.board[i][j])+ CSI + "0m", end='', flush=True)
def detect_start_point(self):
for x in range(len(self.board)):
for y in range(len(self.board[x])):
if self.board[x][y] == 'X':
self.i,self.j = x,y
def move_player(self, move):
return
#you may remove return (if it is not required)
board = game()
board.detect_start_point()
board.show_board()
while(True): # you may need to change the condition
# get the selected action from the user here
move=input()
# implement
board.move_player(move)
board.show_board()
In theory I should only need to finish the:
def move_player(self, move):
return
#you may remove return (if it is not required)
But my attempts to produce even basic movement have failed.
For anyone interested in the particulars, the text file (referred to in the code as 'maze.txt') contains:
##############################
# X * # #
# ############ # #
# * # # #
# # * # #
# ###### #
# #
########## @ #
#* # #
# # ##############
# # * # * #
# # #
# * #
##############################
Where the * are items to collect, and @ is a gate to finish the level. I have no issue coding the conditions for those however.