Use Weather / Particles
Author: NingNing
Last update:2022-05-27
This tutorial has a video:
https://youtu.be/0ZRyC935MUc
This is also a more advanced function. In this tutorial, I will show the use of several weather / particles. If you don't know what that means... in simple terms it's like it's raining/snowing and letting the raindrops or snowflakes float on the screen
Example:
It's not that it's very well done, hehe, just a preview~
Anyway, the picture above is the stuff that will be made by this tutorial
By the way, the GUI used for this project is available on my Itch store
Create animated image
Prepare in advance
Prepare a background image first
I chose a forest trail as the background
Then there is the picture of making the weather/ particles
Here I have prepared 4 pictures of maple leaves falling down
tip
Maybe you have a question - why should I prepare a series of pictures of falling maple leaves instead of using GIFs?
- Because Renpy doesn't support GIF files yet
- So to do this effect, it is best to use a series of pictures to present
- You can use various resources on the Internet to convert video/GIF to PNG file
After these two are ready, go to the next step~
Use animated image
The first is to define the image. Here we use image
+ the code name of the image
- Example:
image maple
- Example 2:
image snow
Add a :
after the image code
- Example:
image maple:
- Example 2:
image snow:
The next line is marked with the path of the image and the number of seconds displayed
So the code for the image would look something like this:
image maple:
"images/flower/1.png"
0.15
"images/flower/1-2.png"
0.15
"images/flower/2.png"
0.15
"images/flower/3.png"
0.15
If you want to repeat the image, you can use the command repeat
So your code will end up like this:
image maple:
"images/flower/1.png"
0.15
"images/flower/1-2.png"
0.15
"images/flower/2.png"
0.15
"images/flower/3.png"
0.15
repeat
Show animated images
To display the animated image just defined, use the command show
show maple
In this way, renpy will automatically repeat the following 4 pictures.
- "images/flower/1.png"
- "images/flower/1-2.png"
- "images/flower/2.png"
- "images/flower/3.png"
Because 0.15 is written in the middle of each picture, the time between each picture playback is 0.15 seconds
Use of particles
In addition to animated image, another way to make special effects such as weather is to use particles
As a prep, all you need is a small image, a small image like snowflakes or raindrops
tip
The tutorial of particles is made with reference to official documents --> https://renpy.org/wiki/renpy/doc/cookbook/More_realistic_snow_effect
Copy the following lines of code:
init python:
import random
random.seed()
def Snow(image, max_particles=50, speed=150, wind=100, xborder=(0,100), yborder=(50,400), **kwargs):
return Particles(SnowFactory(image, max_particles, speed, wind, xborder, yborder, **kwargs))
- max_particles = the maximum number of particles that can appear on the screen
- Speed = the speed at which snowflakes (/or other particles) fall
- wind = how strong the wind is, the higher the bar, the faster the snowflakes (/or other particles) will drift
- Both xborder and yborder are used to determine the distance between the created particles
The following string should be placed (/copied into) the string above:
class SnowFactory(object):
def __init__(self, image, max_particles, speed, wind, xborder, yborder, **kwargs):
# the maximum number of particles we can have on screen at once
self.max_particles = max_particles
# the particle's speed
self.speed = speed
# the wind's speed
self.wind = wind
# the horizontal/vertical range to create particles
self.xborder = xborder
self.yborder = yborder
# the maximum depth of the screen. Higher values lead to more varying particles size,
# but it also uses more memory. Default value is 10 and it should be okay for most
# games, since particles sizes are calculated as percentage of this value.
self.depth = kwargs.get("depth", 10)
# initialize the images
self.image = self.image_init(image)
def create(self, particles, st):
"""
This is internally called every frame by the Particles object to create new particles.
We'll just create new particles if the number of particles on the screen is
lower than the max number of particles we can have.
"""
# if we can create a new particle...
if particles is None or len(particles) < self.max_particles:
# generate a random depth for the particle
depth = random.randint(1, self.depth)
# We expect that particles falling far from the screen will move slowly than those
# that are falling near the screen. So we change the speed of particles based on
# its depth =D
depth_speed = 1.5-depth/(self.depth+0.0)
return [ SnowParticle(self.image[depth-1], # the image used by the particle
random.uniform(-self.wind, self.wind)*depth_speed, # wind's force
self.speed*depth_speed, # the vertical speed of the particle
random.randint(self.xborder[0], self.xborder[1]), # horizontal border
random.randint(self.yborder[0], self.yborder[1]), # vertical border
) ]
- self.depth = kwargs.get("depth", 10) The numbers after this string can be changed
- It uses the size of the particles that are automatically generated after the decision
- Use it to automatically create snowflakes that are smaller than the original image
Then copy the following code:
def image_init(self, image):
rv = [ ]
for depth in range(self.depth):
# Resize and adjust the alpha value based on the depth of the image
p = 1.1 - depth/(self.depth+0.0)
if p > 1:
p = 1.0
rv.append( im.FactorScale( im.Alpha(image, p), p ) )
return rv
def predict(self):
return self.image
class SnowParticle(object):
def __init__(self, image, wind, speed, xborder, yborder):
self.image = image
if speed <= 0:
speed = 1
# wind's speed
self.wind = wind
# particle's speed
self.speed = speed
# The last time when this particle was updated (used to calculate the unexpected delay
# between updates, aka lag)
self.oldst = None
# the horizontal/vertical positions of this particle
self.xpos = random.uniform(0-xborder, renpy.config.screen_width+xborder)
self.ypos = -yborder
def update(self, st):
"""
Called internally in every frame to update the particle.
"""
# calculate lag
if self.oldst is None:
self.oldst = st
lag = st - self.oldst
self.oldst = st
# update the position
self.xpos += lag * self.wind
self.ypos += lag * self.speed
if self.ypos > renpy.config.screen_height or\
(self.wind< 0 and self.xpos < 0) or (self.wind > 0 and self.xpos > renpy.config.screen_width):
## print "Dead"
return None
return int(self.xpos), int(self.ypos), st, self.image
Define image time~
init:
image snowfall = Snow("images/snowflake.png")
- You can change the name of the image and the path of the image at will
Now it's time for official use~
Just type show
show snowfall
This will have the effect of snow
Video Background
In addition, to use these special effects, you can also use the video background
All you need to do is put special effects on top of the picture and record it as a video
So all you have to do is:
- Create Video
⬇ - Define video ->
image Video_code = Movie(play="File_Name.File_Type", mask="File_Name.File_Type")
Example:image fmovie = Movie(play="forest.webm", mask="forest.webm")
⬇ - Show video ->
show Video_code
Example:show fmovie
This post is over now~ You can give me / my website a clap if this help you! My website --> https://spaceofningningen.blogspot.com/ Thank you for your support~