首頁 > 軟體

Python實現敲擊木魚積累功德小專案

2022-11-17 14:00:51

前言:前幾天上課閒著沒事寫了一個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敲擊木魚內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com