Python实现敲击木鱼积累功德小项目

 更新时间:2022年11月15日 15:43:03   作者:DY.memory  
最近大家都很流行用手机敲击电子木鱼积累功德,这在很多短视频中也常常见到。本文将用Python实现这一效果,感兴趣的小伙伴可以了解一下

前言:前几天上课闲着没事写了一个python敲击木鱼积累功德的小项目,当时纯粹就是写着玩,回顾一下鼠标事件的东西还记不记得,发现这个博客的点赞和收藏量还挺高的,我当时也没有把它当回事,后面也有很多人问怎么实现的,想让我再添加一些其他功能!

随着点赞量和关注量不断增高,我又重新看了一下博客,感觉过于简单了,实在不配当当时python热榜的第一,所以我又把代码给稍微大改了一下,在原来总体实现的基础上添加了如下功能!

我们先看原来的效果:

1:实现了点击鼠标会弹出切换功德+1的surface界面,鼠标松开回复原先界面!

2:随着鼠标按键的按下同时也会伴随木鱼敲击的空灵的声音!

本次更新之后的功能有:

1:点击鼠标按键会出现功德+1和累计敲击多少次,积累了多少功德的新画面

2:优化了声音的play,降噪处理!

3:增加较强的互动性能,在不断的积累功德的过程中,会不定时随机出现意想不到的的互动效果,极大程度增加了该程序的趣味性!

ps:下次更新可能会在半个月后了,届时会使用tk的模块添加登录注册,以及网络编程的使用用户功德的统计排行榜!最近期末,还请谅解!

好了,话不多说,直接上代码:

import pygame
from locale import *
pygame.init()
pygame.mixer.init()
screen=pygame.display.set_mode((700,500))
pygame.display.set_caption("木鱼功德")

img1=pygame.image.load("images/muyuluck1.jpg")
# img2=pygame.image.load("images/muyulucky2.png")
img2=pygame.image.load("images/zan.jpg")
img3=pygame.image.load("images/qw.png")
rect1=img1.get_rect()

muyulucky = pygame.mixer.Sound('sound/muyu.WAV')
muyulucky.set_volume(0.4)
if pygame.mouse.get_focused():
            # 获取光标位置,2个值
    ball_x, ball_y = pygame.mouse.get_pos()
screen.blit(img1, (-180, -100))
count=0
f = pygame.font.SysFont('华文楷体',50)
f1 = pygame.font.SysFont('华文楷体',30)
# 生成文本信息,第一个参数文本内容;第二个参数,字体是否平滑;
# 第三个参数,RGB模式的字体颜色;第四个参数,RGB模式字体背景颜色;
# text = f.render("功德+1",True,(255,0,0),(0,0,0))
# text1=f1.render("今日积累功德"+str(count)+"次",True,(255,0,0),(0,0,0))
#获得显示对象的rect区域坐标
# textRect =text.get_rect()
# text1Rect =text1.get_rect()
# 设置显示对象居中
# textRect.topleft = (30,30)
# text1Rect.topleft = (450,30)
flag = False
while True:
    for event in pygame.event.get():
        if pygame.Rect.collidepoint(rect1, (ball_x, ball_y)) and event.type==pygame.MOUSEBUTTONDOWN:
            muyulucky.play()
            flag=True
            count = count + 1
            text = f.render("功德+1", True, (255, 0, 0), (0, 0, 0))
            textRect = text.get_rect()
            textRect.topleft = (30, 30)
            text1 = f1.render("今日积累功德" + str(count) + "次", True, (255, 0, 0), (0, 0, 0))
            text1Rect = text1.get_rect()
            text1Rect.topleft = (450, 30)
            screen.blit(text1, text1Rect)
            screen.blit(text,textRect)
            if count==8:
                f2 = pygame.font.SysFont("华文楷体", 25)
                text2 = f2.render("今日积累功德8次,去表白应该不会被拒绝太难堪哦", True, (255, 0, 20))
                text2Rect = text.get_rect()
                text2Rect.topleft = (60, 150)
                screen.blit(text2, text2Rect)
            if count==10:
                text = f.render("功德+1", True, (255, 0, 0), (0, 0, 0))
                textRect = text.get_rect()
                textRect.topleft = (30, 30)
                text1 = f1.render("今日积累功德" + str(count) + "次", True, (255, 0, 0))
                text1Rect = text1.get_rect()
                text1Rect.topleft = (450, 30)
                screen.blit(img1, (-180, -100))
                screen.blit(text1, text1Rect)
                screen.blit(text, textRect)
            if count==20:
                f2 = pygame.font.SysFont("华文楷体", 25)
                text3 = f2.render("手速这么快干嘛,这是敲木鱼积功德,不是你dfj", True, (230, 90, 80))
                text3Rect = text.get_rect()
                text3Rect.topleft = (60, 150)
                screen.blit(text3, text3Rect)
            if count==22:
                text = f.render("功德+1", True, (255, 0, 0), (0, 0, 0))
                textRect = text.get_rect()
                textRect.topleft = (30, 30)
                text1 = f1.render("今日积累功德" + str(count) + "次", True, (255, 0, 0), (0, 0, 0))
                text1Rect = text1.get_rect()
                text1Rect.topleft = (450, 30)
                screen.blit(img1, (-180, -100))
                screen.blit(text1, text1Rect)
                screen.blit(text, textRect)
            if count==28:
                f2 = pygame.font.SysFont("华文楷体", 25)
                text3 = f2.render("tmd,我看你不是敲木鱼,是泄火吧", True, (255, 200, 20))
                text3Rect = text.get_rect()
                text3Rect.topleft = (60, 150)
                screen.blit(text3, text3Rect)
            if count==30:
                text = f.render("功德+1", True, (255, 0, 0), (0, 0, 0))
                textRect = text.get_rect()
                textRect.topleft = (30, 30)
                text1 = f1.render("今日积累功德" + str(count) + "次", True, (255, 0, 0), (0, 0, 0))
                text1Rect = text1.get_rect()
                text1Rect.topleft = (450, 30)
                screen.blit(img1, (-180, -100))
                screen.blit(text1, text1Rect)
                screen.blit(text, textRect)
            if count==40:
                screen.blit(img2, (-210,10))
            if count==41:
                text = f.render("功德+1", True, (255, 0, 0), (0, 0, 0))
                textRect = text.get_rect()
                textRect.topleft = (30, 30)
                text1 = f1.render("今日积累功德" + str(count) + "次", True, (255, 0, 0), (0, 0, 0))
                text1Rect = text1.get_rect()
                text1Rect.topleft = (450, 30)
                screen.blit(img1, (-180, -100))
                screen.blit(text1, text1Rect)
                screen.blit(text, textRect)
            if count==50:
                f2 = pygame.font.SysFont("华文楷体", 25)
                text3 = f2.render("今日功德累计50次了,小熊后台奖励你一只女朋友!", True, (255, 0, 0))
                text3Rect = text.get_rect()
                text3Rect.topleft = (60, 150)
                screen.blit(text3, text3Rect)
                screen.blit(img3, (-300, 0))
            if count==51:
                text = f.render("功德+1", True, (255, 0, 0), (0, 0, 0))
                textRect = text.get_rect()
                textRect.topleft = (30, 30)
                text1 = f1.render("今日积累功德" + str(count) + "次", True, (255, 0, 0), (0, 0, 0))
                text1Rect = text1.get_rect()
                text1Rect.topleft = (450, 30)
                screen.blit(img1, (-180, -100))
                screen.blit(text1, text1Rect)
                screen.blit(text, textRect)



            pygame.display.flip()
        if pygame.Rect.collidepoint(rect1, (ball_x, ball_y)) and event.type==pygame.MOUSEBUTTONUP:
            flag = False

            text = f.render("功德+1", True, (0, 0, 0), (0, 0, 0))
            textRect = text.get_rect()
            textRect.topleft = (30, 30)
            screen.blit(text, textRect)
            if count==40:
                screen.blit(img2, (-210, 10))
            if count==50:
                f2 = pygame.font.SysFont("华文楷体", 25)
                text3 = f2.render("功德积累是好事,凡事有个度!", True, (255, 0, 0))
                text4 = f2.render("小熊后台检测到你今日功德累计50次,看张照片放松一下吧!!", True, (255, 0, 0))
                text3Rect = text.get_rect()
                text4Rect = text.get_rect()
                text3Rect.topleft = (60, 150)
                text4Rect.topleft = (0, 180)
                screen.blit(img3, (-300, 0))
                screen.blit(text3, text3Rect)
                screen.blit(text4, text4Rect)



            pygame.display.flip()
        if event.type==pygame.QUIT:
            import mouse
            pygame.quit()
    pygame.display.flip()

截图:

到此这篇关于Python实现敲击木鱼积累功德小项目的文章就介绍到这了,更多相关Python敲击木鱼内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python WebSocket长连接心跳与短连接的示例

    Python WebSocket长连接心跳与短连接的示例

    这篇文章主要介绍了Python WebSocket长连接心跳与短连接的示例,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-11-11
  • 利用python制作俄罗斯方块详细图文教程

    利用python制作俄罗斯方块详细图文教程

    俄罗斯方块是一款经典的游戏,它可以用多种编程语言来实现,这篇文章主要给大家介绍了关于利用python制作俄罗斯方块的详细图文教程,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-10-10
  • Python Web框架之Django框架Model基础详解

    Python Web框架之Django框架Model基础详解

    这篇文章主要介绍了Python Web框架之Django框架Model基础,结合实例形式分析了Django框架Model模型相关使用技巧与操作注意事项,需要的朋友可以参考下
    2019-08-08
  • Python2包含中文报错的解决方法

    Python2包含中文报错的解决方法

    这篇文章主要给大家介绍了关于Python2包含中文报错的解决方法,文中通过示例代码详细介绍了解决的方法,相信会对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07
  • python读取hdfs上的parquet文件方式

    python读取hdfs上的parquet文件方式

    这篇文章主要介绍了python读取hdfs上的parquet文件方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • python对两个数组进行合并排列处理的两种方法

    python对两个数组进行合并排列处理的两种方法

    最近遇到数组合并问题,以此记录解决方法,供大家参考学习,下面这篇文章主要给大家介绍了关于python对两个数组进行合并排列处理的两种方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • Python+matplotlib实现循环作图的方法详解

    Python+matplotlib实现循环作图的方法详解

    这篇文章主要为大家介绍了Python如何利用matplotlib实现循环作图的,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习学习
    2022-06-06
  • 使用python本地部署DeepSeek运行时报错 OSError: [WinError 193] %1 不是有效的 Win32 应用程序的问题及解决方法

    使用python本地部署DeepSeek运行时报错 OSError: [WinError 193] %1 不是

    文章介绍了在本地使用Python部署DeepSeek时遇到的OSError: [WinError 193] 错误,通过检查错误信息,发现与numpy版本有关,解决方法是卸载并重新安装numpy,最终,问题得到解决,感兴趣的朋友跟随小编一起看看吧
    2025-02-02
  • 一文秒懂Python中的字符串

    一文秒懂Python中的字符串

    这篇文章主要介绍了一文秒懂Python中的字符串,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Python+OpenCV实现角度测量的示例代码

    Python+OpenCV实现角度测量的示例代码

    本文介绍如何使用python语言实现角度测量,程序包括鼠标选点、直线斜率计算、角度计算三个子程序和一个主程序,感兴趣的可以了解一下
    2022-03-03

最新评论