基于Python tile的游戏(2d数组)玩家移动

基于Python tile的游戏(2d数组)玩家移动,python,pygame,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 = Tru

我在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=球员
  • g=草
  • w=墙
以及输出:

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

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

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

地图下面的数字代表玩家的坐标。我从4开始,4(注意它的索引为0)向上移动两次。当显示的地图是完全错误的,虽然,我已经测试了它在我的实际游戏中,并得到相同的错误,使用图像,而不是文字。知道发生了什么吗?

问题是你的出发位置。你需要在没有玩家的情况下绘制地图,然后将玩家放在地图上。以下是有效的解决方案:

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")

恐怕这不是调试服务。不过这是个有趣的问题。祝你好运,谢谢!为什么这很重要?你总是安排两名球员。一个在游戏地图中,另一个在上移功能中。但是你需要不带玩家的游戏地图,然后在每次调用print_map(print_map函数的第一行)时绘制该玩家。您在游戏地图中设置的玩家位置没有改变,第二个玩家正确移动的第一个位置(4,4)从未绘制。