因此,我一直在尝试弄清楚类和实例在python中是如何工作的,并一直在制作一个基于文本的游戏来尝试和了解它。你知道吗
我知道重复一遍通常意味着你做错了什么,但我就是不知道该怎么办。我的代码如下所示:
win = False
class Rooms(object):
walls = 4
def __init__(self, command, desc, secret, x, y):
self.command = command
self.desc = desc
self.secret = secret
self.x = x
self.y = y
desc是对房间的描述,command是用来访问房间的单词,secret是关于房间的额外信息,x和y是放置在xy轴上的房间
def check(self):
if action == self.command:
player.move_x(self.x)
player.move_y(self.y)
if self.x == player.x and self.y == player.y:
print(self.desc)
我一直在努力解决这个问题,因为我需要它来检查命令,看看它需要移动多少x和y空间,但如果我还需要检查玩家的位置是否匹配xy轴上的房间位置。你知道吗
如果我将它们放在一起,则仅当您输入特定命令时才打印房间描述,因此交互将是: '游戏开始文本' '北' '北房间描述' '北' '' '南' ''
即使从逻辑上讲你会在你开始的北边房间。尽管如此,它还是会带来其他问题。你知道吗
class Player(object):
def __init__(self, x, y):
self.x = x
self.y = y
def move_x(self, new_x):
self.x = self.x + new_x
def move_y(self, new_y):
self.y = self.y + new_y
player = Player(0, 0)
default_room = Rooms('start', '...', '...', 0, 0)
north_room = Rooms('north', '...', '...', 0, 1)
south_room = Rooms('south', '...', '...', 0, -1)
west_room = Rooms('west', '...', '...', -1, 0)
east_room = Rooms('east', '...', '...', 1, 0)
print(default_room.desc)
action = input('> ')
while not win:
north_room.check()
south_room.check()
west_room.check()
east_room.check()
default_room.check()
print(player.x, player.y)
action = input('> ')
如前所述,通过检查工作方式,程序的工作方式如下: '游戏开始资料' '北' “北房间说明” '南' '北房间描述' '起始房间描述'
因为程序总是北支票在“南”之前,它会看到它与“北”房间位于同一位置,并显示“北”描述,然后在检查“南”时,它会实际将它移动到正确的位置。你知道吗
最后一件事,我知道使用房间的实际位置作为移动播放器的基础是行不通的,因为一旦你得到一个房间,比如0,-2,然后离开它会让你回到起点。你知道吗
抱歉,如果我问了太多问题,我只是不知道从哪里开始。你知道吗
我的解决方案:我将所有Room实例(重命名为“Room”)添加到一个列表中,并遍历该列表,检查实例x和y的值是否与播放器匹配,例如:
for room in list_of_rooms:
if room.x == player.x and room.y == player.y:
print(room.description)
而不是检查主回路中的所有房间,
一个玩家只能在一个房间里玩, 在
class Player
中保留一个实际房间的引用。你知道吗考虑以下几点:
相关问题 更多 >
编程相关推荐