gpt4 book ai didi

python - 如何防止玩家移动到给定区域 map 之外?

转载 作者:行者123 更新时间:2023-12-01 00:26:28 30 4
gpt4 key购买 nike

我使用名为 zonemap 的字典创建了一个 4x6 区域 map 。我在该字典中嵌套了多个字典;每个区域代表玩家可以访问并与之互动的区域。我希望能够将玩家的移动限制在该 4x6 区域,并重新显示他们尝试访问“禁止”区域的位置。仅当玩家尝试访问由空字符串标记的区域时,才会发生此限制。示例代码如下:

import cmd
import textwrap
import sys
import os
import time
import random

class player:
def __init__(self):
self.name = ''
self.job = ''
self.hp = 0
self.mp = 0
self.stamina = 0
self.faith = 0
self.status_effects = []
self.location = "a2"
self.gameOver = False
myPlayer = player()

ZONENAME = ''
DESCRIPTION = 'description'
EXAMINATION = 'examine'
SOLVED = False
UP = 'up', 'north'
DOWN = 'down', 'south'
LEFT = 'left', 'west'
RIGHT = 'right', 'east'

solved_places = {'a1': False, 'a2': False, # more places}

zonemap = {
'a1': {
ZONENAME: "West Mounatains",
DESCRIPTION: "A mountain range.",
EXAMINATION: "The slope is too steep to climb.",
SOLVED: False,
UP: '',
DOWN: "Western Forest",
LEFT: '',
RIGHT: "Central Mountains",
},
'a2': {
ZONENAME: "Central Mountains",
DESCRIPTION: "A mountain range",
EXAMINATION: "These mountains go on for miles.",
SOLVED: False,
UP: '',
DOWN: "Eastern Forest",
LEFT: "Western Mountains",
RIGHT: "Mountain Cave",
},
# more dictionaries
}

def prompt():
print("\n" + "=========================")
print("What would you like to do?")
player_action = input("> ")
acceptable_actions = ['move', 'go', 'travel', 'walk', 'quit', 'examine', 'inspect', 'interact', 'look']
while player_action.lower() not in acceptable_actions:
print("Unknown action, try again.\n")
player_action = input("> ")
if player_action.lower() == 'quit':
sys.exit()
elif player_action.lower() in ['move', 'go', 'travel', 'walk']:
player_move(player_action.lower())
elif player_action.lower() in ['examine', 'inspect', 'interact', 'look']:
player_examine(player_action.lower())

def player_move(player_action):
ask = "Where would you like to move?\n"
dest = input(ask)
if dest in ['up', 'north']:
destination = zonemap[myPlayer.location][UP]
movement_handler(destination)
elif dest in ['down', 'south']:
destination = zonemap[myPlayer.location][DOWN]
movement_handler(destination)
elif dest in ['left', 'west']:
destination = zonemap[myPlayer.location][LEFT]
movement_handler(destination)
elif dest in ['right', 'east']:
destination = zonemap[myPlayer.location][RIGHT]
movement_handler(destination)

def movement_handler(destination):
print("\n" + "You have moved to the " + destination + ".")
myPlayer.location = destination
print_location()

我认为 if 语句可行,但我不知道该把它放在哪里。任何帮助,将不胜感激。如果需要,我还可以提供更多代码。

最佳答案

是的,if 可以。检查用户想去哪里。 如果无效,则打印一条消息并且不要移动玩家。 否则移动它们。

def player_move(player_action):
ask = "Where would you like to move?\n"
dest = input(ask).lower()
if dest in ['up', 'north']:
destination = UP
elif dest in ['down', 'south']:
destination = DOWN
elif dest in ['left', 'west']:
destination = LEFT
elif dest in ['right', 'east']:
destination = RIGHT
else:
print("Bad entry")
return

if "" == zonemap[myPlayer.location][destination]:
print("Can't go there")
else:
movement_handler(zonemap[myPlayer.location][destination])

关于python - 如何防止玩家移动到给定区域 map 之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58563827/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com