def move(self, direction):
if direction == north and self.player_position[1] < 1:
self.player_position[1] += 1
print(You moved north.)
elif direction == south and self.player_position[1] 0:
self.player_position[1] = 1
print(You moved south.)
根据方向更新玩家位置
def play(self):
print(Welcome to the adventure game!)
while True:
command = input(What do you want to do? (type 'north', 'south', 'take item', 'inventory')\n).lower().split()
if command[0] in [north, south]:
self.move(command[0])
elif command[0] == take:
item = command[1]
if item in self.items:
print(fYou took {item}.)
else:
print(There is no such item here.)
elif command[0] == inventory:
print(Your inventory is empty.)
if __name__ == __main__:
game = Game()
game.play()
步骤 5: 添加简单的AI
你可以添加一个简单的AI来控制游戏中的NPC或其他元素。例如,一个基本的AI可以随机移动或做出反应:
python
import random
class AI:
def __init__(self):
self.position = [random.randint(0, 5), random.randint(0, 5)]
def move(self):
directions = [north, south, east, west]
direction = random.choice(directions)
根据方向更新AI的位置
print(fAI moved {direction}.)