用户:L0_0查看:0 回复:0 评论:0 创建时间:2021-03-15T20:49:55
作品代码:
import random
from tkinter import Tk, Menu, Label, Button, Toplevel, messagebox
root = Tk()
root.title('速算王者')
root.resizable(width=False, height=False)
the_highest_score_in_easy = 0
the_highest_score_in_secondary = 0
the_highest_score_in_hard = 0
the_highest_score_in_infernal = 0
def game_over():
global the_highest_score_in_easy, the_highest_score_in_hard, the_highest_score_in_infernal, the_highest_score_in_secondary
if hard_num == 10 and score > the_highest_score_in_easy:
the_highest_score_in_easy = score
if hard_num == 50 and score > the_highest_score_in_secondary:
the_highest_score_in_secondary = score
if hard_num == 100 and score > the_highest_score_in_hard:
the_highest_score_in_hard = score
if hard_num == 200 and score > the_highest_score_in_infernal:
the_highest_score_in_infernal = score
score = 0
score_text = Label(root, width=36, height=3, text='得分:0', anchor='w')
score_text.grid(columnspan=4)
easy_num = 0
hard_num = 10
formula = str(random.randint(easy_num, hard_num)) + random.choice(['+', '-', '*', '/']) + str(random.randint(easy_num + 1, hard_num))
if random.choice([True, False]):
answer = eval(formula)
else:
answer = eval(str(random.randint(easy_num, hard_num)) + random.choice(['+', '-', '*', '/']) + str(random.randint(easy_num + 1, hard_num)))
over = False
formula_text = Label(root, width=36, height=3, text=formula + '=' + str(answer), anchor='w')
formula_text.grid(columnspan=4, row=1)
time = 6
time_text = Label(root, width=18, height=3, text='倒计时:' + str(time))
time_text.grid(column=2, columnspan=2, row=3)
def tick():
global time, over, score, answer, formula
if not over:
if eval(formula) == answer:
score += 1
score_text['text'] = '得分:' + str(score)
formula = str(random.randint(easy_num, hard_num)) + random.choice(['+', '-', '*', '/']) + str(random.randint(easy_num + 1, hard_num))
if random.choice([True, False]):
answer = eval(formula)
else:
answer = eval(str(random.randint(easy_num, hard_num)) + random.choice(['+', '-', '*', '/']) + str(random.randint(easy_num + 1, hard_num)))
formula_text['text'] = formula + '=' + str(answer)
time = 6
time_text['text'] = '倒计时:' + str(time)
else:
over = True
messagebox.showerror('速算王者', '这道题做错了。')
game_over()
else:
messagebox.showerror('速算王者', '本局已结束。')
tick_button = Button(root, width=9, height=3, command=tick, text='√')
tick_button.grid(row=3)
def cross():
global time, over, score, answer, formula
if not over:
if eval(formula) != answer:
score += 1
score_text['text'] = '得分:' + str(score)
formula = str(random.randint(easy_num, hard_num)) + random.choice(['+', '-', '*', '/']) + str(random.randint(easy_num + 1, hard_num))
if random.choice([True, False]):
answer = eval(formula)
else:
answer = eval(str(random.randint(easy_num, hard_num)) + random.choice(['+', '-', '*', '/']) + str(random.randint(easy_num + 1, hard_num)))
formula_text['text'] = formula + '=' + str(answer)
time = 6
time_text['text'] = '倒计时:' + str(time)
else:
over = True
messagebox.showerror('速算王者', '这道题做错了。')
game_over()
else:
messagebox.showerror('速算王者', '本局已结束。')
cross_button = Button(root, width=9, height=3, command=cross, text='×')
cross_button.grid(column=1, row=3)
def show_hint():
messagebox.showinfo('玩法', '遇到一个算式就要在6秒内判断对错,题目做对了就会获得分,如果做错了或时间到了,游戏就结束了。')
def new_game(easy, hard):
global time, over, score, answer, formula, easy_num, hard_num
time = 6
over = False
score = 0
easy_num = easy
hard_num = hard
score_text['text'] = '得分:' + str(score)
time_text['text'] = '倒计时:' + str(time)
formula = str(random.randint(easy_num, hard_num)) + random.choice(['+', '-', '*', '/']) + str(random.randint(easy_num + 1, hard_num))
if random.choice([True, False]):
answer = eval(formula)
else:
answer = eval(str(random.randint(easy_num, hard_num)) + random.choice(['+', '-', '*', '/']) + str(random.randint(easy_num + 1, hard_num)))
formula_text['text'] = formula + '=' + str(answer)
def ask_replacement_difficuity(easy, hard):
global top
if messagebox.askquestion('提示', '此操作会放弃你目前的游戏,是否继续?') == 'yes':
menu.entryconfig(1, command=lambda easy=easy, hard=hard: new_game(easy, hard))
new_game(easy, hard)
top.destroy()
def replacement_difficuity():
global top
top = Toplevel()
top.title('更换难度')
Label(top, width=72, height=3, text='你想更换什么难度?').grid(columnspan=4)
Button(top, width=18, height=3, text='简单', command=lambda easy=0, hard=10: ask_replacement_difficuity(easy, hard)).grid(row=1)
Button(top, width=18, height=3, text='中等', command=lambda easy=0, hard=50: ask_replacement_difficuity(easy, hard)).grid(row=1, column=1)
Button(top, width=18, height=3, text='困难', command=lambda easy=0, hard=100: ask_replacement_difficuity(easy, hard)).grid(row=1, column=2)
Button(top, width=18, height=3, text='地狱', command=lambda easy=0, hard=200: ask_replacement_difficuity(easy, hard)).grid(row=1, column=3)
def show_the_highest_score():
messagebox.showinfo('历史纪录', '最高分:\n简单级别:' + str(the_highest_score_in_easy) + '\n中等级别:' + str(the_highest_score_in_secondary) + '\n困难级别:' + str(the_highest_score_in_hard) + '\n地狱级别:' + str(the_highest_score_in_infernal) + '\n\n整体纪录:' + str(max(the_highest_score_in_easy, the_highest_score_in_hard, the_highest_score_in_infernal, the_highest_score_in_secondary)))
menubar = Menu(root)
menu = Menu(root, tearoff=0)
menu.add_command(label='玩法', command=show_hint)
menu.add_command(label='新游戏', command=lambda easy=easy_num, hard=hard_num: new_game(easy, hard))
menu.add_command(label='更换难度', command=replacement_difficuity)
menu.add_separator()
menu.add_command(label='历史纪录', command=show_the_highest_score)
menubar.add_cascade(label='玩游戏', menu=menu)
root.config(menu=menubar)
def count_down():
global time, over
if not over:
time -= 1
time_text['text'] = '倒计时:' + str(time)
if time == 0:
over = True
messagebox.showinfo('速算王者', '答题超时。')
game_over()
root.after(1000, count_down)
root.after(1000, count_down)
root.mainloop()
效果预览:
