基于Python實(shí)現(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(): # 獲取光標(biāo)位置,2個(gè)值 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()
實(shí)現(xiàn)效果
補(bǔ)充
當(dāng)然利用Python語(yǔ)言還可以實(shí)現(xiàn)很多有趣的小項(xiàng)目,小編為大家整理了幾個(gè),感興趣的小伙伴可以嘗試一下
Python代碼實(shí)現(xiàn)信息轟炸
實(shí)現(xiàn)效果:把光標(biāo)放在會(huì)話框里,即可發(fā)送指定的內(nèi)容和信息數(shù)量!
需要下載pyuput庫(kù)----pip install pyuput
代碼如下:
from pynput.keyboard import Key,Controller import time keyboard=Controller() messages=input("請(qǐng)輸入你要轟炸的信息:") times=eval(input("請(qǐng)輸入你要轟炸的次數(shù):")) print("數(shù)據(jù)已被后臺(tái)接受,請(qǐng)將光標(biāo)移動(dòng)至?xí)捒?) 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模擬黑客流星雨
實(shí)現(xiàn)代碼
# -*- coding:utf-8 -*- # 導(dǎo)入系統(tǒng)文件庫(kù) import pygame import random from pygame.locals import * from random import randint # 定義一些窗體參數(shù)及加載字體文件 SCREEN_WIDTH = 900 # 窗體寬度 SCREEN_HEIGHT = 600 # 窗體寬度 LOW_SPEED = 4 # 字體移動(dòng)最低速度 HIGH_SPEED = 20 # 字體移動(dòng)最快速度 FONT_COLOR = (10, 150, 200) # 字體顏色 FONT_SIZE = 15 # 字體尺寸 FONT_NOM = 20 # 顯示字體數(shù)量 從0開始 FONT_NAME = "calibrii.ttf" # 注意字體的文件名必須與真實(shí)文件完全相同(注意ttf的大小寫),且文件名不能是中文 FREQUENCE = 10 # 時(shí)間頻度 times = 0 # 初始化時(shí)間 # 定義隨機(jī)參數(shù) # 隨機(jī)下降速度 def randomspeed(): return randint(LOW_SPEED, HIGH_SPEED) def randomposition(): # 隨機(jī)位置 return randint(0, SCREEN_WIDTH), randint(0, SCREEN_HEIGHT) # 隨機(jī)數(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()
效果圖
到此這篇關(guān)于基于Python實(shí)現(xiàn)敲擊木魚積累功德效果的文章就介紹到這了,更多相關(guān)Python敲擊木魚內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)b站直播自動(dòng)發(fā)送彈幕功能
這篇文章主要介紹了python如何實(shí)現(xiàn)b站直播自動(dòng)發(fā)送彈幕,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-02-02python 實(shí)現(xiàn)敏感詞過(guò)濾的方法
今天小編就為大家分享一篇python 實(shí)現(xiàn)敏感詞過(guò)濾的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01一文教你用python編寫Dijkstra算法進(jìn)行機(jī)器人路徑規(guī)劃
迪杰斯特拉(Dijkstra)算法是典型最短路徑算法,用于計(jì)算一個(gè)節(jié)點(diǎn)到其他節(jié)點(diǎn)的最短路徑,這篇文章主要給大家介紹了關(guān)于利用python編寫Dijkstra算法進(jìn)行機(jī)器人路徑規(guī)劃的相關(guān)資料,需要的朋友可以參考下2021-08-08TF-IDF算法解析與Python實(shí)現(xiàn)方法詳解
這篇文章主要介紹了TF-IDF算法解析與Python實(shí)現(xiàn)方法詳解,文章介紹了tf-idf算法的主要思想,分享了Python實(shí)現(xiàn)tr-idf算法所必要的預(yù)處理過(guò)程,以及具體實(shí)現(xiàn)代碼等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Python基礎(chǔ)學(xué)習(xí)之模塊的安裝和卸載
其實(shí)現(xiàn)在還是有很多剛開始學(xué)習(xí)的小伙伴,會(huì)遇到模塊不會(huì)安裝的情況,或者一遇到報(bào)錯(cuò)就懵了,這樣就很耽誤我們的學(xué)習(xí)進(jìn)度。所以,今天我們就來(lái)了解一下Python幾種安裝模塊的方法吧2022-09-09Pycharm自動(dòng)添加文件頭注釋和函數(shù)注釋參數(shù)的方法
這篇文章主要介紹了Pycharm自動(dòng)添加文件頭注釋和函數(shù)注釋參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10