python实现迷宫小游戏(附源码 简单易懂)
import time # 导入时间模块 # 模拟迷宫地图的二维列表 map_list=[ [" ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"], ["=>","Y"," "," ","@","@"," "," "," "," "," "," ","@"," "," "," ","@","@"," "," "," ","@"," ","@"," ","@"," ","@"," ","@","@"], [" ","@"," "," ","@","@","@"," ","@"," ","@"," ","@","@","@"," "," "," "," ","@"," ","@"," ","@","@","@"," ","@","@","@","@"], [" ","@"," ",""@","@","@"], [" ","@","@","@","@","@"," ","@"," ","@","@"," "," ","@"," ","@","@","@","@","@"," ","@","@"," "," "," "," ","@","@","@","@"], [" ","@"," "," "," ","@"," ","@","@","@","@","@"," ","@"," "," "," ","@","@","@"," ","@"," ","@"," ","@"," "," "," "," ","@"], [" ","@"," ","@"," ","@"," ","@","@"," ","@","@","@"," "," ","@","@"," "," "," "," ","@","@","@"," ","@"," ","@","@","@","@"], [" ","@"," ","@"," "," "," ","@","@"," "," "," ","@","@","@","@","@","@","@","@","@","@","@"," ","@","@","@"," "," "," ","@"], [" ","@"," ","@","@","@","@","@","@"," ","@"," ","@","@","@","@","@"," "," "," "," "," "," "," "," "," "," "," ","@"," ","@"], [" ","@"," "," "," "," "," "," "," "," ","@"," ","@"," "," "," "," "," ","@","@","@","@","@"," ","@","@","@"," ","@"," "," ","=>"], [" ","@"," ","@","@","@","@","@","@","@","@"," ","@","@","@","@","@"," ","@","@","@","@","@"," ","@","@","@"," ","@","@","@"], [" ","@"," "," "," "," "," ","@"," ","@","@"," ","@","@","@","@","@"," ","@","@","@"," "," "," "," "," "," "," ","@","@","@"], [" ","@","@","@","@","@"," ","@"," ","@","@"," ","@"," "," "," "," "," "," ","@","@","@","@","@","@","@","@"," ","@"," ","@"], [" ","@"," "," "," "," "," ","@"," ","@","@"," ","@","@"," ","@","@","@"," "," "," "," "," "," "," "," ","@"," "," "," ","@"], [" ","@"," ","@"," ","@","@","@"," "," "," "," "," "," "," ","@","@","@","@","@","@"," ","@","@","@","@","@","@","@"," ","@"], [" ","@"," "," "," "," "," "," "," ","@","@"," ","@","@"," "," "," "," "," "," ","@"," "," "," "," "," "," "," "," "," ","@"], [" ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"]] # 更新地图 def up_map(): # 打印小标题并指定打印区域的文字以及背景颜色 print(" 33[1;30;42m ----------小迷宫-----------") for i,values in enumerate(map_list): # 遍历二维列表中的18个子列表 for j in range(len(values)): # 遍历子列表中的元素 # 打印每个子列表中的所有元素,并且不换行打印 print(map_list[i][j], end="") print("") # 每打印一个子列表所有元素,换行一次 print(" 33[0m",end="") # 背景色结束位置 if __name__ == '__main__': x = 1 # 横向坐标默认值 y = 1 # 纵向坐标默认值 step_number = 0 # 记录行走步数的变量 up_map() # 更新模拟地图 start = time.time() # 获取开始时的时间戳 while True: # 判断当小人走到迷宫出口时,打印胜利文字,跳出循环 if x == 30 and y == 10: print(" 33[31m恭喜您!走出了迷宫! 33[0m") print(" 33[31m共计行走了",step_number,"步! 33[0m") print(" 33[31m共计用时",int(time.time()-start),"秒! 33[0m") break print(" 33[31m请输入行走的方向(输入exit将退出游戏!) 33[0m") # 获取输入的行走方向8为上、5为下、4为左、6为右 direction = input(" 33[31m8为上、5为下、4为左、6为右: 33[0m") # 当用户输入5,控制人物向下走 if (direction == '5'): if map_list[y+1][x] != "@": # 判断行走位置不是墙时,@(表示墙) map_list[y][x] = " " # 将小人原来的位置设置为空 y += 1 # 修改Y坐标位置 map_list[y][x] = "Y" # 将移动后的位置设置为Y(表示小人) step_number+=1 # 修改小人移动步数 else: print(' 33[31m碰到墙壁! 33[0m') # 当用户输入的位置是墙时,做出提示 up_map() # 更新地图 # 当用户输入6,控制人物向右走 elif (direction == '6'): if map_list[y][x + 1] != "@": map_list[y][x] = " " x += 1 map_list[y][x] = "Y" step_number += 1 # 修改小人移动步数 else: print(' 33[31m碰到墙壁! 33[0m') up_map() #更新地图 # 当用户输入8,控制人物向上走 elif (direction == '8'): if map_list[y - 1][x] != "@": map_list[y][x] = " " y -= 1 物向左走 elif (direction == '4'): if map_list[y][x - 1] != "@": map_list[y][x] = " " x -= 1 map_list[y][x] = "Y" step_number += 1 # 修改小人移动步数 else: print(' 33[31m碰到墙壁! 33[0m') up_map() # 更新地图 # 当用户输入退出命令,跳出循环 elif (direction == "exit"): print("游戏退出。") break
相关知识
python实现迷宫小游戏(附源码 简单易懂)
【python小游戏设计】走迷宫游戏设计与实现
手机python编程师编程小游戏教程
用python写一个安卓小游戏
python简单小游戏代码
看广告赚佣金小游戏app软件开发 源码搭建
看广告玩小游戏盈利app开发 源码搭建
游戏软件开发,小游戏代码,游戏源码网站
游戏程序开发,手游源码,小游戏代码
游戏程序开发,游戏源代码,网页小游戏源码
推荐资讯
- 1老六爱找茬美女的烦恼怎么过- 4999
- 2博德之门3黄金雏龙法杖怎么得 4867
- 3《大侠立志传》剿灭摸金门任务 4312
- 4代号破晓官方正版角色介绍 4023
- 5赛马娘锻炼到底的伙伴支援卡事 3802
- 6闪烁之光11月兑换码大全20 3774
- 7原神原海异种刷怪路线-原神原 3547
- 8爆梗找茬王厕所特工怎么通关- 3542
- 9《我的世界》领地删除指令是什 3440
- 10原神开局星落湖怎么出去 原神 3426