python实现迷宫小游戏(附源码 简单易懂)

简介: python实现迷宫小游戏(附源码 简单易懂)

需要源码请点赞关注收藏后评论区留言~~~

接下来用python实现一个控制台的迷宫小游戏  游戏规则如下

输入exit退出游戏

输入8为向上走

输入5为向下走

输入4为向左走

输入6为向右走

游戏地图如下

Y即为玩家,输入对应数字后可以变换位置,当撞墙时位置不变

部分源码如下

 

import time     # 导入时间模块
# 模拟迷宫地图的二维列表
map_list=[
["  ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"],
["=>","Y"," "," ","@","@"," "," "," "," "," "," ","@"," "," "," ","@","@"," "," "," ","@"," ","@"," ","@"," ","@"," ","@","@"],
["  ","@"," "," ","@","@","@"," ","@"," ","@"," ","@","@","@"," "," "," "," ","@"," ","@"," ","@","@","@"," ","@","@","@","@"],
["  ","@"," ",""@","@","@"],
["  ","@","@","@","@","@"," ","@"," ","@","@"," "," ","@"," ","@","@","@","@","@"," ","@","@"," "," "," "," ","@","@","@","@"],
["  ","@"," "," "," ","@"," ","@","@","@","@","@"," ","@"," "," "," ","@","@","@"," ","@"," ","@"," ","@"," "," "," "," ","@"],
["  ","@"," ","@"," ","@"," ","@","@"," ","@","@","@"," "," ","@","@"," "," "," "," ","@","@","@"," ","@"," ","@","@","@","@"],
["  ","@"," ","@"," "," "," ","@","@"," "," "," ","@","@","@","@","@","@","@","@","@","@","@"," ","@","@","@"," "," "," ","@"],
["  ","@"," ","@","@","@","@","@","@"," ","@"," ","@","@","@","@","@"," "," "," "," "," "," "," "," "," "," "," ","@"," ","@"],
["  ","@"," "," "," "," "," "," "," "," ","@"," ","@"," "," "," "," "," ","@","@","@","@","@"," ","@","@","@"," ","@"," "," ","=>"],
["  ","@"," ","@","@","@","@","@","@","@","@"," ","@","@","@","@","@"," ","@","@","@","@","@"," ","@","@","@"," ","@","@","@"],
["  ","@"," "," "," "," "," ","@"," ","@","@"," ","@","@","@","@","@"," ","@","@","@"," "," "," "," "," "," "," ","@","@","@"],
["  ","@","@","@","@","@"," ","@"," ","@","@"," ","@"," "," "," "," "," "," ","@","@","@","@","@","@","@","@"," ","@"," ","@"],
["  ","@"," "," "," "," "," ","@"," ","@","@"," ","@","@"," ","@","@","@"," "," "," "," "," "," "," "," ","@"," "," "," ","@"],
["  ","@"," ","@"," ","@","@","@"," "," "," "," "," "," "," ","@","@","@","@","@","@"," ","@","@","@","@","@","@","@"," ","@"],
["  ","@"," "," "," "," "," "," "," ","@","@"," ","@","@"," "," "," "," "," "," ","@"," "," "," "," "," "," "," "," "," ","@"],
["  ","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"]]
# 更新地图
def up_map():
    # 打印小标题并指定打印区域的文字以及背景颜色
    print("\033[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("\033[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("\033[31m恭喜您!走出了迷宫!\033[0m")
            print("\033[31m共计行走了",step_number,"步!\033[0m")
            print("\033[31m共计用时",int(time.time()-start),"秒!\033[0m")
            break
        print("\033[31m请输入行走的方向(输入exit将退出游戏!)\033[0m")
        # 获取输入的行走方向8为上、5为下、4为左、6为右
        direction = input("\033[31m8为上、5为下、4为左、6为右:\033[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('\033[31m碰到墙壁!\033[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('\033[31m碰到墙壁!\033[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('\033[31m碰到墙壁!\033[0m')
            up_map()  # 更新地图
        # 当用户输入退出命令,跳出循环
        elif (direction == "exit"):
            print("游戏退出。")
            break
相关文章
|
2月前
|
Python
用python进行视频剪辑源码
这篇文章提供了一个使用Python进行视频剪辑的源码示例,通过结合moviepy和pydub库来实现视频的区间切割和音频合并。
59 2
|
1月前
|
自然语言处理 Java 编译器
为什么要看 Python 源码?它的结构长什么样子?
为什么要看 Python 源码?它的结构长什么样子?
25 2
|
1月前
|
Python
源码解密 Python 的 Event
源码解密 Python 的 Event
38 1
|
1月前
|
数据采集 前端开发 Python
Python pygame 实现游戏 彩色 五子棋 详细注释 附源码 单机版
Python pygame 实现游戏 彩色 五子棋 详细注释 附源码 单机版
68 0
|
3月前
|
Ubuntu Linux 数据安全/隐私保护
使用Cython库包对python的py文件(源码)进行加密,把python的.py文件生成.so文件并调用
本文介绍了在Linux系统(Ubuntu 18.04)下将Python源代码(`.py文件`)加密为`.so文件`的方法。首先安装必要的工具如`python3-dev`、`gcc`和`Cython`。然后通过`setup.py`脚本使用Cython将`.py文件`转化为`.so文件`,从而实现源代码的加密保护。文中详细描述了从编写源代码到生成及调用`.so文件`的具体步骤。此方法相较于转化为`.pyc文件`提供了更高的安全性。
133 2
|
3月前
|
测试技术 Python
python自动化测试中装饰器@ddt与@data源码深入解析
综上所述,使用 `@ddt`和 `@data`可以大大简化写作测试用例的过程,让我们能专注于测试逻辑的本身,而无需编写重复的测试方法。通过讲解了 `@ddt`和 `@data`源码的关键部分,我们可以更深入地理解其背后的工作原理。
50 1
|
3月前
|
算法 关系型数据库 程序员
程序员必备技能)基于Python的鼠标与键盘控制实战扩展与源码
这篇文章是关于如何使用Python的`pyautogui`库来控制鼠标和键盘进行各种操作,包括移动、点击、滚轮控制以及键盘的按键和快捷键输出,并介绍了如何结合图像处理和计算机视觉技术来扩展其应用。
|
1天前
|
存储 Python
Python编程入门:打造你的第一个程序
【10月更文挑战第39天】在数字时代的浪潮中,掌握编程技能如同掌握了一门新时代的语言。本文将引导你步入Python编程的奇妙世界,从零基础出发,一步步构建你的第一个程序。我们将探索编程的基本概念,通过简单示例理解变量、数据类型和控制结构,最终实现一个简单的猜数字游戏。这不仅是一段代码的旅程,更是逻辑思维和问题解决能力的锻炼之旅。准备好了吗?让我们开始吧!
|
1天前
|
机器学习/深度学习 人工智能 TensorFlow
人工智能浪潮下的自我修养:从Python编程入门到深度学习实践
【10月更文挑战第39天】本文旨在为初学者提供一条清晰的道路,从Python基础语法的掌握到深度学习领域的探索。我们将通过简明扼要的语言和实际代码示例,引导读者逐步构建起对人工智能技术的理解和应用能力。文章不仅涵盖Python编程的基础,还将深入探讨深度学习的核心概念、工具和实战技巧,帮助读者在AI的浪潮中找到自己的位置。