Python|快过年了, 不给自己积一下功德吗?

引言

哈喽, 大家好啊, 今天都1月18日了, 掐指一算, 再有三四天就过年了欸。那么我们不应该敲个木鱼积累功德,祝自己在新的一年里过得更好吗?但是, 现在大雪天的,怎么可能跑去山里的寺庙向和尚借个木鱼, 在网上买的话不得要个十天半个月的时间才发到货?其实我们可以用Python做一个前段时间很火的敲木鱼积累功德小游戏。接下来跟我一起制作吧,Let's Go!

敲木鱼小游戏的制作

我们是用pygame做敲木鱼小游戏呢,还是别的呢?其实大家在看别人写的用Pygame写的小游戏时,就可以发现,用Pygame写的游戏代码里面的各种响应事件的函数非常多, 就连窗口的X键都要来专门写代码写它被点击时的响应。我们其实也可以用tkinter来做小游戏, 事件有bind或bind_all参数直接调用函数,不像Pygame内样还要写if语句判断。

我是用tkinter.Label.bind("<Button-1>", 函数)来写木鱼被点击时的事件,之所以不用buuton是因为按钮被按下时它会塌下去,非常难看。

我们新建一个文件夹,在新建的文件夹里先手动创建一个名为mart.txt的文件, 打开mart.txt文件,给第一行输入0(不要写完以后换行),然后保存并退出。再创建一个main.py文件,我们代码就写在main.py文件里, 代码我会上传在百度网盘,大家可以自己下载到本地。

打开main.py文件,我们先创建一个tkinter窗口。

import tkinter as tk
root = tk.Tk()
root.title('只因木鱼')
root.config(bg='black')
width = 650
height = 450

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

xx = int(screen_width / 2 - width / 2)
yy = int(screen_height / 2 - height / 2)
size = '{}x{}+{}+{}'.format(width, height, xx, yy)
root.geometry(size)
root.mainloop()

然后写函数:

def com():
    global gongde_liang
    global shiji
    if gongde_liang == 9999:
        messagebox.showinfo('只因木鱼', '加油!再按一\n下就1万功德了')
    gongde_liang += 1
    shiji += 1
    text = tk.Label(root, text='施主今日功德加一', bg='black', fg='white', font=('楷体', 18))
    y = 150
    text.place(relx=0.7, y=y)
    gongde_label.config(text=f'功德量: {gongde_liang}')
    gongde_label.update()
    while y > 42:
        y -= 12
        time.sleep(0.05)
        text.place(relx=0.7, y=y)
        text.update()
    text.config(text='', fg='black')


def thread_it(fc):
    tt = threading.Thread(target=fc)
    tt.setDaemon(True)
    tt.start()


def cnt(self):
    if self == 'f':
        pass
    thread_it(com)


def bey():
    t = messagebox.askyesno('只因木鱼', f'施主今日积德 {shiji}分!\n功德真高!是否退出?')
    if t:
        with open('mark.txt', 'w') as f:
            f.write(f'{str(gongde_liang)}')
            sys.exit()
    if not t:
        pass

再导入所需库,展示图片,绑定事件,下面的就是所有代码啦:

import sys
import time
import tkinter as tk
from PIL import Image, ImageTk
import threading
from tkinter import messagebox

root = tk.Tk()
shiji = 0
root.title('只因木鱼')
root.config(bg='black')
with open('mark.txt', 'r') as w:
    gongde_liang = int(w.read())


def com():
    global gongde_liang
    global shiji
    if gongde_liang == 9999:
        messagebox.showinfo('只因木鱼', '加油!再按一\n下就1万功德了')
    gongde_liang += 1
    shiji += 1
    text = tk.Label(root, text='施主今日功德加一', bg='black', fg='white', font=('楷体', 18))
    y = 150
    text.place(relx=0.7, y=y)
    gongde_label.config(text=f'功德量: {gongde_liang}')
    gongde_label.update()
    while y > 42:
        y -= 12
        time.sleep(0.05)
        text.place(relx=0.7, y=y)
        text.update()
    text.config(text='', fg='black')


def thread_it(fc):
    tt = threading.Thread(target=fc)
    tt.setDaemon(True)
    tt.start()


def cnt(self):
    if self == 'f':
        pass
    thread_it(com)


def bey():
    t = messagebox.askyesno('只因木鱼', f'施主今日积德 {shiji}分!\n功德真高!是否退出?')
    if t:
        with open('mark.txt', 'w') as f:
            f.write(f'{str(gongde_liang)}')
            sys.exit()
    if not t:
        pass


label_img = Image.open('wooden_fish.png')
label_img_tk = ImageTk.PhotoImage(label_img.resize((403, 268)))
muyu = tk.Label(image=label_img_tk, bg='black', width=265, height=210)
muyu.place(relx=0.3, y=110)
muyu.bind('<Button-1>', cnt)
gongde_label = tk.Label(root, text=f'功德量: {gongde_liang}', bg='black', fg='#C4AD37', font=('汉仪瘦金书简', 19))
gongde_label.place(relx=0.7, y=20)
width = 650
height = 450

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

xx = int(screen_width / 2 - width / 2)
yy = int(screen_height / 2 - height / 2)
size = '{}x{}+{}+{}'.format(width, height, xx, yy)
root.protocol('WM_DELETE_WINDOW', bey)
root.resizable(False, False)
root.attributes('-alpha', 0.9)
root.geometry(size)
root.iconbitmap('icon.ico')
root.mainloop()

运行结果:

代码获取

百度网盘提取链接:https://pan.baidu.com/s/16zJdjWBeCT3tuCesEE2X-Q

提取码:gd60(我第一次用百度网盘,所以可能我没设置提取码,没提取码直接下载就可以了)

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值