玩家和Block类与我的世界级通信?

时间:2015-03-13 10:44:57

标签: python class oop object python-3.x

我正在创建一个简单的2D控制台游戏,这是我目前的课程:

class World(list):
    def __init__(self, data=(), players=None):
        super().__init__(data)
        self.players = players or []

    def print(self):
        for y in range(len(self)):
            line = ''
            for x in range(len(self[y])):
                for player in self.players:
                    if player.x == x and player.y == y: 
                        line += player.char
                        break
                else:
                    line += self[y][x]
            print(line)

class Block:
    def __init__(self, x=0, y=0, char=' ', solid=False):
        self.x = x
        self.y = y
        self.char = char
        self.solid = solid

class Player(Block):
    def __init__(self, x=0, y=0, char='i', solid=True):
        super().__init__(x, y, char, solid)

    def move_up(self):
        pass  # What do I do here?

我不知道怎么做才能让我的玩家知道如何与世界沟通,看看他上方的区块是否存在b)是不是很稳固? 我是OOP的新手。

修改 我如何使用游戏:

my_world = World([
    Block(0, 0, 'x', True), Block(1, 0, 'x', True), Block(2, 0, 'x', True),
    Block(0, 1, 'x', True), Block(1, 1), Block(2, 1, 'x', True),
    Block(0, 2, 'x', True), Block(1, 2), Block(2, 2, 'x', True),
    Block(0, 3, 'x', True), Block(1, 3, 'x', True), Block(2, 3, 'x', True)
])
my_world.players.append(Player(1, 1, 'i'))

创造一个这样的世界:

xxx
xix
x x
xxx

玩家只能上下移动一个街区。

1 个答案:

答案 0 :(得分:1)

玩家知道如何与他们的世界进行交流的一种简单方法是通过向类添加新属性将其存储在每个人中。这样,实例可以通过它访问它们所属的世界。

另一种选择是在他们被召唤时将世界作为参与者传递给玩家的移动方法。

我必须解决您问题中代码的几个问题,但下面的内容似乎可以正常运行。

class World(list):
    def __init__(self, data=(), players=None):
        super().__init__(data)
        self.players = players or []

    def print(self):
        for y in range(len(self)):
            line = '  '
            for x in range(len(self[y])):
                for player in self.players:
                    if player.x == x and player.y == y:
                        line += player.char
                        break
                else:
                    line += self[y][x].char
            print(line)

class Block:
    def __init__(self, x=0, y=0, char=' ', solid=False):
        self.x = x
        self.y = y
        self.char = char
        self.solid = solid

class Player(Block):
    def __init__(self, x=0, y=0, char='i', solid=True, world=None):
        super().__init__(x, y, char, solid)
        self.world = world

    def move_down(self):
        if self.world is None:
            raise ValueError("Can't move player that has no world")
        x, y = self.x, self.y
        if self.world[y+1][x].solid:
            pass  # Ignore, can't move into solid position.
        else:
            self.world[y][x] = Block(x, y)  # Replace ref in attached world.
            self.world[y+1][x] = self
            self.y += 1  # Update position.

以下是执行修订课程的代码:

my_world = World([
    [Block(0, 0, 'x', True), Block(1, 0, 'x', True), Block(2, 0, 'x', True)],
    [Block(0, 1, 'x', True), Block(1, 1),            Block(2, 1, 'x', True)],
    [Block(0, 2, 'x', True), Block(1, 2),            Block(2, 2, 'x', True)],
    [Block(0, 3, 'x', True), Block(1, 3, 'x', True), Block(2, 3, 'x', True)]
])
player1 = Player(1, 1, 'i', world=my_world)
my_world.players.append(player1)
print("Before:")
my_world.print()
print()
print("After:")
player1.move_down()
my_world.print()

输出:

Before:
  xxx
  xix
  x x
  xxx

After:
  xxx
  x x
  xix
  xxx