Pen Particles

Материал из Поле цифровой дидактики

Шаблон:Stub

This tutorial will show how to make pen particles. These are often used as background or death effects in many games.

Adding Particles to a List

The first step to making pen particles is making a list of particles as a 2D array, each particle will have more than one item on the particles list. First, a list needs to be created, called particles:

(particles::list)

Now, a script that adds a particle at a certain x and y position with a certain x and y velocity.

define Add particle | Position:(x), (y) | Velocities: (X Vel) (Y Vel) Color: (color) Size (size::custom)
add (x::custom) to [particles v] // The starting X position of the particle
add (y::custom) to [particles v] // The starting Y position of the particle
add (X vel::custom) to [particles v] // The starting X velocity of the particle
add (Y vel::custom) to [particles v] // The starting Y velocity of the particle
add (color::custom) to [particles v] // The color of the particle
add (size::custom) to [particles v] // The size of the particle

Deleting Particles

Now a script needs to be created to delete a particle at a certain item on the particles list:

define Delete particle (i)
delete ((i)+(5)) of [particles v] // starts at the particle's last item in the list
delete ((i)+(4)) of [particles v] // and moves back until all items on the list that belong to that particle are deleted
delete ((i)+(3)) of [particles v]
delete ((i)+(2)) of [particles v]
delete ((i)+(1)) of [particles v]
delete (i) of [particles v]

Rendering Particles

A script will also be needed to render the particles that are already stored in the particles list:

define Render
erase all // erasing all of the drawing made previously
set [i v] to (1)
repeat ((length of [particles v]::list)/(6)) // The amount of particles in the list because each particle takes up 6 items
   go to x: (item (i) of [particles v]) y: (item ((i)+(1)) of [particles v]) // This tutorial uses the particle's items on the list to set the properties of the sprite
    set pen size to (item ((i)+(5)) of [particles v]::list)
    set pen color to (item ((i)+(4)) of [particles v]::list)
    pen down // Making a dot
    pen up
    change [i v] by (6)

Updating the Particles' Positions

This script moves the particles around:

define Update
set [i v] to (1)
repeat ((length of [paticles v])/(6)
    replace item (i) of [particles v] with ((item (i) of [particles v])+(item ((i) + (2)) of [particles v]))::list // Changing the x position by the x velocity
    replace item ((i)+(1)) of [particles v] with ((item ((i)+(1)) of [particles v])+(item ((i) + (3)) of [particles v])) // Changing the y position of the particle by its y velocity
    replace item ((i)+(2)) of [particles v] with (((item ((i)+(2)) of [particles v])::list)-(1))//Optional gravity, change the Y velocity by -1
    if<<([abs v](item (i) of [particles v])::operators)>(240)> or<([abs v](item ((i)+(1)) of [particles v])::operators)>(180)>>then
        Delete particle (i)::custom // if the particle if off the screen, delete it.
    end
    change [i v] by (6)

Implementing The Code

Now that all the scripts are made, they must be used. Depending on the type of program wished to make, one may want to make mouse effects:

When green flag clicked
delete all of [particles v]
forever
    Add particle | Position:(mouse x), (mouse y) | Velocities: (pick a random (-10) to (10)::operators) (pick a random (-10) to (10)::operators) Color: (pick a random (0) to (100)::operators) Size (pick a random (-10) to (10)::operators)::custom
    Update::custom
    Render::custom
end

Or one can make background effects:

When green flag clicked//If  background effects are made, make sure that they do not have gravity by removing the change y velocity block in the update script.
delete all of [particles v]
forever
    Add particle | Position:(pick a random (-240) to (240)::operators), (pick a random (-180) to (180)::operators) | Velocities: (pick a random (-10) to (10)::operators) (pick a random (-10) to (10)::operators) Color: (pick a random (0) to (100)::operators) Size (pick a random (-10) to (10)::operators)::custom
    Update::custom
    Render::custom
end