如何用python画帆船_python 游戏(船只寻宝)
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画帆船_python 游戏(船只寻宝)相关推荐
- 如何用python画帆船_python学习笔记6——文件操作来生成船只侧面图像的描述文件...
最近在做船只检测的工作需要大量的正样本,之前的一些样本各种形态的船只都有,这一次训练分类器希望使用只含船只侧面的图像,这样检测船的侧面的成功率可能会提高一点.这样就需要大量的船只侧面图像,并生成描述文 ...
- python画城堡_Python游戏设计—Part1
原标题:Python游戏设计-Part1 程序演示图:兔子要保卫自己的城堡家园(4个城堡),右侧方随机出现罐来袭击,兔子需要瞄准并射击罐,从而保卫自己的城堡. [开始]安装Python 如果你想在Wi ...
- 如何用python画饼图_Python中的五颜六色的饼状图!(一)
[1x00]方法描述 matplotlib.pyplot.pie() 方法用于绘制饼状图. 基本语法:matplotlib.pyplot.pie( x[, explode=None, labels=N ...
- 如何用python画长方形_Python 画矩形
1 importpygame, sys2 3 pygame.init()4 screen = pygame.display.set_mode([640, 480])#显示对象 5 #[640, 480 ...
- 如何用python画长方形_python opencv 画矩形跟老齐学Python之用Python计算
一提到计算机,当然现在更多人把她叫做电脑,这两个词都是指computer.不管什么,只要提到她,普遍都会想到她能够比较快地做加减乘除,甚至乘方开方等.乃至于,有的人在口语中区分不开计算机和计算器. 那 ...
- 如何用python画长方形_Python如何绘制长方形
用python绘制长方形的基本步骤: 首先先下载安装好python程序. 在我们自己的电脑上找到python 的IDLE工具. 2.然后打开IDLE,新建一个文件,命名为test1.py 相关推荐:& ...
- 怎么用python画个电脑_python语言还是java如何用python画爱心
用python绘制爱心的基本步骤如下: 002pc.com对<python语言还是java如何用python画爱心>总结来说,为我们学习Python很实用. 首先先下载安装好python程 ...
- python语言画心_python语言还是java如何用python画爱心
用python绘制爱心的基本步骤如下: 002pc.com对<python语言还是java如何用python画爱心>总结来说,为我们学习Python很实用. 首先先下载安装好python程 ...
- 如何用 Python 画一个纸飞机?| 原力计划
[CSDN编者按]如果你是一个80后,可能对于卓依婷的歌曲<纸飞机>并不陌生.歌词里说:"飞在风里的纸飞机,载满我对你的情和意.飞到那思念的另一边,诉说我心中的痴迷." ...
最新文章
- CentOS 5.5 挂载windows ntfs 文件系统
- 从 Windows 切换到 Mac,不能错过这9条Tips
- 获取init程序的调试信息和uevent的调试信息需要打开的两个宏
- 【LeetCode】2. Add Two Numbers
- nginx向响应内容中追加内容(ngx_http_addition_module模块)
- 剖析SpringSession的redis原理
- java file 方法_JAVA中File的常用方法
- seaborn—sns.scatterplot绘制散点图
- Everthing搜索神器,工作利器
- Ps 2022 版新增功能及改进
- cad 三点绘制斜矩形
- 机器学习-分类Classification
- 【寒江雪】Go实现组合模式
- matlab求第二类曲面积分,第二型曲面积分的参数形式计算
- 【本人秃顶程序员】过年了,给亲朋好友解释“啥是程序员”
- 3D Tiles Next
- 前后期绑定Excel/Word对象的应用
- 九、Unity编辑器开发之Gizmos
- VUE3 之 组件传参
- 机器人项目研发笔记(一)