文章目录
- 功能介绍
- 三个文本文件存战绩
- 菜单界面
- 查看全部战绩
- 难度选择:
- 选择难度后进入游戏:
- 游戏后结束界面:
- 最佳战绩判断依据:
- 代码:
- 一些问题解决:
- 清屏
- 按任意键(回车键)继续的乱码问题
程序运行时,如果没有找到该文本文件,将自动创建。
以开始这把游戏,程序运行的时间为依据,以耗时最短,(运气最好!!)的为最佳战绩。
import random
import pyautogui
import sys
import os
import time
dif_op = '-1' # 存用户选择难度时的操作
def start_menu(): # 菜单
print('======== 菜单 =======\n'
'---------------------\n'
'| 1、开始游戏 |\n'
'| |\n'
'| 2、查看全部战绩 |\n'
'| |\n'
'| 3、清空全部战绩 |\n'
'| |\n'
'| 0、退出游戏 |\n'
'---------------------')
def dif_menu(): # 难度选择
print('======== 难度 =======\n'
'---------------------\n'
'| 1、简单 |\n'
'| |\n'
'| 2、中等 |\n'
'| |\n'
'| 3、困难 |\n'
'| |\n'
'| |\n'
'| 0、返回 |\n'
'| |\n'
'---------------------')
def over_menu(): # 猜对之后的选择
print('======== 结束 =======\n'
'---------------------\n'
'| 1、再玩一次 |\n'
'| |\n'
'| 2、查看历史战绩 |\n'
'| |\n'
'| 3、清空历史战绩 |\n'
'| |\n'
'| |\n'
'| 0、返回开始菜单 |\n'
'| |\n'
'| -1、退出游戏 |\n'
'| |\n'
'---------------------')
# 查看简单模式历史战绩
def look_easy():
if os.path.getsize("easy.txt") == 0:
print('============== 简单模式战绩为空 ====================')
print()
return
easy_record = [] # 存简单战绩
f = open("easy.txt")
for line in f.readlines():
curLine = line.strip().split(" ")
easy_record.append(curLine[0:7])
# 程序里读取每一行的时候,会有line.strip().split(“ ”)
# 这里,strip()是为了删除开始和结尾的空格符(仅限头和尾处)
# 后面的split(" ")是划分这一行里的每个元素
# 括号里“ ”表示的是以空格作为分割符,来分割这一行的元素(不是行与行之间的分割)
# print(easy_record)
best_easy = min(easy_record, key=lambda x: x[-1]) # 存最好战绩
# 用游戏耗时比较,列表每一行最后一个存的为耗时
# lambda匿名函数 x[-1]是指列表的倒数第一个元素
print('-------------- 简单模式历史战绩 ----------------------')
print(' 游戏时间 猜测次数 耗时 ')
for week, month, day, times, year, game_num, run_time in easy_record:
if best_easy[1] == month and best_easy[2] == day and best_easy[3] == times and best_easy[4] == year:
best_sign = 'MVP'
else:
best_sign = ''
print(
'{} {} {} {} {} {:<5s} {:<5s} {}'.format(week, month, day, times, year,
game_num, run_time, best_sign))
print('------------------------------------------------------')
print()
f.close()
# 查看中等模式历史战绩
def look_medium():
if os.path.getsize("medium.txt") == 0:
print('============== 中等模式战绩为空 ====================')
print()
return
medium_record = [] # 存中等战绩
f = open("medium.txt")
for line in f.readlines():
curLine = line.strip().split(" ")
medium_record.append(curLine[0:7])
best_medium = min(medium_record, key=lambda x: x[-1])
print('-------------- 中等模式历史战绩 ----------------------')
print(' 游戏时间 猜测次数 耗时 ')
for week, month, day, times, year, game_num, run_time in medium_record:
if best_medium[1] == month and best_medium[2] == day and best_medium[3] == times and best_medium[4] == year:
best_sign = 'MVP'
else:
best_sign = ''
print(
'{} {} {} {} {} {:<5s} {:<5s} {}'.format(week, month, day, times, year,
game_num, run_time, best_sign))
print('------------------------------------------------------')
print()
f.close()
# 查看困难模式历史战绩
def look_hard():
if os.path.getsize("hard.txt") == 0:
print('============== 困难模式战绩为空 ====================')
print()
return
hard_record = [] # 存困难战绩
f = open("hard.txt")
for line in f.readlines():
curLine = line.strip().split(" ")
hard_record.append(curLine[0:7])
best_hard = min(hard_record, key=lambda x: x[-1])
print('-------------- 困难模式历史战绩 ----------------------')
print(' 游戏时间 猜测次数 耗时 ')
for week, month, day, times, year, game_num, run_time in hard_record:
if best_hard[1] == month and best_hard[2] == day and best_hard[3] == times and best_hard[4] == year:
best_sign = 'MVP'
else:
best_sign = ''
print(
'{} {} {} {} {} {:<5s} {:<5s} {}'.format(week, month, day, times, year,
game_num, run_time, best_sign))
print('------------------------------------------------------')
print()
f.close()
def game_easy(): # 简单模式游戏
num = random.randint(1, 10) # num为预设的数
n = 0 # N为猜数次数
print('注意:该数的范围在 1 ~ 10 之间')
while True:
begin_time = time.perf_counter() # 开始时间
n += 1
m = eval(input('请输入你想猜的数字:'))
if m == num:
print('恭喜你,猜中了!正确答案就是 {} !'.format(num))
end_time = time.perf_counter() # 结束时间
run_time = round(end_time - begin_time, 3) # 保留三位小数
print('共猜 {} 次'.format(n))
print('用时 ', run_time, ' 秒')
file = open("easy.txt", "a", encoding="utf-8")
file.write('{}'.format(time.asctime()))
file.write(' {}'.format(n))
file.write(' {}\n'.format(run_time))
file.close()
over()
break
elif m < num:
print('---------你猜的太小了!')
else:
print('---------你猜的太大了!')
def game_medium(): # 中等模式
num = random.randint(1, 100) # num为预设的数
n = 0 # N为猜数次数
print('注意:该数的范围在 1 ~ 100 之间')
while True:
begin_time = time.perf_counter()
n += 1
m = eval(input('请输入你想猜的数字:'))
if m == num:
print('恭喜你,猜中了!正确答案就是 {} !'.format(num))
end_time = time.perf_counter()
run_time = round(end_time - begin_time, 3)
print('共猜 {} 次'.format(n))
print('用时 ', run_time, ' 秒')
file = open("medium.txt", "a", encoding="utf-8")
file.write('{}'.format(time.asctime()))
file.write(' {}'.format(n))
file.write(' {}\n'.format(run_time))
file.close()
over()
break
elif m < num:
print('---------你猜的太小了!')
else:
print('---------你猜的太大了!')
def game_hard(): # 困难模式
num = random.randint(1, 1000) # num为预设的数
n = 0 # N为猜数次数
print('注意:该数的范围在 1 ~ 1000 之间')
while True:
begin_time = time.perf_counter()
n += 1
m = eval(input('请输入你想猜的数字:'))
if m == num:
print('恭喜你,猜中了!正确答案就是 {} !'.format(num))
end_time = time.perf_counter()
run_time = round(end_time - begin_time, 3)
print('共猜 {} 次'.format(n))
print('用时 ', run_time, ' 秒')
file = open("hard.txt", "a", encoding="utf-8")
file.write('{}'.format(time.asctime()))
file.write(' {}'.format(n))
file.write(' {}\n'.format(run_time))
file.close()
over()
break
elif m < num:
print('---------你猜的太小了!')
else:
print('---------你猜的太大了!')
# 清空简单模式历史战绩
def clear_easy():
f = open("easy.txt", "w")
f.truncate()
f.close()
# 清空中等模式历史战绩
def clear_medium():
f = open("medium.txt", "w")
f.truncate()
f.close()
# 清空困难模式历史战绩
def clear_hard():
f = open("hard.txt", "w")
f.truncate()
f.close()
# 结束选择
def over():
print()
os.system('pause')
while True:
pyautogui.hotkey("Alt", "c")
over_menu()
op = input('请输入您的选择:')
pyautogui.hotkey("Alt", "c")
if op == '1': # 再玩一次
if dif_op == '1': # 简单模式
game_easy()
elif dif_op == '2': # 中等模式
game_medium()
elif dif_op == '3': # 困难模式
game_hard()
elif op == '2': # 查看历史战绩
if dif_op == '1': # 简单模式
look_easy() # 查看简单模式战绩
elif dif_op == '2': # 中等模式
look_medium() # 查看中等模式战绩
elif dif_op == '3': # 困难模式
look_hard() # 查看困难模式战绩
print()
os.system('pause')
pyautogui.hotkey("Alt", "c")
elif op == '3': # 清空历史战绩
if dif_op == '1': # 清除简单模式战绩
clear_easy()
elif dif_op == '2': # 清除中等模式战绩
clear_medium()
elif dif_op == '3': # 清除困难模式战绩
clear_hard()
print('已清除历史战绩!')
print()
os.system('pause')
pyautogui.hotkey("Alt", "c")
elif op == '0': # 返回开始菜单
pyautogui.hotkey("Alt", "c")
guess_num()
break
elif op == '-1': # 退出游戏
print('欢迎下次使用!')
sys.exit()
else:
print('输入有误,请重新选择!')
# 难度选择
def start_game():
while True:
dif_menu() # 难度菜单
global dif_op
dif_op = input('请输入你的选择:')
pyautogui.hotkey("Alt", "c")
if dif_op == '1': # 简单
game_easy()
elif dif_op == '2': # 中等
game_medium()
elif dif_op == '3': # 困难
game_hard()
elif dif_op == '0': # 返回
guess_num()
else:
print('输入有误,请重新输入')
def guess_num():
while True:
start_menu()
op = input('请输入您的选择:')
pyautogui.hotkey("Alt", "c")
if op == '1':
start_game()
elif op == '2': # 查看全部历史战绩
look_easy()
look_medium()
look_hard()
print()
os.system('pause')
pyautogui.hotkey("Alt", "c")
elif op == '3': # 清空全部历史战绩
clear_easy()
clear_medium()
clear_hard()
print('已清除全部历史战绩!')
print()
os.system('pause')
pyautogui.hotkey("Alt", "c")
elif op == '0': # 退出游戏
print('欢迎下次使用!')
sys.exit()
else:
print('输入有误,请重新选择!')
guess_num()
安装pyautogui包,设置清空的热键,以Alt+c为例,自己喜欢什么键弄什么键,别冲突就行。
解决方法:把编码格式从 UTF-8改为GBK