基于Python平铺的游戏(2D阵列)播放器运动

时间:2016-10-12 20:14:36

标签: python pygame

我在pygame中制作了一个胭脂,我在网格中玩家移动时遇到了麻烦。此示例程序显示了我计划如何执行此操作:

class P:

    def __init__(self, standing_on):
        self.standing_on = standing_on
        self.row, self.column = 4, 4

    def __str__(self):
        return "@"


class G:
    walkable = True

    def __str__(self):
        return "█"


class W:
    walkable = False

    def __str__(self):
        return "|"

p = P(G())
game_map = [
    [W(), W(), W(), W(), W(), W(), W(), W(), W(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), p,   G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), W(), W(), W(), W(), W(), W(), W(), W(), W()]
]


def print_map():
    for column in range(10):
        for row in range(10):
            print(game_map[column][row], end="")
        print()


def move_up():
    temp = p.row - 1
    if game_map[temp][p.column].walkable:
        game_map[p.column][p.row] = p.standing_on
        p.column -= 1
        p.standing_on = game_map[p.column][p.row]
        game_map[p.column][p.row] = p


print_map()
print(p.row, p.column, "\n")
move_up()
print_map()
print(p.row, p.column, "\n")
move_up()
print_map()
print(p.row, p.column, "\n")
  • p = player
  • g = grass
  • w = wall

和输出:

||||||||||
|████████|
|████████|
|████████|
|████████|
|████████|
|█@██████|
|████████|
|████████|
||||||||||
4 4

||||||||||
|████████|
|████████|
|███@████|
|████████|
|████████|
|█@██████|
|████████|
|████████|
||||||||||
4 3

||||||||||
|████████|
|███@████|
|████████|
|████████|
|████████|
|█@██████|
|████████|
|████████|
||||||||||
4 2

地图下的数字代表玩家坐标。我从4点开始4(注意它的0索引)并向上移动两次。显示时,地图是完全错误的,我已经在我的实际游戏中测试了它并使用图像而不是文本获得相同的错误。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题是你的起始位置。您需要在没有播放器的情况下绘制地图,然后将播放器放在地图上。以下是有效的解决方案:

class P:
   def __init__(self, standing_on):
       self.standing_on = standing_on
       self.row, self.column = 4, 4

   def __str__(self):
       return "@"


class G:
    walkable = True

    def __str__(self):
        return "█"


class W:
    walkable = False

    def __str__(self):
        return "|"

p = P(G())
game_map = [
    [W(), W(), W(), W(), W(), W(), W(), W(), W(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), G(), G(), G(), G(), G(), G(), G(), G(), W()],
    [W(), W(), W(), W(), W(), W(), W(), W(), W(), W()]
]


def print_map():
    game_map[p.column][p.row] = p
    for column in range(10):
        for row in range(10):
            print(game_map[column][row], end="")
        print()

def move_up():
    temp = p.row - 1
    if game_map[temp][p.column].walkable:
        game_map[p.column][p.row] = p.standing_on
        p.column -= 1
        p.standing_on = game_map[p.column][p.row]


print_map()
print(p.row, p.column, "\n")
move_up()
print_map()
print(p.row, p.column, "\n")
move_up()
print_map()
print(p.row, p.column, "\n")