Python文字冒险游戏:打造你的文字类求生探索游戏
class TextAdventureGame: def init(self): self.current_room = None self.game_over = False
def start_game(self):
self.current_room = Room('Start', 'You find yourself in a dark room with no windows. There is a door to the north.')
self.play_game()
def play_game(self):
while not self.game_over:
print(self.current_room.description)
user_input = input('What do you want to do? ')
if user_input.lower() == 'quit':
self.game_over = True
else:
self.handle_input(user_input)
def handle_input(self, user_input):
directions = ['north', 'south', 'east', 'west']
if user_input.lower() in directions:
new_room = self.current_room.get_adjacent_room(user_input.lower())
if new_room is not None:
self.current_room = new_room
else:
print('You can't go that way.')
else:
print('I don't understand that command.')
class Room: def init(self, name, description): self.name = name self.description = description self.adjacent_rooms = {}
def get_adjacent_room(self, direction):
if direction in self.adjacent_rooms:
return self.adjacent_rooms[direction]
else:
return None
def add_adjacent_room(self, direction, room):
self.adjacent_rooms[direction] = room
if name == 'main': game = TextAdventureGame() game.start_game()
原文地址: https://www.cveoy.top/t/topic/lZzm 著作权归作者所有。请勿转载和采集!