[RenPy] 纯文本查看 复制代码
init python:#########下雪random
import random
random.seed()
#################################################################
# Snow particles
# ----------------
def Snow(image, max_particles=1000, speed=150, wind=10, xborder=(0,100), yborder=(50,400), **kwargs):
"""
This creates the snow effect. You should use this function instead of instancing
the SnowFactory directly (we'll, doesn't matter actually, but it saves typing if you're
using the default values =D)
@parm {image} image:
The image used as the snowflakes. This should always be a image file or an im object,
since we'll apply im transformations in it.
@parm {int} max_particles:
The maximum number of particles at once in the screen.
@parm {float} speed:
The base vertical speed of the particles. The higher the value, the faster particles will fall.
Values below 1 will be changed to 1
@parm {float} wind:
The max wind force that'll be applyed to the particles.
@parm {Tuple ({int} min, {int} max)} xborder:
The horizontal border range. A random value between those two will be applyed when creating particles.
@parm {Tuple ({int} min, {int} max)} yborder:
The vertical border range. A random value between those two will be applyed when creating particles.
The higher the values, the fartest from the screen they will be created.
"""
return Particles(SnowFactory(image, max_particles, speed, wind, xborder, yborder, **kwargs))
# ---------------------------------------------------------------
class SnowFactory(object):
"""
The factory that creates the particles we use in the snow effect.
"""
def __init__(self, image, max_particles, speed, wind, xborder, yborder, **kwargs):
"""
Initialize the factory. Parameters are the same as the Snow function.
"""
# 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
) ]
def image_init(self, image):
"""
This is called internally to initialize the images.
will create a list of images with different sizes, so we
can predict them all and use the cached versions to make it more memory efficient.
"""
rv = [ ]
# generate the array of images for each possible depth value.
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):
"""
This is called internally by the Particles object to predict the images the particles
are using. It's expected to return a list of images to predict.
"""
return self.image
# ---------------------------------------------------------------
class SnowParticle(object):
"""
Represents every particle in the screen.
"""
def __init__(self, image, wind, speed, xborder, yborder):
"""
Initializes the snow particle. This is called automatically when the object is created.
"""
# The image used by this particle
self.image = image
# For safety (and since we don't have snow going from the floor to the sky o.o)
# if the vertical speed of the particle is lower than 1, we use 1.
# This prevents the particles of being stuck in the screen forever and not falling at all.
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
# verify if the particle went out of the screen so we can destroy it.
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
# returns the particle as a Tuple (xpos, ypos, time, image)
# since it expects horizontal and vertical positions to be integers, we have to convert
# it (internal positions use float for smooth movements =D)
return int(self.xpos), int(self.ypos), st, self.image
image snow = Snow("patterns/snow01.png")