操作方法は移動は←→↓、スペースキーで右回転、一時停止はEscです。ただしキーをおすとスクロールしてしまうので動作確認はこちらからお願いします。
前回はC#で作成したテトリスをTypeScriptで書き直しましたが、それをコンパイルした結果、以下のようなJavaScriptになります。
まずはHTMLファイルを示します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>鳩でもわかるぷよぷよ</title> </head> <body> <canvas id="can"></cavas> <script type="text/javascript" src="./TypeScript1.js" charset="utf-8"></script> <script type="text/javascript" src="./Field.js" charset="utf-8"></script> <script type="text/javascript" src="./app.js" charset="utf-8"></script> </body> </html> |
TypeScript1.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
class Position { constructor(colum, row) { this.Colum = 0; this.Row = 0; this.Colum = colum; this.Row = row; } } var Angle; (function (Angle) { Angle[Angle["Angle0"] = 0] = "Angle0"; Angle[Angle["Angle90"] = 1] = "Angle90"; Angle[Angle["Angle180"] = 2] = "Angle180"; Angle[Angle["Angle270"] = 3] = "Angle270"; })(Angle || (Angle = {})); class Cell { constructor(x, y) { this.X = 0; this.Y = 0; this.Colum = 0; this.Row = 0; this.IsFixed = false; this.Puyo = Puyo.None; this.Rensa = 0; this.IsChecked = false; this.X = x; this.Y = y; } get Width() { return 30; } get Height() { return 30; } SetColumRow(colum, row) { this.Colum = colum; this.Row = row; } } var Puyo; (function (Puyo) { Puyo[Puyo["None"] = 0] = "None"; Puyo[Puyo["Wall"] = -1] = "Wall"; Puyo[Puyo["Puyo1"] = 1] = "Puyo1"; Puyo[Puyo["Puyo2"] = 2] = "Puyo2"; Puyo[Puyo["Puyo3"] = 3] = "Puyo3"; Puyo[Puyo["Puyo4"] = 4] = "Puyo4"; })(Puyo || (Puyo = {})); |
Field.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; class Field { static InitCells() { this.InitPuyoImages(); this.puyoPositionX = this.puyoStartPositionY; this.puyoPositionY = this.puyoStartPositionY; this.Cells = []; for (let i = 0; i < this.FIELD_HEIGHT; i++) { this.Cells[i] = []; } let cell = new Cell(0, 0); let selWidth = cell.Width; let selHeight = cell.Height; for (let i = 0; i < this.FIELD_HEIGHT; i++) { for (let j = 0; j < this.FIELD_WIDTH; j++) { this.Cells[i][j] = new Cell(j * selWidth, (i - 1) * selHeight); this.Cells[i][j].SetColumRow(j, i); } } // 外枠をつくる for (let i = 0; i < this.FIELD_HEIGHT; i++) this.SetPuyo(0, i, Puyo.Wall); for (let i = 0; i < this.FIELD_HEIGHT; i++) this.SetPuyo(this.FIELD_WIDTH - 1, i, Puyo.Wall); for (let i = 0; i < this.FIELD_WIDTH; i++) this.SetPuyo(i, this.FIELD_HEIGHT - 1, Puyo.Wall); } static InitPuyoImages() { this.RedPuyoImage = this.GetHTMLImageElement("./images/Red.png"); this.BluePuyoImage = this.GetHTMLImageElement("./images/Blue.png"); this.GreenPuyoImage = this.GetHTMLImageElement("./images/Green.png"); this.YellowPuyoImage = this.GetHTMLImageElement("./images/Yellow.png"); this.WallImage = this.GetHTMLImageElement("./images/Wall.png"); this.RedImages.push(this.GetHTMLImageElement("./images/Red01.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red02.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red03.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red04.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red05.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red06.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red07.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red08.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red09.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red10.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red10.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red11.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red12.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red13.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red14.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red15.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red16.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red17.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red18.png")); this.RedImages.push(this.GetHTMLImageElement("./images/Red19.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green01.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green02.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green03.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green04.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green05.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green06.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green07.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green08.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green09.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green10.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green10.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green11.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green12.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green13.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green14.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green15.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green16.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green17.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green18.png")); this.GreenImages.push(this.GetHTMLImageElement("./images/Green19.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue01.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue02.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue03.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue04.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue05.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue06.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue07.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue08.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue09.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue10.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue10.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue11.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue12.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue13.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue14.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue15.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue16.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue17.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue18.png")); this.BlueImages.push(this.GetHTMLImageElement("./images/Blue19.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow01.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow02.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow03.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow04.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow05.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow06.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow07.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow08.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow09.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow10.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow10.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow11.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow12.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow13.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow14.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow15.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow16.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow17.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow18.png")); this.YellowImages.push(this.GetHTMLImageElement("./images/Yellow19.png")); } static GetHTMLImageElement(path) { let img = new Image(); img.src = path; return img; } static SetPuyo(colum, row, puyo) { let cell = this.Cells[row][colum]; cell.Puyo = puyo; cell.Rensa = 0; Draw(); } static CreateNewPuyo() { this.PuyoAngle = Angle.Angle0; let r = this.GetRandomInt(4); if (r == 0) this.DropingPuyo = Puyo.Puyo1; if (r == 1) this.DropingPuyo = Puyo.Puyo2; if (r == 2) this.DropingPuyo = Puyo.Puyo3; if (r == 3) this.DropingPuyo = Puyo.Puyo4; r = this.GetRandomInt(4); if (r == 0) this.SubDropingPuyo = Puyo.Puyo1; if (r == 1) this.SubDropingPuyo = Puyo.Puyo2; if (r == 2) this.SubDropingPuyo = Puyo.Puyo3; if (r == 3) this.SubDropingPuyo = Puyo.Puyo4; this.puyoPositionX = this.puyoStartPositionX; this.puyoPositionY = this.puyoStartPositionY; this.SetPuyo(this.puyoPositionX, this.puyoPositionY, this.DropingPuyo); this.SetPuyo(this.puyoPositionX, this.puyoPositionY - 1, this.SubDropingPuyo); if (this.JudgeGameOver()) this.IsGameOver = true; } static GetRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } static GetSubPuyoPosition(mainColun, mainRow, angle) { if (angle == Angle.Angle0) return new Position(mainColun, mainRow - 1); if (angle == Angle.Angle90) return new Position(mainColun + 1, mainRow); if (angle == Angle.Angle180) return new Position(mainColun, mainRow + 1); if (angle == Angle.Angle270) return new Position(mainColun - 1, mainRow); return null; } static GetColorPuyoType(puyo) { if (puyo == Puyo.None) return "black"; else if (puyo == Puyo.Puyo1) return "red"; else if (puyo == Puyo.Puyo2) return "blue"; else if (puyo == Puyo.Puyo3) return "lightgreen"; else if (puyo == Puyo.Puyo4) return "yellow"; else if (puyo == Puyo.Wall) return "darkgray"; else return ""; } static PuyoMoveLeftIfCan() { let oldX = this.puyoPositionX; let oldY = this.puyoPositionY; let subPosition = this.GetSubPuyoPosition(this.puyoPositionX, this.puyoPositionY, this.PuyoAngle); let oldSubX = subPosition.Colum; let oldSubY = subPosition.Row; // 移動できるのか? let newX = this.puyoPositionX - 1; let newY = this.puyoPositionY; let newSubX = oldSubX - 1; let newSubY = subPosition.Row; let ret1 = this.CanMove(new Position(newX, newY)); let ret2 = this.CanMove(new Position(newSubX, newSubY)); if (ret1 && ret2) { this.SetPuyo(oldX, oldY, Puyo.None); this.SetPuyo(oldSubX, oldSubY, Puyo.None); this.puyoPositionX = newX; this.puyoPositionY = newY; this.SetPuyo(this.puyoPositionX, this.puyoPositionY, this.DropingPuyo); this.SetPuyo(newSubX, newSubY, this.SubDropingPuyo); } } static PuyoMoveRightIfCan() { let oldX = this.puyoPositionX; let oldY = this.puyoPositionY; let subPosition = this.GetSubPuyoPosition(this.puyoPositionX, this.puyoPositionY, this.PuyoAngle); let oldSubX = subPosition.Colum; let oldSubY = subPosition.Row; // 移動できるのか? let newX = this.puyoPositionX + 1; let newY = this.puyoPositionY; let newSubX = oldSubX + 1; let newSubY = subPosition.Row; let ret1 = this.CanMove(new Position(newX, newY)); let ret2 = this.CanMove(new Position(newSubX, newSubY)); if (ret1 && ret2) { this.SetPuyo(oldX, oldY, Puyo.None); if (oldSubY > -1) this.SetPuyo(oldSubX, oldSubY, Puyo.None); this.puyoPositionX = newX; this.puyoPositionY = newY; this.SetPuyo(this.puyoPositionX, this.puyoPositionY, this.DropingPuyo); this.SetPuyo(newSubX, newSubY, this.SubDropingPuyo); } } static PuyoMoveDownIfCan() { let oldX = this.puyoPositionX; let oldY = this.puyoPositionY; let subPosition = this.GetSubPuyoPosition(this.puyoPositionX, this.puyoPositionY, this.PuyoAngle); let oldSubX = subPosition.Colum; let oldSubY = subPosition.Row; // 移動できるのか? let newX = this.puyoPositionX; let newY = this.puyoPositionY + 1; let newSubX = oldSubX; let newSubY = subPosition.Row + 1; // 下げられないなら固定する if (!this.CanMoveDown(new Position(newX, newY)) || !this.CanMoveDown(new Position(newSubX, newSubY))) { this.FixPuyo(oldX, oldY); this.FixPuyo(oldSubX, oldSubY); this.OnFixed(); return; } this.SetPuyo(oldX, oldY, Puyo.None); this.SetPuyo(oldSubX, oldSubY, Puyo.None); this.puyoPositionY = newY; this.SetPuyo(this.puyoPositionX, this.puyoPositionY, this.DropingPuyo); this.SetPuyo(newSubX, newSubY, this.SubDropingPuyo); } static CanMoveDown(newPos) { if (newPos.Row < 0 || newPos.Row > this.FIELD_HEIGHT - 2 || this.Cells[newPos.Row][newPos.Colum].IsFixed) return false; else return true; } static PuyoRotateIfCan() { let oldSubPos = this.GetSubPuyoPosition(this.puyoPositionX, this.puyoPositionY, this.PuyoAngle); let angle = Angle.Angle0; if (this.PuyoAngle == Angle.Angle0) angle = Angle.Angle90; else if (this.PuyoAngle == Angle.Angle90) angle = Angle.Angle180; else if (this.PuyoAngle == Angle.Angle180) angle = Angle.Angle270; else if (this.PuyoAngle == Angle.Angle270) angle = Angle.Angle0; let newSubPos = this.GetSubPuyoPosition(this.puyoPositionX, this.puyoPositionY, angle); if (this.CanMove(newSubPos)) { this.PuyoAngle = angle; this.SetPuyo(oldSubPos.Colum, oldSubPos.Row, Puyo.None); this.SetPuyo(newSubPos.Colum, newSubPos.Row, this.SubDropingPuyo); } } static CanMove(newPos) { if (newPos.Colum < 0 || newPos.Colum > this.FIELD_WIDTH - 1 || newPos.Row < 0 || newPos.Row > this.FIELD_HEIGHT - 1 || this.Cells[newPos.Row][newPos.Colum].IsFixed || this.Cells[newPos.Row][newPos.Colum].Puyo == Puyo.Wall) return false; else return true; } static OnFixed() { return __awaiter(this, void 0, void 0, function* () { IgnoreKeyDown = true; IgnoreTimer = true; let rensa = 1; let tempScore = 0; while (true) { let task = this.DeletePuyoIfNeed(rensa); let ret = yield task; if (ret == 0) break; tempScore += ret; rensa++; } this.Score += tempScore; IgnoreKeyDown = false; IgnoreTimer = false; this.CreateNewPuyo(); }); } static Sleep(ms) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(); }, ms); }); } static DeletePuyoIfNeed(rensa) { return __awaiter(this, void 0, void 0, function* () { // 設置されたプヨの下に空洞があれば上のプヨを落として空洞をうめる let task = this.DownPuyoIfSpaces(); yield task; let deleteCells = []; // 連結ボーナス計算用 let vs = []; // 4つつながっているぷよがあるか探す for (let i = 0; i < Field.FIELD_HEIGHT; i++) { for (let j = 0; j < Field.FIELD_WIDTH; j++) { let cell = Field.Cells[i][j]; if (deleteCells.some(x => x == cell)) continue; let cells = Field.GetConectedCell(cell); Field.ClearCheck(); if (cells.length >= 4) { cells.forEach(x => { x.Rensa = rensa; // 消えるぷよに連鎖回数がわかるように番号をつける deleteCells.push(x); }); vs.push(cells.length); } } } // ぷよの消えた数 ×(連鎖ボーナス+連結ボーナス+色数ボーナス)× 10 // ぷよの消えた数 deleteCells let puyoCount = deleteCells.length; // 連鎖ボーナス rensa let rensaBonus = Field.GetRensaBonus(rensa); // 連結ボーナス cells.Count let renketsuBonus = 0; vs.forEach(x => { renketsuBonus += Field.GetRenketsuBonus(x); }); //let renketsuBonus = vs.Sum(x => Field.GetRenketsuBonus(x)); // 色数ボーナス let colorCount = 0; if (deleteCells.some(x => x.Puyo == Puyo.Puyo1)) colorCount++; if (deleteCells.some(x => x.Puyo == Puyo.Puyo2)) colorCount++; if (deleteCells.some(x => x.Puyo == Puyo.Puyo3)) colorCount++; if (deleteCells.some(x => x.Puyo == Puyo.Puyo4)) colorCount++; let colorsBonus = Field.GetColorCountBonus(colorCount); let score = 0; if (rensaBonus + renketsuBonus + colorsBonus != 0) score = puyoCount * (rensaBonus + renketsuBonus + colorsBonus) * 10; else score = puyoCount * 10; Draw(); yield this.Sleep(200); // ぷよを実際に消す deleteCells.forEach(x => { x.Puyo = Puyo.None; }); if (deleteCells.length != 0) { yield this.Sleep(200); Draw(); } return score; }); } static DownPuyoIfSpaces() { return __awaiter(this, void 0, void 0, function* () { let ret = false; while (Field.DownPuyoIfSpace()) { ret = true; yield this.Sleep(100); } return ret; }); } static FixPuyo(colum, row) { this.Cells[row][colum].IsFixed = true; } static ClearCheck() { for (let i = 0; i < this.FIELD_HEIGHT; i++) { for (let j = 0; j < this.FIELD_WIDTH; j++) { let cell = this.Cells[i][j]; cell.IsChecked = false; } } } static GetConectedCell(cell) { let colum = cell.Colum; let row = cell.Row; let cells = []; if (0 < colum && colum < this.FIELD_WIDTH - 1 && 0 < row && row < this.FIELD_HEIGHT - 1) { let puyo = this.Cells[row][colum].Puyo; if (puyo == Puyo.None || puyo == Puyo.Wall) return cells; if (!this.Cells[row + 1][colum].IsChecked) { this.Cells[row + 1][colum].IsChecked = true; if (this.Cells[row + 1][colum].Puyo == puyo) { cells.push(this.Cells[row + 1][colum]); let newCell = this.GetConectedCell(this.Cells[row + 1][colum]); newCell.forEach(x => { cells.push(x); }); } } if (!this.Cells[row - 1][colum].IsChecked) { this.Cells[row - 1][colum].IsChecked = true; if (this.Cells[row - 1][colum].Puyo == puyo) { cells.push(this.Cells[row - 1][colum]); let newCell = this.GetConectedCell(this.Cells[row - 1][colum]); newCell.forEach(x => { cells.push(x); }); } } if (!this.Cells[row][colum + 1].IsChecked) { this.Cells[row][colum + 1].IsChecked = true; if (this.Cells[row][colum + 1].Puyo == puyo) { cells.push(this.Cells[row][colum + 1]); let newCell = this.GetConectedCell(this.Cells[row][colum + 1]); newCell.forEach(x => { cells.push(x); }); } } if (!this.Cells[row][colum - 1].IsChecked) { this.Cells[row][colum - 1].IsChecked = true; if (this.Cells[row][colum - 1].Puyo == puyo) { cells.push(this.Cells[row][colum - 1]); let newCell = this.GetConectedCell(this.Cells[row][colum - 1]); newCell.forEach(x => { cells.push(x); }); } } } return cells; } static DownPuyoIfSpace() { let ret = false; for (let y = this.FIELD_HEIGHT - 3; y >= 0; y--) { for (let x = 0; x < this.FIELD_WIDTH; x++) { if (this.Cells[y + 1][x].Puyo == Puyo.None && this.Cells[y][x].Puyo != Puyo.None) { // 下に空間があるぷよは下に落とす this.SetPuyo(x, y + 1, this.Cells[y][x].Puyo); this.Cells[y + 1][x].IsFixed = this.Cells[y][x].IsFixed; this.SetPuyo(x, y, Puyo.None); this.Cells[y][x].IsFixed = false; ret = true; } } } if (ret) Draw(); return ret; } static GetRenketsuBonus(i) { if (i == 4) return 0; if (i >= 5 || i <= 10) return i - 3; if (i < 11) return 10; return 0; } static GetRensaBonus(i) { if (i == 1) return 0; if (i == 2) return 8; if (i == 3) return 16; if (i >= 4 || i <= 19) return (i - 3) * 32; if (i > 19) return 512; return 0; } static GetColorCountBonus(i) { if (i == 1) return 0; if (i == 2) return 3; if (i == 3) return 6; if (i == 4) return 12; if (i == 5) return 24; return 0; } static GetImageFromPuyoType(puyo) { if (puyo == Puyo.Puyo1) return this.RedPuyoImage; else if (puyo == Puyo.Puyo2) return this.BluePuyoImage; else if (puyo == Puyo.Puyo3) return this.GreenPuyoImage; else if (puyo == Puyo.Puyo4) return this.YellowPuyoImage; else if (puyo == Puyo.Wall) return this.WallImage; return null; } static GetImageFromPuyoTypeRensa(puyo, rensa) { if (puyo == Puyo.Puyo1) { if (rensa == 0) return this.RedPuyoImage; if (rensa < this.RedImages.length) return this.RedImages[rensa - 1]; else return this.RedImages[this.RedImages.length - 1]; } if (puyo == Puyo.Puyo2) { if (rensa == 0) return this.BluePuyoImage; if (rensa < this.BlueImages.length) return this.BlueImages[rensa - 1]; else return this.BlueImages[this.BlueImages.length - 1]; } if (puyo == Puyo.Puyo3) { if (rensa == 0) return this.GreenPuyoImage; if (rensa < this.GreenImages.length) return this.GreenImages[rensa - 1]; else return this.GreenImages[this.GreenImages.length - 1]; } if (puyo == Puyo.Puyo4) { if (rensa == 0) return this.YellowPuyoImage; if (rensa < this.YellowImages.length) return this.YellowImages[rensa - 1]; else return this.YellowImages[this.YellowImages.length - 1]; } if (puyo == Puyo.Wall) { return this.WallImage; } return null; } static JudgeGameOver() { let newX = this.puyoPositionX; let newY = this.puyoPositionY; return !this.CanMove(new Position(newX, newY)); } } Field.Cells = null; Field.FIELD_WIDTH = 6 + 2; Field.FIELD_HEIGHT = 12 + 1 + 1; Field.DropingPuyo = Puyo.None; Field.SubDropingPuyo = Puyo.None; Field.puyoStartPositionX = 3; Field.puyoStartPositionY = 1; Field.puyoPositionX = 0; Field.puyoPositionY = 0; Field.PuyoAngle = Angle.Angle0; Field.RedPuyoImage = null; Field.BluePuyoImage = null; Field.GreenPuyoImage = null; Field.YellowPuyoImage = null; Field.WallImage = null; Field.RedImages = []; Field.GreenImages = []; Field.BlueImages = []; Field.YellowImages = []; Field.Score = 0; Field.IsGameOver = false; |
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
let FieldPosX = 28; let FieldPosY = 98; let can = document.getElementById('can'); var con = can.getContext("2d"); can.width = 300; can.height = 500; can.style.border = "1px solid #333"; let IgnoreTimer = false; let IgnoreKeyDown = false; let Pause = false; Field.InitCells(); Field.CreateNewPuyo(); document.onkeydown = function (e) { if (IgnoreKeyDown) return; if (e.keyCode == 83 && Field.IsGameOver) { Field.IsGameOver = false; Field.InitCells(); Field.Score = 0; Field.CreateNewPuyo(); } if (Field.IsGameOver) return; if (e.keyCode == 27) Pause = !Pause; if (Pause) return; if (e.keyCode == 37) Field.PuyoMoveLeftIfCan(); if (e.keyCode == 39) Field.PuyoMoveRightIfCan(); if (e.keyCode == 40) Field.PuyoMoveDownIfCan(); if (e.keyCode == 32) Field.PuyoRotateIfCan(); }; function Draw() { con.fillStyle = "black"; con.fillRect(0, 0, can.width, can.height); for (let i = 0; i < Field.FIELD_HEIGHT; i++) { for (let j = 0; j < Field.FIELD_WIDTH; j++) { let cell = Field.Cells[i][j]; let x = FieldPosX + cell.X; let y = FieldPosY + cell.Y; let image = null; image = Field.GetImageFromPuyoTypeRensa(cell.Puyo, cell.Rensa); if (image != null) { con.drawImage(image, x, y, cell.Width, cell.Height); } } } let str = "SCORE " + Field.Score; con.font = "bold 18px 'MS UI Gothic'"; con.fillStyle = "white"; con.fillText(str, 28, 34); if (Field.IsGameOver) { con.font = "bold 28px 'MS UI Gothic'"; con.fillStyle = "white"; con.fillText("GAME OVER", 54, 150); } if (Pause) { con.font = "bold 28px 'MS UI Gothic'"; con.fillStyle = "white"; con.fillText("Pause", 94, 150); } } setInterval(MoveDown, 800); function MoveDown() { if (IgnoreTimer || Field.IsGameOver || Pause) { Draw(); return; } Field.PuyoMoveDownIfCan(); } |