Created
November 15, 2018 12:58
-
-
Save toomasv/261e611fde15ee4a09b8d6d9ebe3b61a to your computer and use it in GitHub Desktop.
Explosions by Bohdan Lechnowsky
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Title: "Explosions in Red" | |
Date: 14-Nov-2018 | |
Author: "Bohdan Lechnowsky" | |
Origin: http://respectech.com/red/explosions.red | |
Adaptation: "Toomas Vooglaid" | |
Notes: { | |
Derived from "Explosions in Python" from p.33 of "Wireframe" magazine (wfmag.cc) Issue 1. | |
Unlike the above program that requires Python to be installed, plus "Pygame Zero" (whatever | |
that is), this can be run within the Red interpreter without any add-ons or installation. | |
Upgrades from the original program include: | |
- Explosions: | |
- vary in base color | |
- have between 50 and 150 particles | |
- have a unique speed of expansion | |
- happen at random intervals | |
- Each particle of each explosion: | |
- is a circle | |
- varies in color | |
- varies in radius | |
- has a unique max_age | |
- fades out in color and reduces in size as it ages | |
} | |
] | |
screen_size: make image! 800x600 | |
drag: .8 | |
max_age: 3.0 | |
particles: copy [] | |
random/seed now/time/precise | |
explode: func [xy speed /local angle radius][ | |
particle_color: 255.255.255 - random 128.128.128 | |
speed: 1 + (-0.5 + random 1.0) * speed | |
loop 50 + random 100 [ | |
append particles reduce [ | |
xy/x | |
xy/y | |
speed * (radius: (random 1.0) ** .5) * sin (angle: random 2 * pi) | |
speed * radius * cos angle | |
random max_age | |
particle_color | |
] | |
] | |
] | |
draw_screen: func [/local xy][ | |
draw_blk: compose [fill-pen black box 0x0 800x600] | |
foreach [x y vx vy age part_col] particles [ | |
xy: to-pair reduce [x y] | |
append draw_blk compose [ | |
pen (pc: (part_col - (random 32.32.32) * (age / max_age))) | |
fill-pen (pc) | |
circle (xy) ((random 2) * (age / max_age)) | |
] | |
] | |
draw screen_size draw_blk | |
] | |
update: func [dt /local particle][ | |
new_particles: copy [] | |
drag: drag ** dt | |
foreach [x y vx vy age part_col] particles [ | |
if positive? age [ | |
vx: vx * drag | |
vy: vy * drag | |
particle: reduce [ | |
x + vx | |
y + vy | |
vx | |
vy | |
age - dt | |
part_col | |
] | |
append new_particles particle | |
] | |
] | |
particles: new_particles | |
] | |
explode random 800x600 1.5 | |
period: .01 | |
speed: 1.5 | |
view compose [ | |
below | |
i: image (draw_screen) | |
; Adapted by @toomasv | |
rate 32 | |
on-time [ | |
update period | |
i/image: draw_screen | |
show i | |
if 1 = random 30 [ | |
explode random 800x600 speed | |
] | |
] | |
across text "Rate:" 50 slider data 50% [rat/text: to-string i/rate: 1 + to-integer 64 * face/data] rat: text 20 | |
text "Period:" slider data 10% [per/text: to-string round/to period: to-float face/data * .1 .001] per: text 30 | |
text "Speed:" slider data 75% [spd/text: to-string round/to period: to-float face/data * 2 .001] spd: text 30 | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment