Own-Programming-Language-Tu.../examples/game/pipes.own

176 lines
4.5 KiB
Plaintext
Raw Permalink Normal View History

2015-07-11 21:19:59 +03:00
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)
}