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

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

我们先看原来的效果:

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

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

本次更新之后的功能有:

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

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

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

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

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

import pygamefrom 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=0f = 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 = Falsewhile 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()

截图: