基于Python实现敲击木鱼积累功德效果

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

基于Python实现敲击木鱼积累功德效果

DY.memory   2022-11-09 我要评论

示例代码

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代码实现信息轰炸

实现效果:把光标放在会话框里,即可发送指定的内容和信息数量!

需要下载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 -*-
# 导入系统文件库
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()

效果图

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们