パイゲームでルーゲリクを作っていて、グリッド内のプレーヤーの動きに問題があります。このサンプル・プログラムでは、私はそれを行うことを計画方法を示しています。Pythonのタイルベースのゲーム(2d配列)のプレイヤーの動き
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 =プレイヤー
- グラム=草
- 壁
と出力= W:
||||||||||
|████████|
|████████|
|████████|
|████████|
|████████|
|█@██████|
|████████|
|████████|
||||||||||
4 4
||||||||||
|████████|
|████████|
|███@████|
|████████|
|████████|
|█@██████|
|████████|
|████████|
||||||||||
4 3
||||||||||
|████████|
|███@████|
|████████|
|████████|
|████████|
|█@██████|
|████████|
|████████|
||||||||||
4 2
マップの下の数字はプレーヤーの座標を表します。私は4,4で始まり(その0は索引付けされています)、2回上に移動します。表示されたマップは完全に間違っていますが、私は実際のゲームでそれをテストし、テキストの代わりにイメージを使って同じバグを得ました。どんな考えが起こっているの?
これはデバッグサービスではありません。しかし、面白い問題。運が良かった。 –