所以我一直在尝试弄清楚类和实例在 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,然后离开它就会带你回到原来的位置。开始。
抱歉,如果我问了太多问题,那只是我不知道从哪里开始。
我的解决方案:我将所有房间实例(重命名为“房间”)添加到列表中,并迭代该列表,检查实例 x 和 y 值是否与玩家匹配,例如:
for room in list_of_rooms:
if room.x == player.x and room.y == player.y:
print(room.description)
最佳答案
Question: ... check every instance of a class?
不是检查主循环中的所有房间,
因为玩家只能在一个房间里,
在类Player
中保留实际房间的引用。
Note: In this Example you could't leave a Room, as no Doors implemented.
考虑以下因素:
class Room(object):
DIMENSION = (5, 5)
def __init__(self, name):
self.name = name
self.pos = (0, 0)
def __str__(self):
return self.name
def move(self, _move):
_pos = (self.pos[0] + _move[0], self.pos[1] + _move[1])
if abs(_pos[0]) == Room.DIMENSION[0] or abs(_pos[1]) == Room.DIMENSION[1]:
return False
self.pos = _pos
return True
class House(object):
def __init__(self, start_room=0):
self.rooms = [Room('Hall'),
Room('Room-1 North'), Room('Room-2 East'),
Room('Room-3 South'), Room('Room-4 West')]
self.room = self.rooms[start_room]
def move(self, _move):
if not self.room.move(_move):
print('Move imposible, you reached the wall!'.format())
return self.room
class Player(object):
def __init__(self, name, house):
self.name = name
self.house = house
self.room = self.house.room()
def move(self, _move):
self.room = self.house.move(_move)
if __name__ == '__main__':
player = Player('Player-1', House(start_room=0))
while True:
print('{}: You are in {} at Position:{}'.format(player.name, player.room, player.room.pos))
action = input('Move to (north, east, south, west)> ')
if action in ["north", "east", "south", "west"]:
player.move({'north': (1, 0), 'east': (0, 1), 'south': (-1, 0), 'west': (0, -1)}[action])
elif action == 'break':
break
else:
print('Unknown Action? {}'.format(action))
Output:
Player-1: You are in Hall at Position:(4, 0) Move to (north, east, south, west)> north Move imposible, you reached the wall! Player-1: You are in Hall at Position:(4, 0) Move to (north, east, south, west)> south Player-1: You are in Hall at Position:(3, 0) Move to (north, east, south, west)> break
关于python - 检查类的每个实例的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251613/