博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 游戏(船只寻宝)
阅读量:5167 次
发布时间:2019-06-13

本文共 5809 字,大约阅读时间需要 19 分钟。

1. 游戏思路和流程图

实现功能:船只在可以在大海上移动打捞宝藏,船只可以扫描1格范围内的宝藏(后续难度,可以调整扫描范围,可以调整前进的格数)

游戏流程图

 

2. 使用模块和游戏提示

import random ships_coordinates={} #船只坐标 def game_info():     '''游戏提示'''     print('欢迎来到船只寻宝游戏')     print('W(上) A(左) S(下) D(右) 来移动')     print('船(B)的探测范围是一格,移动次数为九次')

3. 双选验证

def Double_choice(a,b,hint):     '''双选择验证函数     :param a 第一个选项值     :param b 第二个选项值     :param hint 选项信息     :return 返回输入值'''     choice=''     while choice.lower() !=a and choice.lower() !=b:         print(hint)         choice=input()     return choice

4. 用户输入验证

def letter_move_judge(letter,map_x,map_y):     '''移动字母限制     :param letter 用户输入字母     :param map_x 地图x坐标     :param map_y 地图y坐标     :param 返回用户输入字符'''     ships_x=ships_coordinates['x']     ships_y =ships_coordinates['y']     while True:         if len(letter)!=1:             print('请输入一个字母')         elif letter.lower() not in 'w a s d':             print('请输入W or A or S or D')         else:             if letter.lower()=='w' and ships_y-1 <0:                 print('超过界限了')             elif letter.lower()=='a' and ships_x-1 <0:                 print('超过界限了')             elif letter.lower()=='s' and ships_y+1 >map_y-1:                 print(ships_coordinates['y']-1)                 print('超过界限了')             elif letter.lower()=='d' and ships_x+1 >map_x-1:                 print('超过界限了')             else:                 break         letter=input()     return letter.lower()

5. 海水列表 

def get_seawater(x,y):     '''生成海水列表     :param x 每列的行数     :param y 行数     :return 返回海水列表'''     seawater_map=[['~' for i in range(x)] for j in range(y)]     return seawater_map

6. 宝藏坐标列表 

def treasure_location(x,y):     '''随机宝藏坐标     :param x 每列的行数     :param y 行数     :return 返回宝藏列表'''     treasure_list=[]     count=5     while count:         treasure_x=random.randint(0,x-1)         treasure_y = random.randint(0, y-1)         if (treasure_x,treasure_y) not in treasure_list:             treasure_list.append((treasure_x,treasure_y))             count-=1     return treasure_list

7.  海水界面显示

def seawater_info(seawater_map):     '''打印海水     :param seawater_map 海水列表'''     for i in seawater_map:         print(' '.join(i))

8. 初始化游戏开始船只位置

def restarting_init(seawater_map):     '''初始化船只的位置'''     x=ships_coordinates['x']     y=ships_coordinates['y']     seawater_map[y][x]='B'     return seawater_map

9.  扫描船只范围内宝藏

def treasure_tab(treasure_list,seawater_map):     '''宝藏标记     :param treasure_list 宝藏位置列表     :param seawater_map 海水地图     :return 返回海水地图'''     ships_x=ships_coordinates['x']     ships_y=ships_coordinates['y']     treasure='$'     scope=1 #扫描格数需要改良     for i in range(1,scope+1):         if (ships_x-i, ships_y) in treasure_list:             seawater_map[ships_y][ships_x - i]=treasure         if (ships_x-i,ships_y+i) in treasure_list:             seawater_map[ships_y + i][ships_x - i] = treasure         if (ships_x-i, ships_y - i) in treasure_list:             seawater_map[ships_y-i][ships_x - i] = treasure         if (ships_x + i, ships_y) in treasure_list:             seawater_map[ships_y][ships_x + i] = treasure         if (ships_x+i,ships_y+i) in treasure_list:             seawater_map[ships_y+i][ships_x + i] = treasure         if (ships_x+i,ships_y-i) in treasure_list:             seawater_map[ships_y-i][ships_x + i] = treasure         if (ships_x, ships_y +i) in treasure_list:             seawater_map[ships_y+i][ships_x] = treasure         if (ships_x, ships_y - i) in treasure_list:             seawater_map[ships_y-i][ships_x] = treasure     return seawater_map

10.  判断船只地点是否有宝藏

def treasure_judge(treasure_list):     '''判断坐标是否有宝藏     :param treasure_list 宝藏地图     :return True 代表此地有宝藏'''     if (ships_coordinates['x'], ships_coordinates['y']) in treasure_list:         return True     return False

11.  移动船只

def restarting(ships_remove,seawater_map):     '''改变船的坐标     :param ships_remove 移动的位置     :param seawater_map 海水地图'''     ships_x=ships_coordinates['x']     ships_y=ships_coordinates['y']     number=1     seawater_map[ships_y][ships_x]='~'     if ships_remove=='w':         ships_y-=1     elif ships_remove=='a':         ships_x-=1     elif ships_remove=='s':         ships_y += 1     elif ships_remove=='d':         ships_x += 1     seawater_map[ships_y][ships_x] = 'B'     ships_coordinates['x']=ships_x     ships_coordinates['y']=ships_y     return seawater_map

12.  游戏核心

def game_start():     '''游戏核心'''     map_x=9     map_y=5     ships_coordinates['x']=map_x // 2     ships_coordinates['y'] = map_y // 2     treasure_count=0     count=9     seawater_map=get_seawater(map_x,map_y)     treasure_list=treasure_location(map_x,map_y)     seawater_map=restarting_init(seawater_map)     while count:         if treasure_judge(treasure_list):             treasure_count+=1             treasure_list.remove((ships_coordinates['x'],ships_coordinates['y']))             print('成功打捞宝藏..(%d个)'%treasure_count)         if treasure_count==5:             break         seawater_map=treasure_tab(treasure_list,seawater_map)         seawater_info(seawater_map)         print('请输入移动的位置(机会%d)'%count)         ships_remove=letter_move_judge(input(),map_x,map_y)         seawater_map=restarting(ships_remove,seawater_map)         count -= 1     seawater_info(seawater_map)     if count:         print('你寻找到了所有宝藏,提前结束了寻宝,今天收获%d个宝藏'%treasure_count)     else:         print('天黑了船只返回了船坞,今天收获%d个宝藏'%treasure_count)         #结束条件宝藏打捞完毕或次数用光

13. 游戏外壳

def game_shell():     '''外壳程序'''     game_info()  # 游戏开始提示     game_start()     while True:         message='你想在玩一次吗(Y or N)'         again_flag=Double_choice('y','n',message)         if again_flag=='n':             break         game_start()

14. 运行游戏

game_shell()

 

python学习途径

本游戏参考书本 <<Python游戏编程快速上手>>

友情推荐:  猿人学Python【 】 由一群工作十余年的老程序员结合实际工作经验所写的Python教程。

转载于:https://www.cnblogs.com/gm332211/p/10731729.html

你可能感兴趣的文章
求出斐波那契数组
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
查看>>
.NET CLR基本术语
查看>>
ubuntu的home目录下,Desktop等目录消失不见
查看>>
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
2017.3.24上午
查看>>
Python-常用模块及简单的案列
查看>>
LeetCode 159. Longest Substring with At Most Two Distinct Characters
查看>>
基本算法概论
查看>>
jquery动态移除/增加onclick属性详解
查看>>
JavaScript---Promise
查看>>
暖暖的感动
查看>>
Java中的日期和时间
查看>>
Django基于admin的stark组件创建(一)
查看>>
抛弃IIS,利用FastCGI让Asp.net与Nginx在一起
查看>>
C. Tanya and Toys_模拟
查看>>
springboot jar包运行中获取资源文件
查看>>
基于FPGA实现的高速串行交换模块实现方法研究
查看>>