繁体   English   中英

基于 Python 瓷砖的游戏(二维数组)玩家运动

[英]Python tile based game (2d array) player movement

我在 pygame 中制作了一个 rougelike,我在网格中的玩家移动时遇到了问题。 这个示例程序显示了我打算如何做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM