2 years ago

#56171

test-img

sheeplol

Slight delay after first pass in pygame rain animation

I'm working on a gamejam and decided to start out with some simple rain. Everything works fine, but when the speed is changed, there seems to be a "round" of rain descending on the first pass of the loop, then after a brief delay, the rain will start consistently falling.

Image Name Image
drop.png 1
rain0.png 2
rain1.png 3
rain2.png 4
rain3.png 5
import pygame as pg 
import sys, os
import random

pg.init()

clock = pg.time.Clock()
size = width, height = (800,800)
screen = pg.display.set_mode(size)

class Rain(pg.sprite.Sprite):

    def __init__(self):

        pg.sprite.Sprite.__init__(self)

        self.image = pg.image.load("rain/drop/drop.png").convert_alpha()
        self.rect = self.image.get_rect()

        self.speed = random.randint(4,10)
        self.rect.x = random.randint(0,800)
        self.rect.y = random.randint(-400,-1)


        self.splash_current = 0
        self.splash = []

        self.splash_frames()

    def update(self):

        self.rect.y += self.speed

        if self.rect.y > 700:

            self.speed = 0

            if not self.splash_anim():

                pass

    def splash_frames(self):

        for i in os.listdir("rain/frames"):
            for j in range(5):
                self.splash.append(pg.image.load(f"rain/frames/{i}").convert_alpha())

    def splash_anim(self):

        self.splash_current += 1

        if self.splash_current == len(self.splash):

            self.image = pg.image.load("rain/drop/drop.png").convert_alpha()
            self.splash_current = 0
            self.rect.y = -200
            self.speed = random.randint(4,10)

            return False

        else:
            self.image = self.splash[self.splash_current]


rain_group = pg.sprite.Group()

for i in range(200):

    rain = Rain()
    rain_group.add(rain)

while True:

    for event in pg.event.get():

        if event.type == pg.QUIT:
            pg.quit()
            sys.exit()

    screen.fill((93, 109, 110))

    rain_group.update()
    rain_group.draw(screen)
    pg.display.flip()
    clock.tick(60)

When the values in self.speed are changed from (4,10) to anything else, it seems to be more noticeable, but it's still (slightly) noticable with (4,10).

The goal is to have the rain all start and fall at the same time, which it does after that first awkward pass. I've spent quite some time looking over the code and changing things, but I'm unsure why the first pass is happening the way it does. Any suggestions?

python

pygame

sprite

pygame-surface

pygame2

0 Answers

Your Answer

Accepted video resources