猫史档案馆


【Python作品分享】消除方块【作品秀】

用户:L0_0L0_0查看:0 回复:0 评论:0 创建时间:2021-04-01T21:54:19


【作品展示】

center_image

 

【作品介绍】

tkinter游戏

 

【作品源代码】

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

root = Tk(className='消除方块')
root.resizable(width=False, height=False)

#可能进入窗口没准备,以防万一,点击了新游戏,才能玩
print('进入窗口时,先要点击新游戏,才能开始玩。')

button = {}
score = 0
time = 3
over = True
the_different_color = None
the_different_color_tuple = ('light blue', 'light green', 'light yellow', 'light grey')
color_tuple = ('blue', 'green', 'yellow', 'grey')
the_highest_score = 0

def judge(x, y):
    global over, time, score, the_highest_score, the_different_color
    if over:
        messagebox.showerror('消除方块', '本局已结束。')
        return
    if button[x, y]['bg'] == the_different_color:
        score += 1
        score_text['text'] = '得分:' + str(score)
        time = 3
        time_text['text'] = '⏲:3'
        a = random.randint(0, 8)
        color_index = random.randint(0, 3)
        for i in range(9):
            the_different_color = the_different_color_tuple[color_index]
            if i == a:
                button[i%3, i//3]['bg'] = the_different_color
                button[i%3, i//3]['activebackground'] = the_different_color
            else:
                button[i%3, i//3]['bg'] = color_tuple[color_index]
                button[i%3, i//3]['activebackground'] = color_tuple[color_index]
    else:
        over = True
        if score > the_highest_score:
            the_highest_score = score
        messagebox.showerror('消除方块', '点错了。')

for i in range(9):
    b = Button(root, width=12, height=4, command=lambda x=i%3, y=i//3: judge(x, y))
    b.grid(column=i%3, row=i//3+1)
    button[i%3, i//3] = b

score_text = Label(root, width=24, height=4, text='得分:0', anchor='w')
score_text.grid(row=0, columnspan=2)
time_text = Label(root, width=12, height=4, text='⏲:3', anchor='w')
time_text.grid(column=2, row=0)

def show_hint():
    messagebox.showinfo('玩法', '点击所有方块中不同的方块即可得分,一定要在3秒之内点,否则游戏结束,点错了也会游戏结束。')

def new_game():
    global over, time, score, the_different_color
    over = False
    score = 0
    time = 3
    time_text['text'] = '⏲:3'
    score_text['text'] = '得分:0'
    a = random.randint(0, 8)
    color_index = random.randint(0, 3)
    for i in range(9):
        the_different_color = the_different_color_tuple[color_index]
        if i == a:
            button[i%3, i//3]['bg'] = the_different_color
            button[i%3, i//3]['activebackground'] = the_different_color
        else:
            button[i%3, i//3]['bg'] = color_tuple[color_index]
            button[i%3, i//3]['activebackground'] = color_tuple[color_index]

def show_the_highest_score():
    messagebox.showinfo('历史记录', '最高分:' + str(the_highest_score))

menubar = Menu(root)
gamemenu = Menu(menubar, tearoff=0)
gamemenu.add_command(label='玩法', command=show_hint)
gamemenu.add_command(label='新游戏', command=new_game)
gamemenu.add_separator()
gamemenu.add_command(label='历史记录', command=show_the_highest_score)
menubar.add_cascade(label='玩游戏', menu=gamemenu)
root.config(menu=menubar)

def time_eval():
    global over, time, the_highest_score
    if not over:
        time -= 1
        time_text['text'] = '⏲:' + str(time)
        if time == 0:
            over = True
            messagebox.showerror('消除方块', '点击超时。')
            if score > the_highest_score:
                the_highest_score = score
    root.after(1000, time_eval)

root.after(1000, time_eval)
root.mainloop()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页