class Game:
player = Player('')
#三个单引号可以实现多行字符串,用splitlines的方法,转换为列表
grid = '''..###
#....
#.#.#
#.#.#
#.#..'''
current_grid = []
def __init__(self,name):
self.player.name = name
self.updateGrid()
def updateGrid(self):
self.current_grid = self.grid.splitlines()
for i in range(len(self.current_grid)):
self.current_grid[i] = list(self.current_grid[i])
self.setPlayer(self.player.pos[0],self.player.pos[1])
def setPlayer(self, row, col):
self.current_grid[row][col] = 'P'
def playerMove(self,row,col):
if self.current_grid[row][col] == '#':
self.player.damage(-1)
if self.player.hp <= 0:
print('Die')
exit()
else:
self.player.setPos(row,col)
def printGrid(self):
print('Player:',self.player.name," energy:", self.player.energy," hp:",self.player.hp)
for i in self.current_grid:
for j in i:
print(j,end='')
print()
def gameInput(self):
move = input()
row,col = self.player.pos
if move == 'w':
self.playerMove(row-1,col)
elif move == 's':
self.playerMove(row+1,col)
elif move == 'a':
self.playerMove(row,col-1)
elif move == 'd':
self.playerMove(row,col+1)
self.updateGrid()
def checkEnd(self):
if self.player.pos[0] == len(self.current_grid) - 1 and self.player.pos[1] == len(self.current_grid[0]) - 1:
print('Bingo')
return True
if self.player.energy <= 0:
print('Die')
return True
return False