欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 游戏(船只寻宝)

程序员文章站 2022-04-19 23:19:05
1. 游戏思路和流程图 实现功能:船只在可以在大海上移动打捞宝藏,船只可以扫描1格范围内的宝藏(后续难度,可以调整扫描范围,可以调整前进的格数) 游戏流程图 2. 使用模块和游戏提示 3. 双选验证 4. 用户输入验证 5. 海水列表 6. 宝藏坐标列表 7. 海水界面显示 8. 初始化游戏开始船只 ......

1. 游戏思路和流程图

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

游戏流程图

python 游戏(船只寻宝)

 

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教程。