我目前正在用python编写一个推箱子游戏,我是该语言的新手,我正在使用列表字典来存储我的电路板,到目前为止我已经设法让我的角色移动(尽管没有到目前为止的任何检查。)
我现在正在尝试编写一个单独的函数,当字符位于框旁边且对方有空格时,框允许框移动位置。
我理解如何编写逻辑来执行这些检查和任务,但是我无法理解我如何能够从move_box()函数中的move_player()函数访问player_position,box_position和temp_board 。
有人可以给我一个如何做到这一点的想法吗?我尝试制作这些全局变量但是在move_box()函数中实例化它时不会定义它
这是我的代码:(我试图在python小提琴上设置它,但它不会让我分享链接或运行,抱歉)
import copy
# Level constructs
r_t_corner = "\u2510"
l_t_corner = "\u250C"
l_b_corner = "\u2514"
r_b_corner = "\u2518"
v_wall = "\u2502"
h_wall = "\u2500"
# Game variables
player = "@"
box = "\u2588"
goal = "."
direction = ""
# Level Dictionary set up
levels = [{'board': [[l_t_corner, h_wall, h_wall, h_wall, h_wall, h_wall, h_wall, h_wall, r_t_corner],
[v_wall, " ", " ", " ", " ", " ", " ", " ", v_wall],
[v_wall, " ", " ", " ", " ", " ", " ", goal, v_wall],
[v_wall, " ", " ", " ", " ", " ", " ", " ", v_wall],
[l_b_corner, h_wall, h_wall, h_wall, h_wall, h_wall, h_wall, h_wall, r_b_corner]],
'variables':{'player': [2, 2],
'boxes': [[1, 4]]}
}
]
game_turn = 0
def print_level():
level = levels[game_turn]['board']
variable_positions = levels[game_turn]['variables']
player_position = levels[game_turn]['variables']['player']
box_positions = levels[game_turn]['variables']['boxes']
temp_board = copy.deepcopy(level)
temp_board[player_position[0]][player_position[1]] = player
for i in range(len(box_positions)):
temp_board[box_positions[i][0]][box_positions[i][1]] = box
print('\n'.join(''.join(row) for row in temp_board)) # Test board printing ok
def move_player():
global direction
direction = (input("Please make a move using (w,a,s,d): "))
# Right
if direction == "d":
col = levels[game_turn]['variables']['player'][1] + 1
row = levels[game_turn]['variables']['player'][0]
if levels[game_turn]['board'][row][col] == h_wall or levels[game_turn]['board'][row][col] == v_wall:
levels[game_turn]['variables']['player'][1]
else:
levels[game_turn]['variables']['player'][1] += 1
# Left
if direction == "a":
col = levels[game_turn]['variables']['player'][1] - 1
row = levels[game_turn]['variables']['player'][0]
if levels[game_turn]['board'][row][col] == h_wall or levels[game_turn]['board'][row][col] == v_wall:
levels[game_turn]['variables']['player'][1]
else:
levels[game_turn]['variables']['player'][1] -= 1
# Up
if direction == "w":
row = levels[game_turn]['variables']['player'][0] - 1
col = levels[game_turn]['variables']['player'][1]
if levels[game_turn]['board'][row][col] == h_wall or levels[game_turn]['board'][row][col] == v_wall:
levels[game_turn]['variables']['player'][1]
else:
levels[game_turn]['variables']['player'][0] -= 1
# Down
if direction == "s":
row = levels[game_turn]['variables']['player'][0] + 1
col = levels[game_turn]['variables']['player'][1]
if levels[game_turn]['board'][row][col] == h_wall or levels[game_turn]['board'][row][col] == v_wall:
levels[game_turn]['variables']['player'][1]
else:
levels[game_turn]['variables']['player'][0] += 1
def move_boxes(temp_board, player_position):
# check what location player is, check location of box, push in row / column facing
if direction == "d":
if temp_board[player_position[0]][player_position[1]] == box and temp_board[player_position[0]][player_position[1]] == " ":
temp_board[player_position[0]][player_position[1]] = player
temp_board[player_position[0]][player_position[1] - 1] = box
for i in range(len(box_positions)):
if player_position == box_positions[i]:
levels[game_turn]['variable']['boxes'][i][1] -= 1
while True:
print_level()
move_player()
答案 0 :(得分:0)
您可以将它们设为全局(与direction
一样)或将它们作为函数之间的参数传递。
我不明白你把它们变成全球性的问题,但是你还没有表明你是如何尝试这样做的。