猫史档案馆


【Python作品分享】猜单词【作品秀】

用户:L0_0L0_0查看:0 回复:0 评论:0 创建时间:2021-03-27T09:39:39


【作品展示】

center_image

 

【作品介绍】

tkinter游戏

 

【作品源代码】

import random
from tkinter import Tk, Menu, Label, Button, messagebox

root = Tk()
root.title('猜单词')
root.resizable(width=False, height=False)

clue_list = list('?????')
clue_dict = {}

word_list = ['about', 'actor', 'after', 'again', 'agree', 'ahead', 'alike', 'alive', 'allow', 'alone', 'aloud', 'amaze', 'angel', 'anger', 'angle', 'apple', 'April', 'arrow', 'awake', 'beach', 'begin', 'bench', 'black', 'blank', 'blind', 'bread', 'break', 'bring', 'brown', 'build', 'candy', 'catch', 'chair', 'chess', 'child', 'class', 'clean', 'clear', 'climb', 'clock', 'cloud', 'color', 'cough', 'could', 'count', 'dance', 'eight', 'enjoy', 'enter', 'drink', 'earth', 'dress', 'dirty', 'error', 'fifty', 'first', 'float', 'floor', 'forty', 'fruit', 'glass', 'glove', 'grass', 'green', 'guess', 'happy', 'heavy', 'hello', 'input', 'juice', 'label', 'learn', 'leave', 'lemon', 'level', 'lunch', 'March', 'match', 'mouse', 'music', 'never', 'north', 'party', 'piano', 'place', 'plane', 'point', 'press', 'print', 'right', 'river', 'round', 'score', 'seven', 'sleep', 'south', 'space', 'start', 'third', 'three', 'while']
letter_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
the_secret_word = random.choice(word_list)
lives = 20
over = False

game_count = 1
win_count = 0

lives_label = Label(root, width=45, height=3, text='♥' * 20, fg='red', anchor='w')
lives_label.grid(columnspan=5)

for i in range(5):
    clue = Label(root, width=9, height=3, text=clue_list[i])
    clue.grid(column=i, row=1)
    clue_dict[i] = clue

input_box = Label(root, width=51, height=3, text='', bg='grey', anchor='w')
input_box.grid(row=2, columnspan=5)

def input_letter(text):
    input_box['text'] += text

b_dict = {}

for i in range(26):
    b = Button(root, width=9, height=3, text=letter_list[i])
    b['command'] = lambda text=b['text']: input_letter(text)
    b.grid(column=i%5, row=i//5+3)
    b_dict[i] = b

def backspace():
    input_box['text'] = input_box['text'][0:len(input_box['text'])-1]

Button(root, width=9, height=3, text='→', command=backspace).grid(column=1, row=8)

def shift():
    for i in b_dict:
        if b_dict[i]['text'] in letter_list:
            b_dict[i]['text'] = b_dict[i]['text'].upper()
            b_dict[i]['command'] = lambda text=b_dict[i]['text']: input_letter(text)
        else:
            b_dict[i]['text'] = b_dict[i]['text'].lower()
            b_dict[i]['command'] = lambda text=b_dict[i]['text']: input_letter(text)

Button(root, width=9, height=3, text='⇧', command=shift).grid(column=2, row=8)

def enter():
    global over, lives, win_count
    if over:
        messagebox.showerror('错误', '本局已结束。')
        return
    if input_box['text'] == the_secret_word:
        messagebox.showinfo('提示', '猜对了。')
        over = True
        win_count += 1
    elif len(input_box['text']) == 5:
        lives -= 1
        lives_label['text'] = '♥' * lives
        if lives == 0:
            messagebox.showerror('错误', '猜错了,正确单词是:' + the_secret_word)
            over = True
    elif len(input_box['text']) != 1:
        messagebox.showerror('错误', '一次只能猜一个字母或一个单词。')
    elif input_box['text'] in the_secret_word:
        index = 0
        while index < len(the_secret_word):
            if input_box['text'] == the_secret_word[index]:
                clue_list[index] = input_box['text']
            index += 1
    else:
        lives -= 1
        lives_label['text'] = '♥' * lives
        if lives == 0:
            messagebox.showerror('错误', '猜错了,正确单词是:' + the_secret_word)
            over = True
    for i in clue_dict:
        clue_dict[i]['text'] = clue_list[i]
    if '?' not in clue_list:
        messagebox.showinfo('提示', '猜对了。')
        win_count += 1
        over = True

Button(root, width=19, height=3, command=enter, text='↲').grid(column=3, row=8, columnspan=2)

def new_game():
    global over, lives, clue_list, game_count, the_secret_word
    clue_list = list('?????')
    for i in clue_dict:
        clue_dict[i]['text'] = '?'
    game_count += 1
    the_secret_word = random.choice(word_list)
    lives = 20
    over = False
    lives_label['text'] = '♥' * 20
    menu.entryconfig(5, state='normal')

def show_hint():
    messagebox.showinfo('玩法', '目标是猜单词,取胜有两种方法:1.猜出整个单词,2.猜出所有字母,一次只能猜一个单词或一个字母,否则会出错,猜错了生命值减少1,用完了生命游戏就结束了。\n小提示:你可以点提示按钮,但一局只能用1次。')

def show_record():
    messagebox.showinfo('统计', '游戏次数:' + str(game_count) + '\n获胜次数:' + str(win_count) + '\n获胜几率:' + str(win_count / game_count * 100) + '%')

def show_dict():
    messagebox.showinfo('词典', word_list)

def hint():
    if over:
        messagebox.showerror('错误', '本局已结束。')
        return
    else:
        if messagebox.askquestion('提示', '一局只能提示一次,是否提示?') == 'yes':
            a = random.randint(1, 5)
            messagebox.showinfo('提示', '第' + str(a) + '个字母是' + str(the_secret_word[a - 1]))
            menu.entryconfig(5, state='disabled')

def reset():
    global win_count, game_count
    if messagebox.askquestion('提示', '此操作将会重置你的所有记录,是否继续?') == 'yes':
        game_count = 0
        win_count = 0
        new_game()

menubar = Menu(root)
menu = Menu(menubar, tearoff=0)
menu.add_command(label='新游戏', command=new_game)
menu.add_separator()
menu.add_command(label='玩法', command=show_hint)
menu.add_command(label='统计', command=show_record)
menu.add_separator()
menu.add_command(label='提示', command=hint)
menu.add_separator()
menu.add_command(label='词典', command=show_dict)
menu.add_separator()
menu.add_command(label='重置', command=reset)
menu.add_command(label='退出', command=root.quit)
menubar.add_cascade(label='菜单', menu=menu)
root.config(menu=menubar)

root.mainloop()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页