基于Python實現(xiàn)敲擊木魚積累功德效果
示例代碼
import pygame
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")
rect1=img1.get_rect()
rect2=img2.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, (-150, -100))
while True:
for event in pygame.event.get():
if pygame.Rect.collidepoint(rect2, (ball_x, ball_y)) and event.type==pygame.MOUSEBUTTONDOWN:
screen.blit(img2, (-150, -100))
muyulucky.play()
pygame.display.flip()
if pygame.Rect.collidepoint(rect1, (ball_x, ball_y)) and event.type==pygame.MOUSEBUTTONUP:
screen.blit(img1, (-150, -100))
pygame.display.flip(),
if event.type==pygame.QUIT:
pygame.quit()
pygame.display.flip()實現(xiàn)效果

補充
當然利用Python語言還可以實現(xiàn)很多有趣的小項目,小編為大家整理了幾個,感興趣的小伙伴可以嘗試一下
Python代碼實現(xiàn)信息轟炸
實現(xiàn)效果:把光標放在會話框里,即可發(fā)送指定的內(nèi)容和信息數(shù)量!
需要下載pyuput庫----pip install pyuput
代碼如下:
from pynput.keyboard import Key,Controller
import time
keyboard=Controller()
messages=input("請輸入你要轟炸的信息:")
times=eval(input("請輸入你要轟炸的次數(shù):"))
print("數(shù)據(jù)已被后臺接受,請將光標移動至會話框")
time.sleep(2)
for i in range(3):
print("距離信息轟炸還需要%d秒"%(3-i))
time.sleep(1)
for i in range(times):
keyboard.type(messages)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
time.sleep(0.1)
print("信息轟炸已經(jīng)順利完成,已退出!")效果圖

python模擬黑客流星雨
實現(xiàn)代碼
# -*- coding:utf-8 -*-
# 導入系統(tǒng)文件庫
import pygame
import random
from pygame.locals import *
from random import randint
# 定義一些窗體參數(shù)及加載字體文件
SCREEN_WIDTH = 900 # 窗體寬度
SCREEN_HEIGHT = 600 # 窗體寬度
LOW_SPEED = 4 # 字體移動最低速度
HIGH_SPEED = 20 # 字體移動最快速度
FONT_COLOR = (10, 150, 200) # 字體顏色
FONT_SIZE = 15 # 字體尺寸
FONT_NOM = 20 # 顯示字體數(shù)量 從0開始
FONT_NAME = "calibrii.ttf" # 注意字體的文件名必須與真實文件完全相同(注意ttf的大小寫),且文件名不能是中文
FREQUENCE = 10 # 時間頻度
times = 0 # 初始化時間
# 定義隨機參數(shù)
# 隨機下降速度
def randomspeed():
return randint(LOW_SPEED, HIGH_SPEED)
def randomposition():
# 隨機位置
return randint(0, SCREEN_WIDTH), randint(0, SCREEN_HEIGHT)
# 隨機數(shù)值
def randomvalue():
return randint(0, 100) # this is your own display number range
# class of sprite 定義精靈
class Word(pygame.sprite.Sprite):
def __init__(self, bornposition):
pygame.sprite.Sprite.__init__(self)
self.value = randomvalue()# 精靈數(shù)值
self.font = pygame.font.Font(None, FONT_SIZE)# 精靈字體
self.image = self.font.render(str(self.value), True, FONT_COLOR)# 精靈外觀
self.speed = randomspeed()# 精靈速度
self.rect = self.image.get_rect()# 精靈大小
self.rect.topleft = bornposition# 精靈位置
def update(self):# updata pygame內(nèi)部方法
self.rect = self.rect.move(0, self.speed)
if self.rect.top > SCREEN_HEIGHT:
self.kill()# kill pygame內(nèi)部方法
# init the available modules
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("ViatorSun CodeRain")
clock = pygame.time.Clock()
group = pygame.sprite.Group()
group_count = int(SCREEN_WIDTH / FONT_NOM)
# mainloop
while True:
time = clock.tick(FREQUENCE)
for event in pygame.event.get():
if event.type == QUIT:
import mouse
pygame.quit()
exit()
screen.fill((0, 0, 0))
for i in range(0, group_count):
group.add(Word((i * FONT_NOM, -FONT_NOM)))
group.update()
group.draw(screen)
pygame.display.update()效果圖

到此這篇關于基于Python實現(xiàn)敲擊木魚積累功德效果的文章就介紹到這了,更多相關Python敲擊木魚內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)b站直播自動發(fā)送彈幕功能
這篇文章主要介紹了python如何實現(xiàn)b站直播自動發(fā)送彈幕,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-02-02
一文教你用python編寫Dijkstra算法進行機器人路徑規(guī)劃
迪杰斯特拉(Dijkstra)算法是典型最短路徑算法,用于計算一個節(jié)點到其他節(jié)點的最短路徑,這篇文章主要給大家介紹了關于利用python編寫Dijkstra算法進行機器人路徑規(guī)劃的相關資料,需要的朋友可以參考下2021-08-08
Pycharm自動添加文件頭注釋和函數(shù)注釋參數(shù)的方法
這篇文章主要介紹了Pycharm自動添加文件頭注釋和函數(shù)注釋參數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10

