以下是部分代码预览:
"""移动鼠标时,会有渐隐的拖尾现象scratch动画转Python代码, 这本来是一个scratch作品,现在用Python把它做一下. 问题: 本程序的叶子应该逐渐消失,但是在程序中它消失后在最后几帧或一帧为什么会完全显示,明明是把它的透明度不断减少啊....."""
import time
import pygame
from pygame.locals import *
class Leaf:
"""叶子类,它会不断克隆自己,从而形成渐隐的鼠标拖尾效果"""
def __init__(self,image,position,group):
"""初始化方法,image:叶子的surface对象,position:坐标,group:所在组"""
pygame.sprite.Sprite.__init__(self) # 初始化父类
self.image = image
self.rect = self.image.get_rect() # 矩形对象
self.rect.center = position # 中心点坐标
self.display_time = 1 # 显示时间
self.start_time = time.time() # 起始时间
self.group = group # 这样能引用自己的组
self.group.add(self) # 加入到自己的组中
self._alpha = 255 # 自定义属性,完全不透明
self.disapper = False
def update(self):
"""超时从组中删除"""
width,height = 960,720
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("动态背景_月亮夜色情景_鼠标渐隐拖尾交互多媒体动画")
pygame.mixer.init()
pygame.mixer.music.load("Igor_Khabarov_-_Try.mp3.wav")
pygame.mixer.music.play(-1,0) # 循环播放背景音乐
leaf_image = pygame.image.load("叶子.png").convert()
leaf_image.set_colorkey((0,0,0))
leaf_group = set()
clock = pygame.time.Clock()
running = True
frame_counter = 0
while running:
frame_counter +=1 # 帧计数器
# 事件检测
event = pygame.event.poll()
if event.type == QUIT:running = False
# 更新逻辑
mx,my = pygame.mouse.get_pos()
if frame_counter % 2 ==0 : Leaf(leaf_image,(mx,my),leaf_group)
[leaf.update() for leaf in leaf_group ] # 更新所有叶子
# draw all images
screen.fill((0,0,0))
screen.blit(background,background_rect)
clock.tick(30)
pygame.quit()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

