Last active
January 17, 2019 11:44
-
-
Save nedzadarek/53ed0cdb89e28c99175fb8ff692da7f3 to your computer and use it in GitHub Desktop.
Image version of this: https://github.com/vejitatoja/Red-demos/tree/master/Balls
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
system/view/auto-sync?: no | |
canvas-size: 900x900 | |
ball-speed: 1x1 | |
ball-size: 10 | |
ball-spacing: 36x1 | |
balls-number: 1000 | |
balls: make block! 1500 | |
balls2: make block! 1500 | |
draw-block: [] | |
ball-color: does [as-color random 255 210 222] | |
move-ball: function [ball velocity] [ | |
ball/1/x: ball/1/x + velocity/x | |
ball/1/y: ball/1/y + velocity/y | |
if ball/1/x > canvas-size/x [ball/1/x: 0] | |
if ball/1/x < 0 [ball/1/x: canvas-size/x] | |
if ball/1/y > canvas-size/y [ball/1/y: 0] | |
] | |
img: make image! canvas-size | |
update: does [ | |
foreach ball balls [move-ball ball ball-speed] | |
foreach ball balls2 [move-ball ball ball-speed * -1x1] | |
draw img draw-block | |
] | |
time-block: func [block /reps times] [ | |
if not reps [times: 1] | |
start: now/time/precise | |
loop times [do :block] | |
print now/time/precise - start | |
] | |
repeat i balls-number [ | |
position: as-pair i * ball-spacing/x i * ball-spacing/y | |
while [position/x > canvas-size/x] [position/x: -1 * (canvas-size/x - position/x)] | |
while [position/y > canvas-size/y] [position/y: -1 * (canvas-size/y - position/y)] | |
insert insert b: insert tail draw-block compose [ | |
pen off fill-pen (ball-color) circle | |
] position ball-size | |
append/only balls b | |
] | |
repeat i balls-number [ | |
position: as-pair i * ball-spacing/x i * ball-spacing/y | |
while [position/x > canvas-size/x] [position/x: -1 * (canvas-size/x - position/x)] | |
while [position/y > canvas-size/y] [position/y: -1 * (canvas-size/y - position/y)] | |
insert insert b: insert tail draw-block compose [ | |
pen off fill-pen (ball-color) circle | |
] position ball-size | |
append/only balls2 b | |
] | |
view compose [ title "balls demo" | |
canvas: base 110.50.70 (canvas-size) rate 60 on-time [ | |
time-block [ | |
canvas/image: update | |
] | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rev2: at the moment, images are not garbage collected. I am using
draw
with existing image to avoid create more than one image.