请用python写出有十个关卡的推箱子小游戏
时间: 2023-12-25 07:04:14 浏览: 122
好的,以下是一个简单的Python实现的十个关卡的推箱子小游戏:
```python
# 推箱子小游戏
# 地图
map_1 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# @ #",
"# #",
"# #",
"# #",
"# #",
"# #",
"############"
]
map_2 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"# #",
"# #",
"# #",
"############"
]
map_3 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"# #",
"# #",
"# #",
"############"
]
map_4 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"# #",
"# #",
"# #",
"# #",
"############"
]
map_5 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"# #",
"# #",
"# #",
"############"
]
map_6 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"# #",
"# #",
"# #",
"############"
]
map_7 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# @ #",
"# $ #",
"# #",
"# #",
"# #",
"############"
]
map_8 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"# #",
"# #",
"############"
]
map_9 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"# #",
"# #",
"############"
]
map_10 = [
"############",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"# #",
"# #",
"############"
]
# 地图列表
maps = [map_1, map_2, map_3, map_4, map_5, map_6, map_7, map_8, map_9, map_10]
# 玩家坐标
player_x = 0
player_y = 0
# 目标坐标
target_x = 0
target_y = 0
# 箱子坐标列表
box_x_list = []
box_y_list = []
# 当前地图
current_map = []
# 初始化游戏
def init_game(map_index):
global player_x, player_y, target_x, target_y, box_x_list, box_y_list, current_map
current_map = maps[map_index]
player_x, player_y, target_x, target_y, box_x_list, box_y_list = get_map_info(current_map)
show_map(current_map)
# 获取地图信息
def get_map_info(map_data):
player_x = 0
player_y = 0
target_x = 0
target_y = 0
box_x_list = []
box_y_list = []
for i in range(len(map_data)):
for j in range(len(map_data[i])):
if map_data[i][j] == "@":
player_x = j
player_y = i
elif map_data[i][j] == "$":
box_x_list.append(j)
box_y_list.append(i)
elif map_data[i][j] == ".":
target_x = j
target_y = i
return player_x, player_y, target_x, target_y, box_x_list, box_y_list
# 显示地图
def show_map(map_data):
for i in range(len(map_data)):
print(map_data[i])
# 移动箱子
def move_box(direction):
global player_x, player_y, target_x, target_y, box_x_list, box_y_list, current_map
dx, dy = get_direction(direction)
for i in range(len(box_x_list)):
if player_x + dx == box_x_list[i] and player_y + dy == box_y_list[i]:
if is_valid_move(box_x_list[i] + dx, box_y_list[i] + dy):
box_x_list[i] += dx
box_y_list[i] += dy
player_x += dx
player_y += dy
update_map()
return True
else:
return False
return False
# 移动玩家
def move_player(direction):
global player_x, player_y, target_x, target_y, box_x_list, box_y_list, current_map
dx, dy = get_direction(direction)
if is_valid_move(player_x + dx, player_y + dy):
player_x += dx
player_y += dy
update_map()
return True
else:
return move_box(direction)
# 更新地图
def update_map():
global player_x, player_y, target_x, target_y, box_x_list, box_y_list, current_map
for i in range(len(current_map)):
current_map[i] = current_map[i].replace("@", " ")
current_map[i] = current_map[i].replace("$", " ")
current_map[i] = current_map[i].replace(".", " ")
current_map[player_y] = current_map[player_y][:player_x] + "@" + current_map[player_y][player_x+1:]
for i in range(len(box_x_list)):
current_map[box_y_list[i]] = current_map[box_y_list[i]][:box_x_list[i]] + "$" + current_map[box_y_list[i]][box_x_list[i]+1:]
if all_boxes_on_targets():
print("You win!")
return True
else:
show_map(current_map)
return False
# 检查所有箱子是否都在目标位置
def all_boxes_on_targets():
global player_x, player_y, target_x, target_y, box_x_list, box_y_list, current_map
for i in range(len(box_x_list)):
if current_map[box_y_list[i]][box_x_list[i]] != ".":
return False
return True
# 获取移动方向
def get_direction(direction):
dx = 0
dy = 0
if direction == "up":
dy = -1
elif direction == "down":
dy = 1
elif direction == "left":
dx = -1
elif direction == "right":
dx = 1
return dx, dy
# 判断移动是否有效
def is_valid_move(x, y):
global player_x, player_y, target_x, target_y, box_x_list, box_y_list, current_map
if current_map[y][x] == "#":
return False
for i in range(len(box_x_list)):
if x == box_x_list[i] and y == box_y_list[i]:
return False
return True
# 开始游戏
def start_game():
map_index = 0
init_game(map_index)
while True:
direction = input("Please input direction (up/down/left/right):")
if direction in ["up", "down", "left", "right"]:
move_player(direction)
elif direction == "exit":
break
else:
print("Invalid input")
if all_boxes_on_targets():
map_index += 1
if map_index < len(maps):
init_game(map_index)
else:
print("Congratulations! You have completed all levels!")
break
if __name__ == "__main__":
start_game()
```
这是一个比较简单的实现,地图和关卡数量都比较少,可以根据需要进行扩展和改进。
阅读全文