Добавлены примеры

This commit is contained in:
Victor 2015-07-11 21:19:59 +03:00
parent 27e822a6a7
commit ab4a8c66b9
10 changed files with 416 additions and 0 deletions

14
examples/canvas/1.own Normal file
View File

@ -0,0 +1,14 @@
use "canvas"
w = 800 h = 600
window("canvas example", w, h);
// Очистка экрана белым цветом
color(#ffffff)
frect(0, 0, w, h)
// Рисуем две линии красным цветом
color(#ff0000)
line(0, 0, w, h)
line(w, 0, 0, h)
repaint()

17
examples/canvas/2.own Normal file
View File

@ -0,0 +1,17 @@
use "std"
use "canvas"
w = 800 h = 600
window("canvas example 2", w, h);
color(#ffffff)
frect(0, 0, w, h)
step = rand(20)
color(#0000ff)
for y = 0, y < h, y = y + step {
line(0, y, w, y)
}
for x = 0, x < w, x = x + step {
line(x, 0, x, h)
}
repaint();

View File

@ -0,0 +1,55 @@
use "canvas"
use "std"
w = 800 h = 600
window("Animate line", 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)
hue = 0
run = 1
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
hue = hue + 0.0001
if (hue >= 1) hue = 0
sethsbcolor(hue)
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)
def floor(v) return v - v % 1
def sethsbcolor(h1) {
qr = h1 * 6 // временно для расчёта
hueindex = floor(qr) % 6
f = qr - floor(qr)
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)
}

View File

@ -0,0 +1,25 @@
use "canvas"
use "std"
w = 640 h = 480
window("Управление точкой", w, h)
x = rand(w) y = rand(h)
run = 1
while run {
key = keypressed()
if (key == VK_LEFT && x > 0) x = x - 1
else if (key == VK_RIGHT && x < w) x = x + 1
else if (key == VK_UP && y > 0) y = y - 1
else if (key == VK_DOWN && y < h) y = y + 1
else if key == VK_ESCAPE run = 0
color(255,255,255)
frect(0,0,w,h)
color(0)
line(0, h, x, y)
line(w, h, x, y)
repaint()
sleep(10)
}

View File

@ -0,0 +1,46 @@
use "canvas"
use "math"
use "std"
msg = ""
NUM_POINTS = 0
while (NUM_POINTS <= 2 || NUM_POINTS > 25) {
NUM_POINTS = 0 + prompt("Сколькиугольник? (3..25)" + msg)
if (NUM_POINTS <= 2) msg = "!! Сказано же, ну!"
else if (NUM_POINTS > 25) msg = " Чувак, " + NUM_POINTS + " это будет ООООЧЕНЬ долго!"
}
angle = 2*PI / NUM_POINTS;
DIVIDER = 2.8
w = 800 h = 600
window("Fractal polygon demo", w, h)
fractal(w/2, h/2, w/2)
repaint()
def cpoly(cx, cy, size) {
ox = cx oy = cy - size
i = 0
ang = 0
while i < NUM_POINTS {
ang = ang + angle
nx = cx - sin(ang)*size ny = cy - cos(ang)*size
line(ox, oy, nx, ny)
ox = nx oy = ny
i = i + 1
}
}
def fractal(cx, cy, size) {
if size >= 3 {
s2 = size / 2
color(0, 0, 255 - size * 255 / w/2)
cpoly(cx, cy, size / DIVIDER)
fractal(cx, cy - s2, size / DIVIDER)
n = 0
while n < NUM_POINTS {
fractal(cx - sin(angle*n)*s2, cy - cos(angle*n)*s2, size / DIVIDER)
n = n + 1
}
}
}

View File

@ -0,0 +1,24 @@
use "canvas"
w = 800 h = 600
window("Fractal rectangle demo", w, h)
fractal(w/2, h/2, w/2)
repaint()
def rect(x, y, w, h) {
line(x, y, x + w, y)
line(x + w, y, x + w, y + h)
line(x, y + h, x + w, y + h)
line(x, y, x, y + h)
}
def fractal(cx, cy, size) {
if size >= 2 {
color(0, 0, 255 - size * 255 / w/2)
rect(cx-size/2, cy-size/2, size, size)
fractal(cx-size/2, cy-size/2, size / 2)
fractal(cx+size/2, cy-size/2, size / 2)
fractal(cx-size/2, cy+size/2, size / 2)
fractal(cx+size/2, cy+size/2, size / 2)
}
}

View File

@ -0,0 +1,17 @@
use "std"
echo(#ABCDEF - #12345)
echo(-8 << 2)
echo(-8 >> 2)
echo(-8 >>> 2)
echo()
for a = 0, a <= 1, a = a + 1 {
for b = 0, b <= 1, b = b + 1 {
echo(a, " | ", b, " ", a | b)
echo(a, " & ", b, " ", a & b)
echo(a, " ^ ", b, " ", a ^ b)
}
echo(" ~", a, " ", ~a)
echo(" !", a, " ", !a)
}

View File

@ -0,0 +1,7 @@
use "std"
a = 0
b = 1
print (a ? "text1" : "text2") + "\n"
print (b ? "text3" : "text4") + "\n"

View File

@ -0,0 +1,35 @@
// Вычисляем факториал
def factorial(n) {
if n == 1 return 1
return n * factorial(n-1)
}
print factorial(6)
print "\n"
// 0 1 1 2 3 5 8 13 21 34
def fibonacci(count) {
def fib(n) {
if n < 2 return n
return fib(n-2) + fib(n-1)
}
return fib(count)
}
for i = 0, i < 10, i = i + 1
print " " + fibonacci(i)
print "\n"
// Вычисляем факториал с помощью хвостовой рекурсии
def factorial_tailrec(n) {
def fact(acc, n) {
if n == 0 return acc
return fact(acc * n, n - 1)
}
return fact(1, n-1)
}
print factorial_tailrec(6)
print "\n"

176
examples/game/pipes.own Normal file
View File

@ -0,0 +1,176 @@
use "std"
use "canvas"
///-------------------------------------
/// PIPES CELL
///-------------------------------------
CELL_START = 0
HORIZONTAL = 0
VERTICAL = 1
LEFT_TO_DOWN = 2
LEFT_TO_UP = 3
RIGHT_TO_UP = 4
RIGHT_TO_DOWN = 5
CROSS = 6
CELL_LAST = 6
// лево, право, верх, низ
HorizontalCell = [1,1,0,0]
VerticalCell = [0,0,1,1]
LeftToDownCell = [1,0,0,1]
LeftToUpCell = [1,0,1,0]
RightToUpCell = [0,1,1,0]
RightToDownCell = [0,1,0,1]
CrossCell = [1,1,1,1]
support = [
HorizontalCell, VerticalCell,
LeftToDownCell, LeftToUpCell,
RightToUpCell, RightToDownCell,
CrossCell
]
def draw(v, cellSize) {
c2 = cellSize / 2
if (v == HORIZONTAL) fillRect(0, c2 - 2, cellSize, 4)
else if (v == VERTICAL) fillRect(c2 - 2, 0, 4, cellSize)
else if (v == LEFT_TO_DOWN) {
fillRect(0, c2 - 2, c2, 4)
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
}
else if (v == LEFT_TO_UP) {
fillRect(0, c2 - 2, c2, 4)
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
}
else if (v == RIGHT_TO_UP) {
fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
fillRect(c2 - 2, 0, 4, c2 + 2)
}
else if (v == RIGHT_TO_DOWN) {
fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
}
else if (v == CROSS) {
fillRect(c2 - 2, 0, 4, cellSize)
fillRect(0, c2 - 2, cellSize, 4)
}
}
def supportLeft(v) return support[v][0]
def supportRight(v) return support[v][1]
def supportUp(v) return support[v][2]
def supportDown(v) return support[v][3]
///-------------------------------------
///-------------------------------------
/// PIPES BOARD
///-------------------------------------
SIZE = 10
// Создаём игровое поле
board = newarray(SIZE, SIZE)
def createBoard() {
for i=0, i<SIZE, i=i+1
for j=0, j<SIZE, j=j+1
board[i][j] = rand(CELL_LAST)
}
def switchCell(x, y) {
nextType = board[x][y] + 1
board[x][y] = nextType > CELL_LAST ? CELL_START : nextType
}
def isFinished() {
// Стартовая труба должна иметь левую точку соприкосновения
if (!supportLeft(board[0][0])) return 0
// А конечная труба - правую
if (!supportRight(board[SIZE - 1][SIZE - 1])) return 0
visited = newarray(SIZE, SIZE)
return isConnected(0, 0, visited)
}
def isConnected(curX, curY, visited) {
// Если достигли конечной ячейки - выходим.
if ( (curX == SIZE - 1) && (curY == SIZE - 1) ) return 1
// Если уже посещали - выходим.
if (visited[curX][curY]) return 0
// Отмечаем посещение.
visited[curX][curY] = 1
current = board[curX][curY]
if ( supportLeft(current) && (curX > 0) && (supportRight(board[curX - 1][curY])) ) {
if (isConnected(curX - 1, curY, visited)) return 1
}
if ( supportRight(current) && (curX < SIZE - 1) && (supportLeft(board[curX + 1][curY])) ) {
if (isConnected(curX + 1, curY, visited)) return 1
}
if ( supportUp(current) && (curY > 0) && (supportDown(board[curX][curY - 1])) ) {
if (isConnected(curX, curY - 1, visited)) return 1
}
if ( supportDown(current) && (curY < SIZE - 1) && (supportUp(board[curX][curY + 1])) ) {
if (isConnected(curX, curY + 1, visited)) return 1
}
return 0
}
///-------------------------------------
///-------------------------------------
/// PIPES MAIN
///-------------------------------------
translateX = 0 translateY = 0
/* frect с поддержкой translate*/
def fillRect(x,y,w,h) {
frect(translateX+x, translateY+y, w, h)
}
// JAVA ME
// showcanvas()
// JAVA SE
WIDTH = 480 HEIGHT = 480
window("Pipes", WIDTH, HEIGHT)
cellSize = WIDTH / SIZE
createBoard()
// курсор
curX = 0
curY = 0
run = 1
while run {
//key = gameaction(keypressed())
key = keypressed()
if (key == VK_LEFT && curX > 0) curX = curX - 1
else if (key == VK_RIGHT && curX < SIZE - 1) curX = curX + 1
else if (key == VK_UP && curY > 0) curY = curY - 1
else if (key == VK_DOWN && curY < SIZE - 1) curY = curY + 1
else if key == VK_FIRE switchCell(curX,curY)
else if key == 48 run = 0
// фон
color(isFinished() ? #00FF00 : #FFFFFF)
frect(0,0,WIDTH,HEIGHT)
// курсор
color(#4444FF)
frect(curX*cellSize, curY*cellSize, cellSize, cellSize)
for (i=0, i<SIZE, i=i+1) {
color(0)
ic = i*cellSize
line(0, ic, cellSize*SIZE, ic)
line(ic, 0, ic, cellSize*SIZE)
color(#FF0000)
for j=0, j<SIZE, j=j+1 {
translateX = ic
translateY = j*cellSize
draw(board[i][j], cellSize)
translateX = -ic
translateY = -j*cellSize
}
}
repaint()
sleep(50)
}