1+ use "std"
2+ use "canvas"
3+ use "socket"
4+
5+ /// --- PIPES CELL ---
6+ CELL_START = 0
7+ HORIZONTAL = 0
8+ VERTICAL = 1
9+ LEFT_TO_DOWN = 2
10+ LEFT_TO_UP = 3
11+ RIGHT_TO_UP = 4
12+ RIGHT_TO_DOWN = 5
13+ CROSS = 6
14+ CELL_LAST = 6
15+
16+ Cells = [
17+ {"index": HORIZONTAL, "next": VERTICAL},
18+ {"index": VERTICAL, "next": HORIZONTAL},
19+ {"index": LEFT_TO_DOWN, "next": LEFT_TO_UP},
20+ {"index": LEFT_TO_UP, "next": RIGHT_TO_UP},
21+ {"index": RIGHT_TO_UP, "next": RIGHT_TO_DOWN},
22+ {"index": RIGHT_TO_DOWN, "next": LEFT_TO_DOWN},
23+ {"index": CROSS, "next": CROSS}
24+ ];
25+
26+
27+ def draw(v, cellSize) {
28+ c2 = cellSize / 2
29+ match v {
30+ case HORIZONTAL : fillRect(0, c2 - 2, cellSize, 4)
31+ case VERTICAL : fillRect(c2 - 2, 0, 4, cellSize)
32+ case LEFT_TO_DOWN : {
33+ fillRect(0, c2 - 2, c2, 4)
34+ fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
35+ }
36+ case LEFT_TO_UP : {
37+ fillRect(0, c2 - 2, c2, 4)
38+ fillRect(c2 - 2, 0, 4, c2 + 2)
39+ }
40+ case RIGHT_TO_UP : {
41+ fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
42+ fillRect(c2 - 2, 0, 4, c2 + 2)
43+ }
44+ case RIGHT_TO_DOWN : {
45+ fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
46+ fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
47+ }
48+ case CROSS : {
49+ fillRect(c2 - 2, 0, 4, cellSize)
50+ fillRect(0, c2 - 2, cellSize, 4)
51+ }
52+ }
53+ }
54+
55+
56+ /// --- PIPES BOARD ---
57+ SIZE = 10
58+
59+ // Creating game board
60+ board = newarray(SIZE, SIZE)
61+ boardGhost = newarray(SIZE, SIZE)
62+
63+ def switchCell(x, y) {
64+ board[x][y] = Cells[board[x][y]].next
65+ }
66+ def setGhostCell(x, y) {
67+ boardGhost[x][y] = Cells[boardGhost[x][y]].next
68+ }
69+
70+
71+ /// --- PIPES MAIN ---
72+ translateX = 0 translateY = 0
73+ isGameFinished = false
74+ isWin = false
75+
76+ /* frect with translate ability */
77+ def fillRect(x,y,w,h) {
78+ frect(translateX+x, translateY+y, w, h)
79+ }
80+
81+ WIDTH = 320 HEIGHT = 320
82+ WINDOW_WIDTH = WIDTH * 2
83+ window("Pipes Online", WINDOW_WIDTH, HEIGHT)
84+ cellSize = WIDTH / SIZE
85+
86+ // cursor
87+ curX = 0 curY = 0
88+ curGhostX = 0 curGhostY = 0
89+
90+ // Initialize client
91+ socket = newSocket("http://localhost:6469")
92+ socket.on("gameStart", def(data) {
93+ data = data[0]
94+ for i=0, i<SIZE, i++
95+ for j=0, j<SIZE, j++
96+ boardGhost[i][j] = board[i][j] = data[i][j]
97+ thread(::gameLoop)
98+ })
99+ .on("updateGhostCell", def(data) {
100+ data = data[0]
101+ setGhostCell(data.x, data.y);
102+ })
103+ .on("updateGhostCursor", def(data) {
104+ data = data[0]
105+ curGhostX = data.x
106+ curGhostY = data.y
107+ })
108+ .on("gameFinished", def(data) {
109+ isGameFinished = true
110+ isWin = data[0]
111+ })
112+ socket.connect()
113+
114+ def gameLoop() {
115+ run = 1
116+ while run {
117+ key = keypressed()
118+ if (!isGameFinished) {
119+ if (key == VK_LEFT && curX > 0) {
120+ curX--
121+ socket.emit("updateCursor", {"x": curX, "y": curY})
122+ } else if (key == VK_RIGHT && curX < SIZE - 1) {
123+ curX++
124+ socket.emit("updateCursor", {"x": curX, "y": curY})
125+ } else if (key == VK_UP && curY > 0) {
126+ curY--
127+ socket.emit("updateCursor", {"x": curX, "y": curY})
128+ } else if (key == VK_DOWN && curY < SIZE - 1) {
129+ curY++
130+ socket.emit("updateCursor", {"x": curX, "y": curY})
131+ } else if (key == VK_FIRE) {
132+ switchCell(curX, curY)
133+ socket.emit("switchCell", {"x": curX, "y": curY})
134+ }
135+ else if (key == 48) run = 0
136+ }
137+
138+ // background
139+ color(isGameFinished ? (isWin ? #66FF66 : #FF6666) : #FFFFFF)
140+ frect(0, 0, WIDTH, HEIGHT)
141+ color(isGameFinished ? (!isWin ? #66FF66 : #FF6666) : #DDDDDD)
142+ frect(WIDTH, 0, WIDTH, HEIGHT)
143+ // cursor
144+ color(#4444FF)
145+ frect(curX*cellSize, curY*cellSize, cellSize, cellSize)
146+ color(#4040DD)
147+ frect(WIDTH + curGhostX*cellSize, curGhostY*cellSize, cellSize, cellSize)
148+ for (i=0, i<SIZE, i++) {
149+ color(0)
150+ ic = i*cellSize
151+ // ourrebt board
152+ line(0, ic, cellSize*SIZE, ic)
153+ line(ic, 0, ic, cellSize*SIZE)
154+ // ghost board
155+ line(WIDTH, ic, WIDTH + cellSize*SIZE, ic)
156+ line(WIDTH + ic, 0, WIDTH + ic, cellSize*SIZE)
157+ color(#FF0000)
158+ for j=0, j<SIZE, j++ {
159+ translateX = ic
160+ translateY = j*cellSize
161+ draw(board[i][j], cellSize)
162+ translateX = -ic
163+ translateY = -j*cellSize
164+ // ghost cells
165+ translateX = WIDTH + ic
166+ translateY = j*cellSize
167+ draw(boardGhost[i][j], cellSize)
168+ translateX = - WIDTH - ic
169+ translateY = -j*cellSize
170+ }
171+ }
172+ repaint()
173+ sleep(50)
174+ }
175+ socket.disconnect()
176+ }
0 commit comments