操作方法は移動は←→↓、ハードドロップはスペースキー、左回転Z、右回転X、一時停止はEscです。ただしキーをおすとスクロールしてしまうので動作確認はこちらからお願いします。
C#で作成したテトリスをJavaScriptで書き直そうとしたのですが、エラー続出。コンパイラ言語と違ってコンパイルエラーがでないため、実行前にエラーに気づくことができないわけです。そして実行すると「あれ~~動かない」の連続
調べてみるとTypeScriptというものがありました。これだとコーディングをしているときにエラーに気づくことができます。
PictureBoxに頼らずC#でテトリスをつくる (その1)とMovingTetriminoクラス PictureBoxに頼らずC#でテトリスをつくる (その2)で作成したC#のコードをTypeScriptで書き直してみました。
tsconfig.json
1 2 3 4 5 6 7 8 9 10 11 |
{ "compilerOptions": { "module": "commonjs", "target": "es6", "lib": [ "es2019", "dom", "dom.iterable" ], "sourceMap": false }, "exclude": [ "node_modules" ] } |
PlaySoundeffect.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class PlaySoundeffect { public static Rotate(): void { let audioElm = new Audio('./mp3/rotate.mp3'); audioElm.play(); } public static Drop(): void { let audioElm = new Audio('./mp3/drop.mp3'); audioElm.play(); } public static Delete(): void { let audioElm = new Audio('./mp3/delete.mp3'); audioElm.play(); } } |
Tetrimino.ts
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 |
class Constant { public static FieldColumMax = 10; public static FieldRowMax = 20; public static BlockSize = 20; public static LeftTopBlockPositionX = 150; public static LeftTopBlockPositionY = 70; } enum TetriminoTypes { // I - テトリミノ(水色) // O - テトリミノ(黄色) // S - テトリミノ(緑) // Z - テトリミノ(赤) // J - テトリミノ(青) // L - テトリミノ(オレンジ) // T - テトリミノ(紫) I, O, S, Z, J, L, T, None, } enum TetriminoAngle { Angle0, Angle90, Angle180, Angle270, } |
Block.ts
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 |
class Block { public constructor(posX: number, posY: number) { this.Color = "gray"; this.PosX = posX; this.PosY = posY; } private _posX: number; set PosX(value) { this._posX = value; } get PosX(): number { return this._posX; } private _posY: number; set PosY(value) { this._posY = value; } get PosY(): number { return this._posY; } private _color: string; set Color(value) { this._color = value; } get Color(): string { return this._color; } get X(): number { return this.Width * this.PosX + Constant.LeftTopBlockPositionX; } get Y(): number { return this.Height * this.PosY + Constant.LeftTopBlockPositionY; } get Width() { return Constant.BlockSize; } get Height() { return Constant.BlockSize; } public Draw(con): void { con.fillStyle = this.Color; con.fillRect(this.X, this.Y, this.Width, this.Height); con.strokeStyle = "black"; con.strokeRect(this.X, this.Y, this.Width, this.Height); } } |
MovingTetrimino.ts
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 |
class MovingTetrimino { private static _posX: number; static set PosX(value) { this._posX = value; } static get PosX(): number { return this._posX; } private static _posY: number; static set PosY(value) { this._posY = value; } static get PosY(): number { return this._posY; } private static _angle: TetriminoAngle; static set Angle(value) { this._angle = value; } static get Angle() { return this._angle; } public static GetBlocks(angle: TetriminoAngle): Block[] { let blocks: Block[] = []; let color: string = ""; if (this.Type == TetriminoTypes.I) { let blocks0: Block[] = []; blocks0.push(new Block(0, 1)); blocks0.push(new Block(1, 1)); blocks0.push(new Block(2, 1)); blocks0.push(new Block(3, 1)); blocks = this.GetRotateBlocks44(blocks0, angle); color = TetriminoColor.I; } if (this.Type == TetriminoTypes.J) { let blocks0: Block[] = []; blocks0.push(new Block(0, 0)); blocks0.push(new Block(0, 1)); blocks0.push(new Block(1, 1)); blocks0.push(new Block(2, 1)); blocks = this.GetRotateBlocks33(blocks0, angle); color = TetriminoColor.J; } if (this.Type == TetriminoTypes.L) { let blocks0: Block[] = []; blocks0.push(new Block(2, 0)); blocks0.push(new Block(0, 1)); blocks0.push(new Block(1, 1)); blocks0.push(new Block(2, 1)); blocks = this.GetRotateBlocks33(blocks0, angle); color = TetriminoColor.L; } if (this.Type == TetriminoTypes.O) { blocks.push(new Block(1, 0)); blocks.push(new Block(1, 1)); blocks.push(new Block(2, 0)); blocks.push(new Block(2, 1)); color = TetriminoColor.O; } if (this.Type == TetriminoTypes.S) { let blocks0: Block[] = []; blocks0.push(new Block(1, 0)); blocks0.push(new Block(2, 0)); blocks0.push(new Block(0, 1)); blocks0.push(new Block(1, 1)); blocks = this.GetRotateBlocks33(blocks0, angle); color = TetriminoColor.S; } if (this.Type == TetriminoTypes.Z) { let blocks0: Block[] = []; blocks0.push(new Block(0, 0)); blocks0.push(new Block(1, 0)); blocks0.push(new Block(1, 1)); blocks0.push(new Block(2, 1)); blocks = this.GetRotateBlocks33(blocks0, angle); color = TetriminoColor.Z; } if (this.Type == TetriminoTypes.T) { let blocks0: Block[] = []; blocks0.push(new Block(1, 0)); blocks0.push(new Block(0, 1)); blocks0.push(new Block(1, 1)); blocks0.push(new Block(2, 1)); blocks = this.GetRotateBlocks33(blocks0, angle); color = TetriminoColor.T; } let ret: Block[] = []; blocks.forEach(block0 => { let block: Block = new Block(block0.PosX + this.PosX, block0.PosY + this.PosY); block.Color = color; ret.push(block); }); return ret; } static GetRotateBlocks33(blocks: Block[], angle: TetriminoAngle): Block[] { if (angle == TetriminoAngle.Angle0) return blocks; else if (angle == TetriminoAngle.Angle90) return blocks.map(x => new Block(3 - x.PosY - 1, x.PosX)); else if (angle == TetriminoAngle.Angle180) return blocks.map(x => new Block(3 - x.PosX - 1, 3 - x.PosY - 1)); else// if (angle == TetriminoAngle.Angle270) return blocks.map(x => new Block(x.PosY, 3 - x.PosX - 1)); } static GetRotateBlocks44(blocks: Block[], angle: TetriminoAngle): Block[] { if (angle == TetriminoAngle.Angle0) return blocks; else if (angle == TetriminoAngle.Angle90) return blocks.map(x => new Block(4 - x.PosY - 1, x.PosX)); else if (angle == TetriminoAngle.Angle180) return blocks.map(x => new Block(4 - x.PosX - 1, 4 - x.PosY - 1)); else// if (angle == TetriminoAngle.Angle270) return blocks.map(x => new Block(x.PosY, 4 - x.PosX - 1)); } public static MoveLeft():void { let blocks: Block[] = this.GetMoveLeft(); if (Math.min.apply(null, blocks.map(function (o) { return o.PosX; })) >= 0 && !FixedTetrimino.ExistsBlock(blocks)) this.PosX--; } public static MoveRight():void { let blocks: Block[] = this.GetMoveRight(); if (Math.max.apply(null, blocks.map(function (o) { return o.PosX; })) < Constant.FieldColumMax && !FixedTetrimino.ExistsBlock(blocks)) this.PosX++; } public static MoveDown(): void { let blocks: Block[] = this.GetMoveDown(); if (Math.max.apply(null, blocks.map(function (o) { return o.PosY; })) >= Constant.FieldRowMax) { this.FixTetrimino(); return; } if (FixedTetrimino.ExistsBlock(blocks)) { this.FixTetrimino(); return; } this.PosY++; } static GetMoveLeft(): Block[] { let ret: Block[] = this.GetBlocks(this.Angle); return ret.map(x => new Block(x.PosX - 1, x.PosY)); } static GetMoveRight(): Block[] { let ret: Block[] = this.GetBlocks(this.Angle); return ret.map(x => new Block(x.PosX + 1, x.PosY)); } static GetMoveDown(): Block[] { let ret: Block[] = this.GetBlocks(this.Angle); return ret.map(x => new Block(x.PosX, x.PosY + 1)); } static RotateL(): void { let result: SuperRotationResult = null; if (this.Type != TetriminoTypes.I && this.Type != TetriminoTypes.O) result = this.GetSuperRotationResultLeftTSZLJ(); if (this.Type == TetriminoTypes.I) result = this.GetSuperRotationResultLeftI(); if (result != null) { this.Angle = this.GetAngleRotateL(); this.PosX += result.MoveX; this.PosY += result.MoveY; PlaySoundeffect.Rotate(); } } public static RotateR():void { let result: SuperRotationResult = null; if (this.Type != TetriminoTypes.I && this.Type != TetriminoTypes.O) result = this.GetSuperRotationResultRightTSZLJ(); if (this.Type == TetriminoTypes.I) result = this.GetSuperRotationResultRightI(); if (result != null) { this.Angle = this.GetAngleRotateR(); this.PosX += result.MoveX; this.PosY += result.MoveY; PlaySoundeffect.Rotate(); } } public static CanRotate(blocks: Block[]): boolean { if (Math.min.apply(null, blocks.map(function (o) { return o.PosX; })) < 0) return false; if (Math.max.apply(null, blocks.map(function (o) { return o.PosX; })) >= Constant.FieldColumMax) return false; if (Math.max.apply(null, blocks.map(function (o) { return o.PosY; })) >= Constant.FieldRowMax) return false; if (FixedTetrimino.ExistsBlock(blocks)) return false; return true; } static GetAngleRotateL(): TetriminoAngle { if (this.Angle == TetriminoAngle.Angle0) return TetriminoAngle.Angle270; else if (this.Angle == TetriminoAngle.Angle90) return TetriminoAngle.Angle0; else if (this.Angle == TetriminoAngle.Angle180) return TetriminoAngle.Angle90; else// if (Angle == TetriminoAngle.Angle270) return TetriminoAngle.Angle180; } static GetAngleRotateR(): TetriminoAngle { if (this.Angle == TetriminoAngle.Angle0) return TetriminoAngle.Angle90; else if (this.Angle == TetriminoAngle.Angle90) return TetriminoAngle.Angle180; else if (this.Angle == TetriminoAngle.Angle180) return TetriminoAngle.Angle270; else// if (Angle == TetriminoAngle.Angle270) return TetriminoAngle.Angle0; } static GetSuperRotationResultLeftTSZLJ(): SuperRotationResult { let angle:TetriminoAngle = this.GetAngleRotateL(); let blocks: Block[] = this.GetBlocks(angle); if (this.CanRotate(blocks)) return new SuperRotationResult(0, 0); return SuperRotation.GetRotateL_TSZLJ(blocks, angle); } static GetSuperRotationResultRightTSZLJ(): SuperRotationResult { let angle: TetriminoAngle = this.GetAngleRotateR(); let blocks: Block[] = this.GetBlocks(angle); if (this.CanRotate(blocks)) return new SuperRotationResult(0, 0); return SuperRotation.GetRotateR_TSZLJ(blocks, angle); } static GetSuperRotationResultRightI(): SuperRotationResult { let angle: TetriminoAngle = this.GetAngleRotateR(); let blocks: Block[] = this.GetBlocks(angle); if (this.CanRotate(blocks)) return new SuperRotationResult(0, 0); return SuperRotation.GetRotateR_I(blocks, angle); } static GetSuperRotationResultLeftI(): SuperRotationResult { let angle: TetriminoAngle = this.GetAngleRotateL(); let blocks: Block[] = this.GetBlocks(angle); if (this.CanRotate(blocks)) return new SuperRotationResult(0, 0); return SuperRotation.GetRotateL_I(blocks, angle); } public static DropPoint = 0; public static HardDrop():void { let count: number = 0; count = this.GetHardDropCount(); this.PosY += count; this.DropPoint = count; this.FixTetrimino(); } static GetHardDropedBlocks(): Block[] { let i = this.GetHardDropCount(); let blocks = this.GetBlocks(this.Angle).map(x => { let block: Block = new Block(x.PosX, x.PosY + i); block.Color = x.Color; return block; }); return blocks; } static GetHardDropCount(): number { let blocks: Block[] = this.GetMoveDown(); let i: number = 0; while (Math.max.apply(null, blocks.map(function (o) { return o.PosY; })) < Constant.FieldRowMax && !FixedTetrimino.ExistsBlock(blocks)) { i++; blocks = blocks.map(x => new Block(x.PosX, x.PosY + 1)); } return i; } static TetriminoFixed = false; static FixTetrimino():void { let blocks: Block[] = this.GetBlocks(this.Angle); blocks.forEach(block => { FixedTetrimino.Blocks.push(block); }); PlaySoundeffect.Drop(); FixedTetrimino.DeleteLines(); this.Type = this.PopNext(); this.TetriminoFixed = true; } static _type: TetriminoTypes = TetriminoTypes.None; static GameOver = false; static set Type(value) { this._type = value; this.PosX = 3; this.PosY = -3; this.Angle = TetriminoAngle.Angle0; if (FixedTetrimino.ExistsBlock(this.GetBlocks(this.Angle))) { this.GameOver = true; } } static get Type(): TetriminoTypes { return this._type; } static NextTetoroTypes: TetriminoTypes[] = []; public static Init(): void { this.NextTetoroTypes = []; this.GetNext7(); this.Type = this.PopNext(); } static CreateBag():void { let r: number; let types: TetriminoTypes[] = []; types.push(TetriminoTypes.I); r = Math.floor(Math.random() * (types.length + 1)); types.splice(r, 0, TetriminoTypes.O); r = Math.floor(Math.random() * (types.length + 1)); types.splice(r, 0, TetriminoTypes.S); r = Math.floor(Math.random() * (types.length + 1)); types.splice(r, 0, TetriminoTypes.T); r = Math.floor(Math.random() * (types.length + 1)); types.splice(r, 0, TetriminoTypes.Z); r = Math.floor(Math.random() * (types.length + 1)); types.splice(r, 0, TetriminoTypes.J); r = Math.floor(Math.random() * (types.length + 1)); types.splice(r, 0, TetriminoTypes.L); Array.prototype.push.apply(this.NextTetoroTypes, types); } public static GetNext(): TetriminoTypes { if (this.NextTetoroTypes.length == 0) this.CreateBag(); return this.NextTetoroTypes[0]; } public static GetNext7(): TetriminoTypes[] { if (this.NextTetoroTypes.length < 7) this.CreateBag(); return this.NextTetoroTypes.slice(0, 7); } public static PopNext(): TetriminoTypes { let ret: TetriminoTypes = this.GetNext(); this.NextTetoroTypes.splice(0, 1); return ret; } public static Draw(con): void { if (this.Type == TetriminoTypes.None) return; let blocks: Block[] = this.GetBlocks(this.Angle); blocks.forEach(block => { block.Draw(con); }); } public static DrawGohst(con):void { if (this.Type == TetriminoTypes.None) return; let blocks: Block[] = this.GetGohstBlocks(); blocks.forEach(block => { con.globalAlpha = 0.2; con.fillStyle = block.Color; con.fillRect(block.X, block.Y, block.Width, block.Height); con.strokeStyle = "black"; con.strokeRect(block.X, block.Y, block.Width, block.Height); }); con.globalAlpha = 1.0; } public static GetGohstBlocks(): Block[] { return this.GetHardDropedBlocks(); } public static MoveUp(): void { this.PosY--; } } class TetriminoColor { public static I: string = "aqua"; // I - テトリミノ(水色) public static O = "yellow"; // O - テトリミノ(黄色) public static S = "green"; // S - テトリミノ(緑) public static Z = "red"; // Z - テトリミノ(赤) public static J = "blue"; // J - テトリミノ(青) public static L = "orange"; // L - テトリミノ(オレンジ) public static T = "violet"; // T - テトリミノ(紫) } |
SuperRotation.ts
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 |
class SuperRotationResult { constructor(moveX: number, moveY: number) { this.MoveX = moveX; this.MoveY = moveY; } _moveX: number = 0; get MoveX() { return this._moveX; } set MoveX(value) { this._moveX = value; } _moveY: number = 0; get MoveY() { return this._moveY; } set MoveY(value) { this._moveY = value; } } class SuperRotation { static SuperRotationJuge(blocks: Block[], moveX: number, moveY: number): boolean { let blocks2: Block[] = blocks.map(x => new Block(x.PosX + moveX, x.PosY + moveY)); if (MovingTetrimino.CanRotate(blocks2)) return true; else return false; } public static GetRotateL_TSZLJ(blocks: Block[], angle: TetriminoAngle): SuperRotationResult { let moveX: number = 0; let moveY: number = 0; // (1) if (angle == TetriminoAngle.Angle0 || angle == TetriminoAngle.Angle270) moveX = 1; else moveX = -1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); // (2) if (angle == TetriminoAngle.Angle90 || angle == TetriminoAngle.Angle270) moveY = -1; else moveY = 1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); //(3) moveX = 0; moveY = 0; if (angle == TetriminoAngle.Angle90 || angle == TetriminoAngle.Angle270) moveY = 2; else moveY = -2; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); //(4) if (angle == TetriminoAngle.Angle0 || angle == TetriminoAngle.Angle270) moveX = 1; else moveX = -1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); return null; } public static GetRotateR_TSZLJ(blocks: Block[], angle: TetriminoAngle): SuperRotationResult { // (1) // 0度,90度の場合は左へ1マス移動 // 180度、270度の場合は右へ1マス移動 // (2) // その状態から // 90度,270度の場合は上へ1マス移動 // 0度,180度の場合は下へ1マス移動 // (3) // 元に戻し // 90度,270度の場合は下へ2マス移動 // 0度,180度の場合は上へ2マス移動 // (4) // その状態から // 0度,90度の場合は左へ1マス移動 // 180度、270度の場合は右へ1マス移動 let moveX: number = 0; let moveY: number = 0; // (1) if (angle == TetriminoAngle.Angle0 || angle == TetriminoAngle.Angle90) moveX = -1; else moveX = 1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); // (2) if (angle == TetriminoAngle.Angle90 || angle == TetriminoAngle.Angle270) moveY = -1; else moveY = 1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); //(3) moveX = 0; moveY = 0; if (angle == TetriminoAngle.Angle90 || angle == TetriminoAngle.Angle270) moveY = 2; else moveY = -2; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); //(4) if (angle == TetriminoAngle.Angle0 || angle == TetriminoAngle.Angle90) moveX = -1; else moveX = 1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); return null; } public static GetRotateL_I(blocks: Block[], angle: TetriminoAngle): SuperRotationResult { if (angle == TetriminoAngle.Angle0) { // (1) if (this.SuperRotationJuge(blocks, 2, 0)) return new SuperRotationResult(2, 0); // (2) if (this.SuperRotationJuge(blocks, -1, 0)) return new SuperRotationResult(-1, 0); // (3) if (this.SuperRotationJuge(blocks, 2, -1)) return new SuperRotationResult(2, -1); // (4) if (this.SuperRotationJuge(blocks, -1, 2)) return new SuperRotationResult(-1, 2); return null; } else if (angle == TetriminoAngle.Angle90) { // (1) if (this.SuperRotationJuge(blocks, 1, 0)) return new SuperRotationResult(1, 0); // (2) if (this.SuperRotationJuge(blocks, -2, 0)) return new SuperRotationResult(-2, 0); // (3) if (this.SuperRotationJuge(blocks, 1, 2)) return new SuperRotationResult(1, 2); // (4) if (this.SuperRotationJuge(blocks, -2, -1)) return new SuperRotationResult(-2, -1); return null; } else if (angle == TetriminoAngle.Angle180) { // (1) if (this.SuperRotationJuge(blocks, 1, 0)) return new SuperRotationResult(1, 0); // (2) if (this.SuperRotationJuge(blocks, -2, 0)) return new SuperRotationResult(-2, 0); // (3) if (this.SuperRotationJuge(blocks, -2, 1)) return new SuperRotationResult(-2, 1); // (4) if (this.SuperRotationJuge(blocks, -2, -1)) return new SuperRotationResult(-2, -1); return null; } else //if (angle == TetriminoAngle.Angle270) { // (1) if (this.SuperRotationJuge(blocks, -1, 0)) return new SuperRotationResult(-1, 0); // (2) if (this.SuperRotationJuge(blocks, 2, 0)) return new SuperRotationResult(2, 0); // (3) if (this.SuperRotationJuge(blocks, -1, -2)) return new SuperRotationResult(-1, -2); // (4) if (this.SuperRotationJuge(blocks, 2, 1)) return new SuperRotationResult(2, 1); return null; } } public static GetRotateR_I(blocks: Block[], angle: TetriminoAngle): SuperRotationResult { if (angle == TetriminoAngle.Angle0) { // (1) if (this.SuperRotationJuge(blocks, -2, 0)) return new SuperRotationResult(-2, 0); // (2) if (this.SuperRotationJuge(blocks, 1, 0)) return new SuperRotationResult(1, 0); // (3) if (this.SuperRotationJuge(blocks, 1, 2)) return new SuperRotationResult(1, 2); // (4) if (this.SuperRotationJuge(blocks, -2, -1)) return new SuperRotationResult(-2, -1); return null; } else if (angle == TetriminoAngle.Angle90) { // (1) if (this.SuperRotationJuge(blocks, -2, 0)) return new SuperRotationResult(-2, 0); // (2) if (this.SuperRotationJuge(blocks, 1, 0)) return new SuperRotationResult(1, 0); // (3) if (this.SuperRotationJuge(blocks, -2, 1)) return new SuperRotationResult(-2, 1); // (4) if (this.SuperRotationJuge(blocks, 1, -2)) return new SuperRotationResult(1, -2); return null; } else if (angle == TetriminoAngle.Angle180) { // (1) if (this.SuperRotationJuge(blocks, -1, 0)) return new SuperRotationResult(-1, 0); // (2) if (this.SuperRotationJuge(blocks, 2, 0)) return new SuperRotationResult(2, 0); // (3) if (this.SuperRotationJuge(blocks, -1, -2)) return new SuperRotationResult(-1, -2); // (4) if (this.SuperRotationJuge(blocks, 2, 1)) return new SuperRotationResult(2, 1); return null; } else //if (angle == TetriminoAngle.Angle270) { // (1) if (this.SuperRotationJuge(blocks, 2, 0)) return new SuperRotationResult(2, 0); // (2) if (this.SuperRotationJuge(blocks, -1, 0)) return new SuperRotationResult(-1, 0); // (3) if (this.SuperRotationJuge(blocks, 2, -1)) return new SuperRotationResult(2, -1); // (4) if (this.SuperRotationJuge(blocks, -1, 2)) return new SuperRotationResult(-1, 2); return null; } } } |
FixedTetrimino.ts
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 |
class FixedTetrimino { public static Blocks: Block[] = []; public static Init(): void { this.Blocks = []; } public static ExistsBlock(blocks: Block[]): boolean { for (let i = 0; i < this.Blocks.length; i++) if (blocks.some(x => x.PosX == this.Blocks[i].PosX && x.PosY == this.Blocks[i].PosY)) { return true; } return false; } static DeleteLineCount: number = 0; static DeleteLines() { let lineNums = this.GetDeleteLineNums(); if (lineNums.length == 0) return; for (let i = 0; i < lineNums.length; i++) this.Blocks = this.Blocks.filter(block => block.PosY != lineNums[i]); this.DeleteLineCount = lineNums.length; if (lineNums.length > 0) setTimeout(this.DeleteLine2, 100, this.Blocks, lineNums); } static DeleteLine2(blocks: Block[], lineNums: number[]) { lineNums.forEach(num => { blocks.forEach(block => { if (block.PosY < num) block.PosY++; }); }); PlaySoundeffect.Delete(); } static GetDeleteLineNums(): number[] { let lineNums: number[] = []; for (let row: number = 0; row < Constant.FieldRowMax; row++) { if (this.Blocks.filter(block => block.PosY == row).length == Constant.FieldColumMax) lineNums.push(row); } return lineNums; } public static Draw(con): void { this.Blocks.forEach(block => { block.Draw(con); }); } } |
app.ts
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 |
let can: HTMLCanvasElement = <HTMLCanvasElement>document.getElementById('can'); let con = can.getContext("2d"); let images = null; can.width = 500; can.height = 500; can.style.border = "1px solid #111"; const ScreenW = 500, ScreenH = 500; let isGameOvered: boolean = false; let isAllowHold: boolean = true; let holdTetriminoType: TetriminoTypes = TetriminoTypes.None; let score = 0; let pause = false; let timer = null; Main(); function Main() { images = InitNextImages(); InitNextImages(); FixedTetrimino.Init(); MovingTetrimino.Init(); Update(); document.onkeydown = OnKeyDown; timer = setInterval(MoveDownUpdate, 500); } function GetOutsideBlocks(): Block[] { let outsideBlocks: Block[] = []; for (let row = 0; row < Constant.FieldRowMax + 1; row++) { outsideBlocks.push(new Block(-1, row)); outsideBlocks.push(new Block(Constant.FieldColumMax, row)); } for (let colum = -1; colum < Constant.FieldColumMax + 1; colum++) { outsideBlocks.push(new Block(colum, Constant.FieldRowMax)); } return outsideBlocks; } function DrawOutsideBlocks() { let outsideBlocks = GetOutsideBlocks(); for (let i = 0; i < outsideBlocks.length; i++) { outsideBlocks[i].Draw(con); } } function ClearScreen() { con.fillStyle = "black"; con.fillRect(0, 0, ScreenW, ScreenH); } function GameStart() { isGameOvered = false; isAllowHold = true; holdTetriminoType = TetriminoTypes.None; ResetScore(); // MovingTetrimino.Init()より先に実行しないと // MovingTetrimino.CantPutNewTetriminoイベントが発生する FixedTetrimino.Init(); MovingTetrimino.Init(); } function ResetScore(): void { score = 0; } function InitNextImages(): HTMLImageElement[] { let images: HTMLImageElement[] = []; let img0 = new Image(); img0.src = "./images/i.png"; let img1 = new Image(); img1.src = "./images/o.png"; let img2 = new Image(); img2.src = "./images/s.png"; let img3 = new Image(); img3.src = "./images/z.png"; let img4 = new Image(); img4.src = "./images/j.png"; let img5 = new Image(); img5.src = "./images/l.png"; let img6 = new Image(); img6.src = "./images/t.png"; images.push(img0); images.push(img1); images.push(img2); images.push(img3); images.push(img4); images.push(img5); images.push(img6); return images; } function Update() { ClearScreen(); DrawOutsideBlocks(); MovingTetrimino.Draw(con); MovingTetrimino.DrawGohst(con); FixedTetrimino.Draw(con); let nextTypes = MovingTetrimino.GetNext7(); let next0 = images[nextTypes[0]]; let next1 = images[nextTypes[1]]; let next2 = images[nextTypes[2]]; con.drawImage(next0, 0, 0, 80, 80, 400, 50, 80, 80); con.drawImage(next1, 0, 0, 80, 80, 400, 150, 80, 80); con.drawImage(next2, 0, 0, 80, 80, 400, 250, 80, 80); if (holdTetriminoType != TetriminoTypes.None) { let holdImage = images[holdTetriminoType]; con.drawImage(holdImage, 0, 0, 80, 80, 30, 100, 80, 80); } ShowScore(); if (MovingTetrimino.GameOver) { con.font = "bold 40px 'MS ゴシック'"; con.fillStyle = "white"; con.fillText("GAME OVER", 160, 180); con.font = "bold 20px 'MS ゴシック'"; con.fillText("RESTART S KEY", 180, 220); } if (pause) { con.font = "bold 40px 'MS ゴシック'"; con.fillStyle = "white"; con.fillText("PAUSE", 200, 180); } if (MovingTetrimino.TetriminoFixed) { MovingTetrimino.TetriminoFixed = false; isAllowHold = true; } } function MoveDownUpdate() { if (MovingTetrimino.GameOver) return; if (pause) return; MovingTetrimino.MoveDown(); keyDownCount = 0; Update(); } function ShowScore() { FixedTetrimino.DeleteLineCount; score += GetAddScore(FixedTetrimino.DeleteLineCount); score += MovingTetrimino.DropPoint; FixedTetrimino.DeleteLineCount = 0; MovingTetrimino.DropPoint = 0; con.font = "24px 'MS ゴシック'"; con.fillStyle = "white"; con.fillText("SCORE", 36, 36); con.fillText(score.toLocaleString(), 36, 72); } function GetAddScore(lineCount) { if (lineCount == 0) return 0; if (lineCount == 1) return 40; if (lineCount == 2) return 100; if (lineCount == 3) return 300; if (lineCount == 4) return 1200; } function Restart() { FixedTetrimino.Init(); MovingTetrimino.Init(); score = 0; isAllowHold = true; MovingTetrimino.GameOver = false; Update(); } function Hold() { if (holdTetriminoType == TetriminoTypes.None) { holdTetriminoType = MovingTetrimino.Type; MovingTetrimino.Type = MovingTetrimino.PopNext(); } else { let old: TetriminoTypes = holdTetriminoType; holdTetriminoType = MovingTetrimino.Type; MovingTetrimino.Type = old; } } let keyDownCount = 0; function OnKeyDown(e) { const leftKey = 37; const rightKey = 39; const downKey = 40; const upKey = 38; const zKey = 90; const xKey = 88; const cKey = 67; const sKey = 83; const spaceKey = 32; const escKey = 27; if (e.keyCode == sKey) { Restart(); } if (MovingTetrimino.GameOver == true) return; if (e.keyCode == escKey) { if (pause) pause = false; else pause = true; Update(); } if (pause) return; if (e.keyCode == leftKey) { MovingTetrimino.MoveLeft(); Update(); MakeInterval(); keyDownCount++; } if (e.keyCode == upKey) { MovingTetrimino.RotateR(); Update(); MakeInterval(); keyDownCount++; } if (e.keyCode == rightKey) { MovingTetrimino.MoveRight(); Update(); MakeInterval(); keyDownCount++; } if (e.keyCode == downKey) { MovingTetrimino.MoveDown(); Update(); keyDownCount = 0; } if (e.keyCode == zKey) { MovingTetrimino.RotateL(); Update(); MakeInterval(); keyDownCount++; } if (e.keyCode == xKey) { MovingTetrimino.RotateR(); Update(); MakeInterval(); keyDownCount++; } if (e.keyCode == cKey) { if (isAllowHold) { Hold(); isAllowHold = false; Update(); } } if (e.keyCode == spaceKey) { MovingTetrimino.HardDrop(); Update(); keyDownCount = 0; } } function MakeInterval(){ if (keyDownCount > 15) return; console.log(keyDownCount); clearInterval(timer); timer = setInterval(MoveDownUpdate, 500); } |