基于Python實現敲擊木魚積累功德效果
更新時間:2022年11月08日 14:34:29 作者:DY.memory
最近大家都很流行用手機敲擊電子木魚積累功德,這在很多短視頻中也常常見到。本文將用Python語言實現這一效果,感興趣的小伙伴開業(yè)了解一下
示例代碼
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()
實現效果
補充
當然利用Python語言還可以實現很多有趣的小項目,小編為大家整理了幾個,感興趣的小伙伴可以嘗試一下
Python代碼實現信息轟炸
實現效果:把光標放在會話框里,即可發(fā)送指定的內容和信息數量!
需要下載pyuput庫----pip install pyuput
代碼如下:
from pynput.keyboard import Key,Controller import time keyboard=Controller() messages=input("請輸入你要轟炸的信息:") times=eval(input("請輸入你要轟炸的次數:")) print("數據已被后臺接受,請將光標移動至會話框") 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("信息轟炸已經順利完成,已退出!")
效果圖
python模擬黑客流星雨
實現代碼
# -*- coding:utf-8 -*- # 導入系統(tǒng)文件庫 import pygame import random from pygame.locals import * from random import randint # 定義一些窗體參數及加載字體文件 SCREEN_WIDTH = 900 # 窗體寬度 SCREEN_HEIGHT = 600 # 窗體寬度 LOW_SPEED = 4 # 字體移動最低速度 HIGH_SPEED = 20 # 字體移動最快速度 FONT_COLOR = (10, 150, 200) # 字體顏色 FONT_SIZE = 15 # 字體尺寸 FONT_NOM = 20 # 顯示字體數量 從0開始 FONT_NAME = "calibrii.ttf" # 注意字體的文件名必須與真實文件完全相同(注意ttf的大小寫),且文件名不能是中文 FREQUENCE = 10 # 時間頻度 times = 0 # 初始化時間 # 定義隨機參數 # 隨機下降速度 def randomspeed(): return randint(LOW_SPEED, HIGH_SPEED) def randomposition(): # 隨機位置 return randint(0, SCREEN_WIDTH), randint(0, SCREEN_HEIGHT) # 隨機數值 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()# 精靈數值 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內部方法 self.rect = self.rect.move(0, self.speed) if self.rect.top > SCREEN_HEIGHT: self.kill()# kill pygame內部方法 # 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實現敲擊木魚積累功德效果的文章就介紹到這了,更多相關Python敲擊木魚內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
一文教你用python編寫Dijkstra算法進行機器人路徑規(guī)劃
迪杰斯特拉(Dijkstra)算法是典型最短路徑算法,用于計算一個節(jié)點到其他節(jié)點的最短路徑,這篇文章主要給大家介紹了關于利用python編寫Dijkstra算法進行機器人路徑規(guī)劃的相關資料,需要的朋友可以參考下2021-08-08