检查类的每个实例的更好方法?

2024-05-16 02:51:57 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我一直在尝试弄清楚类和实例在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)

Tags: 实例self游戏newsecretmovedefcheck
1条回答
网友
1楼 · 发布于 2024-05-16 02:51:57

Question: ... check every instance of a class?

而不是检查主回路中的所有房间,
一个玩家只能在一个房间里玩, 在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

相关问题 更多 >