Last active
June 20, 2016 23:37
-
-
Save greggirwin/6a444d61e4dd2a2d59e430b6d40b25ad to your computer and use it in GitHub Desktop.
Fast bubbles in Red (move mouse over to make it go faster)
This file contains hidden or 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: "Bubbles" | |
Author: [REBOL version "Gabriele Santilli" Red port "Gregg Irwin"] | |
File: %bubbles.red | |
Tabs: 4 | |
Needs: View | |
] | |
system/view/auto-sync?: no | |
win-size: 400x400 | |
bubbles: [] | |
d: compose [ | |
pen 80.80.255.175 | |
fill-pen linear 0x0 0 (win-size/y) 90 1 1 10.10.255 0.0.100 | |
box 0x0 (win-size) | |
] | |
t: now/time/precise | |
random/seed to integer! t/second | |
rand: func [v] [random v] | |
rnd-pair: does [as-pair rand win-size/x rand win-size/y] | |
max-bubble-size: 30 | |
num-bubbles: 100 | |
move-bubble: func [bubble] [ | |
bubble/1/x: bubble/1/x - 3 + rand 5 | |
bubble/1/y: bubble/1/y - 3 - rand 6 | |
if bubble/1/y < 24 [bubble/1/y: win-size/y + max-bubble-size] | |
bubble/-10: bubble/1 - (bubble/2 / 3) | |
] | |
gen-bubbles: does [ | |
clear at d 16 | |
loop num-bubbles [ | |
insert insert bbl: insert tail d compose [ | |
fill-pen radial 150x150 (max-bubble-size) (win-size/y) 0 1 1 128.128.255.105 90.90.255.165 80.80.255.175 | |
circle | |
] rnd-pair (4 + rand 20) | |
; Take these adjustments out for flat, cartoonish bubbles | |
bbl/-8: bbl/2 * 2 ; gradient end = circle radius | |
;!! R2 version does not use * 2 here | |
bbl/-9: to integer! bbl/2 * 0.2 ; gradient start = 20% of radius ; decimal! chokes draw right now | |
bbl/-10: (bbl/1 - (bbl/2 / 3)) ; gradient offset = offset - 1/3 radius | |
append/only bubbles bbl | |
] | |
] | |
gen-bubbles | |
win: layout/tight [ | |
size win-size | |
origin 0x0 | |
canvas: base win-size 10.10.255 draw d | |
] | |
win/actors: context [ | |
on-close: func [face evt [event!]][quit?: yes] | |
on-resize: func [face evt [event!]][ | |
;print ['resize face/size] | |
win-size: face/size | |
canvas/size: face/size | |
poke d 7 face/size/y | |
poke d 15 face/size | |
gen-bubbles | |
] | |
] | |
view/flags/no-wait win [resize] | |
quit?: no | |
until [ | |
foreach bubble bubbles [move-bubble bubble] | |
show canvas | |
do-events/no-wait | |
quit? | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
loop 10 [do-events/no-wait]
here to process more events, make the animation more smooth.