在pygame中,如何停止在曲面上渲染运动图像的每一帧?

2024-05-19 02:52:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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

Tags: offromrectimageimportselfnumberdef
1条回答
网友
1楼 · 发布于 2024-05-19 02:52:13

您需要做的是将创建雨滴的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形位置重现雨滴

相关问题 更多 >

    热门问题