Own-Programming-Language-Tu.../examples/canvas/animate_line_thread.own

57 lines
1.3 KiB
Scala
Raw Normal View History

2016-02-20 10:12:26 +02:00
use "canvas"
use "std"
w = 800 h = 600
window("Animate line with thread", w, h)
// Крайние точки линии
x1 = rand(w) y1 = rand(h)
x2 = rand(w) y2 = rand(h)
// Направление движения
d1x = rand() d1y = -rand()
d2x = rand() d2y = rand()
// Очищаем экран
color(rand(#FFFFFF))
frect(0,0,w,h)
run = 1
thread("changehue")
def changehue() {
hue = 0
while run {
hue = hue + 0.0001
if (hue >= 1) hue = 0
qr = hue * 6 // временно для расчёта
hueindex = (qr - qr % 1) % 6
f = qr - (qr - qr % 1)
if hueindex == 0 color(255, f*255, 0)
else if hueindex == 1 color(255 - f*255, 255, 0)
else if hueindex == 2 color(0, 255, f*255)
else if hueindex == 3 color(0, 255-f*255, 255)
else if hueindex == 4 color(f*255, 0, 255)
else if hueindex == 5 color(255, 0, 255-f*255)
sleep(5)
}
}
while run {
if checkhoriz(x1) d1x = -d1x
if checkhoriz(x2) d2x = -d2x
if checkvert(y1) d1y = -d1y
if checkvert(y2) d2y = -d2y
x1 = x1 + d1x x2 = x2 + d2x
y1 = y1 + d1y y2 = y2 + d2y
line(x1, y1, x2, y2)
repaint()
sleep(10)
if keypressed() == VK_ESCAPE run = 0
}
def checkhoriz(px) return (px >= w || px < 0)
def checkvert(py) return (py >= h || py < 0)