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

这篇具有很好参考价值的文章主要介绍了python实现迷宫小游戏(附源码 简单易懂)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

输入exit退出游戏

输入8为向上走

输入5为向下走

输入4为向左走

输入6为向右走

游戏地图如下

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

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

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

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

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

部分源码如下

 文章来源地址https://www.toymoban.com/news/detail-505301.html

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

到了这里,关于python实现迷宫小游戏(附源码 简单易懂)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python项目分享 python走迷宫小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 走迷宫小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年01月16日
    浏览(40)
  • python项目分享 - python走迷宫小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 走迷宫小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年02月02日
    浏览(46)
  • C语言版---2048小游戏,简单易懂,看过就会

    游戏截图           相信认真看完我的代码注释的小伙伴,一定感慨,老夫活这么久,查阅代码无数,从未见有如此注释简单,清晰的代码(说笑^_^),之所以写这么详细,是因为查代码的小伙伴很大程度上不会写,或者自己写的时候出bug,不知道哪里错了,想看看人家的,

    2024年02月09日
    浏览(42)
  • 【超详细教学】Python制作迷宫小游戏教程

    我将为你提供一个简单的迷宫小游戏的Python代码,并解释每个部分的作用,可以为你在工作之余可以堂堂正正的摸会小鱼。 运用工具: Python、Pygame、PyCharm 点击领取Python工具助学资料 在这个迷宫游戏中,我们需要先确定迷宫的大小。我们可以通过定义一个常量来表示迷宫的

    2024年02月11日
    浏览(39)
  • python小游戏 滑雪小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 滑雪小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 我们利用python制作

    2024年02月04日
    浏览(45)
  • python小游戏毕设 2048小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 2048小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: https://

    2024年02月12日
    浏览(47)
  • python小游戏毕设 扫雷小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 扫雷小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: https:

    2024年02月06日
    浏览(42)
  • python小游戏毕设 消消乐小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 消消乐小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年02月09日
    浏览(49)
  • python小游戏毕设 吃豆人小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 吃豆人小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年02月10日
    浏览(49)
  • python小游戏毕设 飞扬的小鸟小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 飞扬的小鸟小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取:

    2024年02月08日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包