如何在pygame中停止将运动图像的每一帧渲染到Surface上?

豆豆

我正在尝试使用pygame制作一个简单的降雨动画。我正在向下移动图像,但正在更新图像的正确位置;但是,雨滴运动的每一帧都呈现在屏幕上。许多消息来源指出,我将不得不以某种方式刷新每一帧的屏幕,但是位置更新还应该放在哪里?

这是完整的代码:

import sys
import pygame
from raindrops import Raindrop
from pygame.sprite import Group 


def let_it_rain():
    '''initialize pygame, settings, and screen object'''
    pygame.init()
    screen_width = 1200
    screen_height = 800
    bg_color = (144, 177, 226)
    screen = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption("Let It Rain")

    raindrop = Raindrop(screen)
    raindrops = Group()

    #number of drops in a row
    spacex = screen_width - (2 * raindrop.rect.width)
    raindrop_number_x = int(spacex / (2 * raindrop.rect.width))

    #start window for raindrops
    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        #create row of raindrops
        for raindrop_number in range(raindrop_number_x):
            raindrop = Raindrop(screen)
            raindrop.x = raindrop.rect.x + 2 * raindrop.rect.x * raindrop_number  
            raindrop.rect.x = raindrop.x
            raindrops.add(raindrop)

        screen.fill(bg_color)
        raindrops.update()
        raindrops.draw(screen)
        pygame.display.flip()

let_it_rain()

以及相关的雨滴模块:

import pygame
from pygame.sprite import Sprite


class Raindrop(Sprite):

    def __init__(self, screen):

        #load image
        super(Raindrop, self).__init__()
        self.screen = screen
        self.pic = pygame.image.load('rain.png')
        self.image = pygame.transform.smoothscale(self.pic,(50,60))
        self.rect = self.image.get_rect()

        #starting position
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        #store exact decimal position of rainddrop
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

    def update(self):
        speed = 5
        self.y += speed 
        screen_rect = self.screen.get_rect()
        self.rect.y = self.y
        #once raindrops reach the bottom of the screen, reposition to the top corrdinates
        if self.rect.y == screen_rect.bottom:
            self.y = 0
            self.rect.y = 0
Zettatekdev

您需要做的是将for循环移动到while循环之前创建雨滴的循环,如下所示:

import sys
import pygame
from raindrops import Raindrop
from pygame.sprite import Group 


def let_it_rain():
    '''initialize pygame, settings, and screen object'''
    pygame.init()
    screen_width = 1200
    screen_height = 800
    bg_color = (144, 177, 226)
    screen = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption("Let It Rain")

    raindrop = Raindrop(screen)
    raindrops = Group()

    #number of drops in a row
    spacex = screen_width - (2 * raindrop.rect.width)
    raindrop_number_x = int(spacex / (2 * raindrop.rect.width))

    #create row of raindrops
    for raindrop_number in range(raindrop_number_x):
        raindrop = Raindrop(screen)
        raindrop.x = raindrop.rect.x + 2 * raindrop.rect.x * raindrop_number  
        raindrop.rect.x = raindrop.x
        raindrops.add(raindrop)
    #start window for raindrops
    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        

        screen.fill(bg_color)
        raindrops.update()
        raindrops.draw(screen)
        pygame.display.flip()

let_it_rain()

当它处于循环中时,它会在每个新的y位置上不断创建雨滴

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用rmagick将图像粘贴到ruby的gif的gif的每一帧中

如何每N秒将一帧视频提取到图像?

如何从python中的cv2.VideoCapture获取每一帧作为图像

如何为vulkan中的每一帧更新纹理?

如何在dx11的同一帧中渲染TriangleList和LineList

是否可以在运动jpeg的同一帧中包含两个图像?

如何在ActionScript 3中的下一帧停止音乐功能?

如何在OpenCV(Python)的视频的同一帧中显示图像?

OpenCV-如何在局域网中的单独主机中处理视频的每一帧?

在OS X中从视频文件中提取每一帧作为图像

实时视频源中的每一帧叠加图像质量都会下降

使用SDL2在每一帧上渲染屏幕的一部分

如何在 Inception 中对视频帧运行分类,而不为每一帧启动一个新的 tensorflow 会话?

在Windows Phone 8应用中录制视频时,如何从相机中获取每一帧?

导航到另一帧后如何停止MediaPlayerElement播放音频?

如何改变Love2D中每一帧的速度?

如何使用 ffmpeg 在视频的每一帧中裁剪多个区域?

如何避免对每一帧进行类型检查?

如何防止我的代码产生每一帧?

获取视频中每一帧的时间戳

动画gif中每一帧的Imagemagick重置场景

将Pygame中的多个图像“合并”到一个Surface中以动态旋转所有图像

如何获得渲染的精灵图像(pygame)的连续运动

如何在MouseListener中将一帧移到另一帧

如何将JTable值传递给一帧传递给另一帧

如何将数据从一帧移动到另一帧?

如何将每 25 帧的一张图像保存到文件夹中

如何停止将视频流中的最后一帧作为使用 ffmpeg 编码的 mp4 容器的一部分?

如何在 GridView 中每 6 个不同的项目渲染一个图像?