williamcc
191 天前
```
#############################################################LV2############################################################
'''
看来你们已经基本掌握了条件语句和循环语句的基本用法了
利用今天所学完成最终测试吧
程序约精炼越好哦😀
得分越高越好哦😁
跳转到 153 行,开始吧😎!!!
'''
import random
import sys
#######################################!!!!!中间这段别改哦!!!!!去 145 行哦!!!##############################################################################
import time # 导入时间模块
####################################
x = 1 # 横向坐标默认值
y = 1 # 纵向坐标默认值
step_number = 0 # 记录行走步数的变量
star_number = 0 # 记录吃糖豆得分
start = time.time() # 获取开始时的时间戳
####################################13 21 4
map_list = [
[" ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@",
"@", "@", "@", "@", "@", "@", "@", "@"],
["=>", "Y", " ", " ", "@", "@", " ", " ", " ", " ", " ", " ", "@", " ", " ", "机器人创新协会", " ", "@"],
[" ", "@", " ", " ", "@", "@", "@", "@", " ", " ", "@", " ", "@", " ", " ", " ", "第一次培训课", " ", " ", "@"],
[" ", "@", "O", " ", " ", " ", " ", "@", " ", "@", "@", " ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@",
"@", "@", "@", "@", "@", "@", "@", "@"],
[" ", "@", "@", "@", "@", "@", " ", "@", " ", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", "@", "@", "@", "@", "@", "@"],
[" ", "@", " ", " ", " ", "@", " ", "@", " ", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@",
" ", " ", "@", " ", " ", " ", "O", "@"],
[" ", "@", " ", "@", " ", "@", " ", "@", " ", " ", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@",
" ", " ", "@", " ", "@", "@", "@", "@"],
[" ", "@", " ", "@", " ", " ", " ", "@", " ", " ", " ", " ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@",
" ", "@", "@", " ", " ", " ", " ", "@"],
[" ", "@", " ", "@", "@", "@", "@", "@", " ", " ", "@", " ", "@", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", "@", " ", "@"],
[" ", "@", "O", " ", " ", " ", " ", " ", " ", " ", "@", " ", "@", "O", " ", " ", " ", " ", "@", "@", "@", "@", "@",
" ", "@", "@", "@", " ", "@", " ", " ", "=>"],
[" ", "@", " ", "@", "@", "@", "@", "@", "@", "@", "@", " ", "@", "@", "@", "@", "@", " ", "@", "@", "@", "@", "@",
" ", "@", "@", "@", " ", "@", "@", "@"],
[" ", "@", " ", " ", " ", " ", " ", "@", " ", "@", "@", " ", "@", "@", "@", "@", "@", " ", "@", "@", "@", "O", " ",
" ", " ", " ", " ", " ", "@", "@", "@"],
[" ", "@", "@", "@", "@", "@", " ", "@", " ", "@", "@", " ", "@", " ", " ", " ", " ", " ", " ", "@", "@", "@", "@",
"@", "@", "@", "@", " ", "@", " ", "@"],
[" ", "@", " ", " ", " ", " ", "O", "@", " ", "@", "@", " ", "@", "@", " ", "@", "@", "@", " ", " ", " ", " ", " ",
" ", "O", " ", "@", " ", " ", " ", "@"],
[" ", "@", " ", "@", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", "@", "@", "@", "@", "@", "@", " ", "@",
"@", "@", "@", "@", "@", "@", " ", "@"],
[" ", "@", "O", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@", "@", " ", " ", " ", " ", " ", "O", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", "@"],
[" ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@",
"@", "@", "@", "@", "@", "@", "@", "@"]]
starx = [13, 14, 15, 16, 17, 18, 19, 20, 21]
sx = random.sample(starx, 5)
map_list[4][sx[0]] = "O"
map_list[5][sx[1]] = "O"
map_list[4][sx[2]] = "O"
map_list[6][sx[3]] = "O"
map_list[6][sx[4]] = "O"
# 更新地图
def up_map():
# 打印小标题并指定打印区域的文字以及背景颜色
print("\033[200;30;94m ---------lv3---挑战一下------")
for i, values in enumerate(map_list): # 遍历二维列表中的 18 个子列表
for j in range(len(values)): # 遍历子列表中的元素
# 打印每个子列表中的所有元素,并且不换行打印
print(map_list[i][j], end="")
print("") # 每打印一个子列表所有元素,换行一次
print("得分", star_number)
print("\033[0m", end="") # 背景色结束位置
def godown():
global x, y, step_number, map_list, star_number
time.sleep(0.1)
if map_list[y + 1][x] == "O":
star_number = star_number + 1
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() # 更新地图
jiance() # 检查位置
surround() # 周围检查
def goup():
global x, y, step_number, map_list, star_number
time.sleep(0.1)
if map_list[y - 1][x] == "O":
star_number = star_number + 1
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() # 更新地图
jiance() # 检查位置
surround() # 周围检查
def goright():
global x, y, step_number, map_list, star_number
time.sleep(0.1)
if map_list[y][x + 1] == "O":
star_number = star_number + 1
if map_list[y][x + 1] != "@": # 判断行走位置不是墙时,@(表示墙)
map_list[y][x] = " " # 将小人原来的位置设置为空
x += 1 # 修改 Y 坐标位置
map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
step_number += 1 # 修改小人移动步数
else:
print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
up_map() # 更新地图
jiance() # 检查位置
surround() # 周围检查
def goleft():
global x, y, step_number, map_list, star_number
time.sleep(0.1)
if map_list[y][x - 1] == "O":
star_number = star_number + 1
if map_list[y][x - 1] != "@": # 判断行走位置不是墙时,@(表示墙)
map_list[y][x] = " " # 将小人原来的位置设置为空
x -= 1 # 修改 Y 坐标位置
map_list[y][x] = "Y" # 将移动后的位置设置为 Y (表示小人)
step_number += 1 # 修改小人移动步数
else:
print('\033[31m 碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示
up_map() # 更新地图
jiance() # 检测位置
surround() # 周围检查
def jiance():
time.sleep(0.1)
global x, y, start
if x == 31 and y == 9:
print("\033[31m 恭喜你呀!完成了 lv3 的训练!满分 15 分你拿到了%d 分😏\033[0m" % (star_number))
print("\033[31m 今天的培训到这就结束喽😘\033[0m")
print("\033[31m 共计行走了", step_number, "步!\033[0m")
print("\033[31m 共计用时", float(time.time() - start), "秒!\033[0m")
sys.exit(0)
def surround():
global x, y, step_number, map_list, shang, xia, zuo, you
shang = map_list[y - 1][x]
xia = map_list[y + 1][x]
zuo = map_list[y][x - 1]
you = map_list[y][x + 1]
up_map() # 更新模拟地图
###############################################路径规划##################################################
```