|
| 1 | +// This demo uses non-standard `kbGet()` and `kbEcho()` |
| 2 | + |
| 3 | +import "vt" |
| 4 | + |
| 5 | +delay = 0.5 |
| 6 | +delaySub = 0.01 |
| 7 | +fieldHeight = 20 |
| 8 | +fieldWidth = 10 |
| 9 | +fieldLeft = 20 |
| 10 | +fieldColor = vt.color.black |
| 11 | +kUp = char(27) + "[A" |
| 12 | +kDown = char(27) + "[B" |
| 13 | +kRight = char(27) + "[C" |
| 14 | +kLeft = char(27) + "[D" |
| 15 | +anchorRow = fieldHeight + 2 |
| 16 | +gameOverFG = vt.color.white |
| 17 | +gameOverBG = vt.color.teal |
| 18 | + |
| 19 | +cell = {} |
| 20 | +cell.empty = null |
| 21 | +cell.I = vt.color.aqua |
| 22 | +cell.O = vt.color.yellow |
| 23 | +cell.T = vt.color.purple |
| 24 | +cell.L = vt.color.orange |
| 25 | +cell.J = vt.color.navy |
| 26 | +cell.S = vt.color.lime |
| 27 | +cell.Z = vt.color.red |
| 28 | +_cell = cell |
| 29 | + |
| 30 | +field = [] |
| 31 | +shape = null |
| 32 | + |
| 33 | +initField = function |
| 34 | + globals.field = [] |
| 35 | + for r in range(0, fieldHeight - 1, 1) |
| 36 | + field.push [] |
| 37 | + for c in range(0, fieldWidth - 1, 1) |
| 38 | + field[-1].push _cell.empty |
| 39 | + end for |
| 40 | + end for |
| 41 | +end function |
| 42 | + |
| 43 | +drawField = function |
| 44 | + for r in range(0, fieldHeight - 1, 1) |
| 45 | + print vt.cursor.goto(r + 1, fieldLeft - 1) + vt.backColor(fieldColor) + "|", "" |
| 46 | + for c in range(0, fieldWidth - 1, 1) |
| 47 | + print vt.backColor(field[r][c]) + " ", "" |
| 48 | + end for |
| 49 | + print vt.backColor(fieldColor) + "|" |
| 50 | + end for |
| 51 | + print vt.cursor.goto(fieldHeight + 1, fieldLeft - 1) + vt.backColor(fieldColor) + "+" + "-" * fieldWidth * 2 + "+" |
| 52 | + print vt.cursor.goto(anchorRow, 1) |
| 53 | +end function |
| 54 | + |
| 55 | +drawGameOver = function |
| 56 | + print vt.cursor.goto(10, fieldLeft) + vt.textColor(gameOverFG) + vt.backColor(gameOverBG) + " G A M E " |
| 57 | + print vt.cursor.goto(11, fieldLeft) + vt.textColor(gameOverFG) + vt.backColor(gameOverBG) + " O V E R " |
| 58 | + print vt.cursor.goto(anchorRow, 1) |
| 59 | +end function |
| 60 | + |
| 61 | +initShape = function |
| 62 | + f = [@initI, |
| 63 | + @initO, |
| 64 | + @initT, |
| 65 | + @initL, |
| 66 | + @initJ, |
| 67 | + @initS, |
| 68 | + @initZ][rnd * 7] |
| 69 | + s = f |
| 70 | + for cell in s.cells |
| 71 | + if field[cell.row][cell.col] != _cell.empty then return null |
| 72 | + end for |
| 73 | + return s |
| 74 | +end function |
| 75 | + |
| 76 | +initI = function |
| 77 | + s = {} |
| 78 | + s.cells = [] |
| 79 | + s.cells.push {"row": 0, "col": 3, "color": _cell.I} |
| 80 | + s.cells.push {"row": 0, "col": 4, "color": _cell.I} |
| 81 | + s.cells.push {"row": 0, "col": 5, "color": _cell.I} |
| 82 | + s.cells.push {"row": 0, "col": 6, "color": _cell.I} |
| 83 | + s.centerRow = 0 |
| 84 | + s.centerCol = 4 |
| 85 | + return s |
| 86 | +end function |
| 87 | + |
| 88 | +initO = function |
| 89 | + s = {} |
| 90 | + s.cells = [] |
| 91 | + s.cells.push {"row": 0, "col": 4, "color": _cell.O} |
| 92 | + s.cells.push {"row": 0, "col": 5, "color": _cell.O} |
| 93 | + s.cells.push {"row": 1, "col": 5, "color": _cell.O} |
| 94 | + s.cells.push {"row": 1, "col": 4, "color": _cell.O} |
| 95 | + s.centerRow = 0.5 |
| 96 | + s.centerCol = 4.5 |
| 97 | + return s |
| 98 | +end function |
| 99 | + |
| 100 | +initT = function |
| 101 | + s = {} |
| 102 | + s.cells = [] |
| 103 | + s.cells.push {"row": 0, "col": 3, "color": _cell.T} |
| 104 | + s.cells.push {"row": 0, "col": 4, "color": _cell.T} |
| 105 | + s.cells.push {"row": 0, "col": 5, "color": _cell.T} |
| 106 | + s.cells.push {"row": 1, "col": 4, "color": _cell.T} |
| 107 | + s.centerRow = 0 |
| 108 | + s.centerCol = 4 |
| 109 | + return s |
| 110 | +end function |
| 111 | + |
| 112 | +initL = function |
| 113 | + s = {} |
| 114 | + s.cells = [] |
| 115 | + s.cells.push {"row": 1, "col": 4, "color": _cell.L} |
| 116 | + s.cells.push {"row": 0, "col": 4, "color": _cell.L} |
| 117 | + s.cells.push {"row": 0, "col": 5, "color": _cell.L} |
| 118 | + s.cells.push {"row": 0, "col": 6, "color": _cell.L} |
| 119 | + s.centerRow = 0.5 |
| 120 | + s.centerCol = 4.5 |
| 121 | + return s |
| 122 | +end function |
| 123 | + |
| 124 | +initJ = function |
| 125 | + s = {} |
| 126 | + s.cells = [] |
| 127 | + s.cells.push {"row": 0, "col": 3, "color": _cell.J} |
| 128 | + s.cells.push {"row": 0, "col": 4, "color": _cell.J} |
| 129 | + s.cells.push {"row": 0, "col": 5, "color": _cell.J} |
| 130 | + s.cells.push {"row": 1, "col": 5, "color": _cell.J} |
| 131 | + s.centerRow = 0.5 |
| 132 | + s.centerCol = 4.5 |
| 133 | + return s |
| 134 | +end function |
| 135 | + |
| 136 | +initS = function |
| 137 | + s = {} |
| 138 | + s.cells = [] |
| 139 | + s.cells.push {"row": 1, "col": 3, "color": _cell.S} |
| 140 | + s.cells.push {"row": 1, "col": 4, "color": _cell.S} |
| 141 | + s.cells.push {"row": 0, "col": 4, "color": _cell.S} |
| 142 | + s.cells.push {"row": 0, "col": 5, "color": _cell.S} |
| 143 | + s.centerRow = 0.5 |
| 144 | + s.centerCol = 4.5 |
| 145 | + return s |
| 146 | +end function |
| 147 | + |
| 148 | +initZ = function |
| 149 | + s = {} |
| 150 | + s.cells = [] |
| 151 | + s.cells.push {"row": 0, "col": 4, "color": _cell.Z} |
| 152 | + s.cells.push {"row": 0, "col": 5, "color": _cell.Z} |
| 153 | + s.cells.push {"row": 1, "col": 5, "color": _cell.Z} |
| 154 | + s.cells.push {"row": 1, "col": 6, "color": _cell.Z} |
| 155 | + s.centerRow = 0.5 |
| 156 | + s.centerCol = 4.5 |
| 157 | + return s |
| 158 | +end function |
| 159 | + |
| 160 | +drawShape = function(s) |
| 161 | + for cell in s.cells |
| 162 | + print vt.cursor.goto(cell.row + 1, fieldLeft + cell.col * 2) + vt.backColor(cell.color) + " " |
| 163 | + end for |
| 164 | + print vt.cursor.goto(anchorRow, 1) |
| 165 | +end function |
| 166 | + |
| 167 | +eraseShape = function(s) |
| 168 | + for cell in s.cells |
| 169 | + print vt.cursor.goto(cell.row + 1, fieldLeft + cell.col * 2) + vt.backColor(fieldColor) + " " |
| 170 | + end for |
| 171 | + print vt.cursor.goto(anchorRow, 1) |
| 172 | +end function |
| 173 | + |
| 174 | +absorbShape = function(s) |
| 175 | + for cell in s.cells |
| 176 | + field[cell.row][cell.col] = cell.color |
| 177 | + end for |
| 178 | +end function |
| 179 | + |
| 180 | +cloneShape = function(s) |
| 181 | + s2 = {} |
| 182 | + s2.cells = [] |
| 183 | + for cell in s.cells |
| 184 | + s2.cells.push cell + {} |
| 185 | + end for |
| 186 | + s2.centerRow = s.centerRow |
| 187 | + s2.centerCol = s.centerCol |
| 188 | + return s2 |
| 189 | +end function |
| 190 | + |
| 191 | +moveDown = function(s) |
| 192 | + s2 = cloneShape(s) |
| 193 | + for cell in s2.cells |
| 194 | + cell.row += 1 |
| 195 | + if cell.row >= fieldHeight then return null |
| 196 | + if field[cell.row][cell.col] != _cell.empty then return null |
| 197 | + end for |
| 198 | + s2.centerRow += 1 |
| 199 | + return s2 |
| 200 | +end function |
| 201 | + |
| 202 | +moveLeft = function(s) |
| 203 | + s2 = cloneShape(s) |
| 204 | + for cell in s2.cells |
| 205 | + cell.col -= 1 |
| 206 | + if cell.col < 0 then return null |
| 207 | + if field[cell.row][cell.col] != _cell.empty then return null |
| 208 | + end for |
| 209 | + s2.centerCol -= 1 |
| 210 | + return s2 |
| 211 | +end function |
| 212 | + |
| 213 | +moveRight = function(s) |
| 214 | + s2 = cloneShape(s) |
| 215 | + for cell in s2.cells |
| 216 | + cell.col += 1 |
| 217 | + if cell.col >= fieldWidth then return null |
| 218 | + if field[cell.row][cell.col] != _cell.empty then return null |
| 219 | + end for |
| 220 | + s2.centerCol += 1 |
| 221 | + return s2 |
| 222 | +end function |
| 223 | + |
| 224 | +fall = function(s) |
| 225 | + s2 = cloneShape(s) |
| 226 | + while true |
| 227 | + moved = moveDown(s2) |
| 228 | + if moved == null then return s2 |
| 229 | + s2 = moved |
| 230 | + end while |
| 231 | +end function |
| 232 | + |
| 233 | +rotate = function(s) |
| 234 | + s2 = cloneShape(s) |
| 235 | + crossedLeftP = false |
| 236 | + crossedRightP = false |
| 237 | + for cell in s2.cells |
| 238 | + r = cell.row |
| 239 | + c = cell.col |
| 240 | + cell.row = s2.centerRow + (c - s2.centerCol) |
| 241 | + cell.col = s2.centerCol - (r - s2.centerRow) |
| 242 | + if cell.col < 0 then crossedLeftP = true |
| 243 | + if cell.col >= fieldWidth then crossedRightP = true |
| 244 | + if cell.row >= fieldHeight then return null |
| 245 | + if field[cell.row][cell.col] != _cell.empty then return null |
| 246 | + end for |
| 247 | + if crossedLeftP then s2 = moveRight(s2) |
| 248 | + if crossedRightP then s2 = moveLeft(s2) |
| 249 | + return s2 |
| 250 | +end function |
| 251 | + |
| 252 | +deleteRows = function |
| 253 | + redrawP = false |
| 254 | + for i in range(0, fieldHeight - 1, 1) |
| 255 | + nNulls = 0 |
| 256 | + for cell in field[i] |
| 257 | + if cell == _cell.empty then nNulls += 1 |
| 258 | + end for |
| 259 | + if nNulls == 0 then |
| 260 | + emptyRow = [] |
| 261 | + for j in range(0, fieldWidth - 1, 1) |
| 262 | + emptyRow.push _cell.empty |
| 263 | + end for |
| 264 | + outer.field = [emptyRow] + field[:i] + field[i + 1:] |
| 265 | + outer.delay -= delaySub |
| 266 | + redrawP = true |
| 267 | + end if |
| 268 | + end for |
| 269 | + if redrawP then drawField |
| 270 | +end function |
| 271 | + |
| 272 | + |
| 273 | +initField |
| 274 | +drawField |
| 275 | +kbEcho false |
| 276 | +t = time |
| 277 | +while true |
| 278 | + yield |
| 279 | + |
| 280 | + if shape == null then |
| 281 | + shape = initShape |
| 282 | + if shape == null then |
| 283 | + drawGameOver |
| 284 | + kbEcho true |
| 285 | + exit |
| 286 | + end if |
| 287 | + drawShape shape |
| 288 | + end if |
| 289 | + |
| 290 | + s = shape |
| 291 | + moved = null |
| 292 | + k = kbGet |
| 293 | + k3 = k[:3] |
| 294 | + if k3 == kUp then |
| 295 | + moved = rotate(s) |
| 296 | + else if k3 == kDown then |
| 297 | + moved = fall(s) |
| 298 | + else if k3 == kLeft then |
| 299 | + moved = moveLeft(s) |
| 300 | + else if k3 == kRight then |
| 301 | + moved = moveRight(s) |
| 302 | + end if |
| 303 | + |
| 304 | + if moved != null then s = moved |
| 305 | + |
| 306 | + if time > t then |
| 307 | + t = time + delay |
| 308 | + moved = moveDown(s) |
| 309 | + if moved == null then |
| 310 | + absorbShape s |
| 311 | + deleteRows |
| 312 | + shape = null |
| 313 | + s = null |
| 314 | + else |
| 315 | + s = moved |
| 316 | + end if |
| 317 | + end if |
| 318 | + |
| 319 | + if s != shape then |
| 320 | + eraseShape shape |
| 321 | + drawShape s |
| 322 | + shape = s |
| 323 | + end if |
| 324 | +end while |
0 commit comments