操作方法は移動は←→↓、ハードドロップはスペースキー、左回転Z、右回転X、一時停止はEscです。ただしキーをおすとスクロールしてしまうので動作確認はこちらからお願いします。
前回はC#で作成したテトリスをTypeScriptで書き直しましたが、それをコンパイルした結果、以下のようなJavaScriptになります。
まずはHTMLファイルを示します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>鳩でもわかるテトリス</title> </head> <body> <canvas id="can"></cavas> <script type="text/javascript" src="./PlaySoundeffect.js" charset="utf-8"></script> <script type="text/javascript" src="./Tetrimino.js" charset="utf-8"></script> <script type="text/javascript" src="./Block.js" charset="utf-8"></script> <script type="text/javascript" src="./MovingTetrimino.js" charset="utf-8"></script> <script type="text/javascript" src="./SuperRotation.js" charset="utf-8"></script> <script type="text/javascript" src="./FixedTetrimino.js" charset="utf-8"></script> <script type="text/javascript" src="./app.js" charset="utf-8"></script> </body> </html> |
PlaySoundeffect.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class PlaySoundeffect { static Rotate() { let audioElm = new Audio('./mp3/rotate.mp3'); audioElm.play(); } static Drop() { let audioElm = new Audio('./mp3/drop.mp3'); audioElm.play(); } static Delete() { let audioElm = new Audio('./mp3/delete.mp3'); audioElm.play(); } } |
Tetrimino.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 |
class Constant { } Constant.FieldColumMax = 10; Constant.FieldRowMax = 20; Constant.BlockSize = 20; Constant.LeftTopBlockPositionX = 150; Constant.LeftTopBlockPositionY = 70; var TetriminoTypes; (function (TetriminoTypes) { // I - テトリミノ(水色) // O - テトリミノ(黄色) // S - テトリミノ(緑) // Z - テトリミノ(赤) // J - テトリミノ(青) // L - テトリミノ(オレンジ) // T - テトリミノ(紫) TetriminoTypes[TetriminoTypes["I"] = 0] = "I"; TetriminoTypes[TetriminoTypes["O"] = 1] = "O"; TetriminoTypes[TetriminoTypes["S"] = 2] = "S"; TetriminoTypes[TetriminoTypes["Z"] = 3] = "Z"; TetriminoTypes[TetriminoTypes["J"] = 4] = "J"; TetriminoTypes[TetriminoTypes["L"] = 5] = "L"; TetriminoTypes[TetriminoTypes["T"] = 6] = "T"; TetriminoTypes[TetriminoTypes["None"] = 7] = "None"; })(TetriminoTypes || (TetriminoTypes = {})); var TetriminoAngle; (function (TetriminoAngle) { TetriminoAngle[TetriminoAngle["Angle0"] = 0] = "Angle0"; TetriminoAngle[TetriminoAngle["Angle90"] = 1] = "Angle90"; TetriminoAngle[TetriminoAngle["Angle180"] = 2] = "Angle180"; TetriminoAngle[TetriminoAngle["Angle270"] = 3] = "Angle270"; })(TetriminoAngle || (TetriminoAngle = {})); |
Block.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 |
class Block { constructor(posX, posY) { this.Color = "gray"; this.PosX = posX; this.PosY = posY; } set PosX(value) { this._posX = value; } get PosX() { return this._posX; } set PosY(value) { this._posY = value; } get PosY() { return this._posY; } set Color(value) { this._color = value; } get Color() { return this._color; } get X() { return this.Width * this.PosX + Constant.LeftTopBlockPositionX; } get Y() { return this.Height * this.PosY + Constant.LeftTopBlockPositionY; } get Width() { return Constant.BlockSize; } get Height() { return Constant.BlockSize; } Draw(con) { 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.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 |
class MovingTetrimino { static set PosX(value) { this._posX = value; } static get PosX() { return this._posX; } static set PosY(value) { this._posY = value; } static get PosY() { return this._posY; } static set Angle(value) { this._angle = value; } static get Angle() { return this._angle; } static GetBlocks(angle) { let blocks = []; let color = ""; if (this.Type == TetriminoTypes.I) { let blocks0 = []; 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 = []; 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 = []; 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 = []; 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 = []; 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 = []; 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 = []; blocks.forEach(block0 => { let block = new Block(block0.PosX + this.PosX, block0.PosY + this.PosY); block.Color = color; ret.push(block); }); return ret; } static GetRotateBlocks33(blocks, angle) { 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, angle) { 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)); } static MoveLeft() { let blocks = this.GetMoveLeft(); if (Math.min.apply(null, blocks.map(function (o) { return o.PosX; })) >= 0 && !FixedTetrimino.ExistsBlock(blocks)) this.PosX--; } static MoveRight() { let blocks = this.GetMoveRight(); if (Math.max.apply(null, blocks.map(function (o) { return o.PosX; })) < Constant.FieldColumMax && !FixedTetrimino.ExistsBlock(blocks)) this.PosX++; } static MoveDown() { let blocks = 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() { let ret = this.GetBlocks(this.Angle); return ret.map(x => new Block(x.PosX - 1, x.PosY)); } static GetMoveRight() { let ret = this.GetBlocks(this.Angle); return ret.map(x => new Block(x.PosX + 1, x.PosY)); } static GetMoveDown() { let ret = this.GetBlocks(this.Angle); return ret.map(x => new Block(x.PosX, x.PosY + 1)); } static RotateL() { let result = 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(); } } static RotateR() { let result = 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(); } } static CanRotate(blocks) { 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() { 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() { 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() { let angle = this.GetAngleRotateL(); let blocks = this.GetBlocks(angle); if (this.CanRotate(blocks)) return new SuperRotationResult(0, 0); return SuperRotation.GetRotateL_TSZLJ(blocks, angle); } static GetSuperRotationResultRightTSZLJ() { let angle = this.GetAngleRotateR(); let blocks = this.GetBlocks(angle); if (this.CanRotate(blocks)) return new SuperRotationResult(0, 0); return SuperRotation.GetRotateR_TSZLJ(blocks, angle); } static GetSuperRotationResultRightI() { let angle = this.GetAngleRotateR(); let blocks = this.GetBlocks(angle); if (this.CanRotate(blocks)) return new SuperRotationResult(0, 0); return SuperRotation.GetRotateR_I(blocks, angle); } static GetSuperRotationResultLeftI() { let angle = this.GetAngleRotateL(); let blocks = this.GetBlocks(angle); if (this.CanRotate(blocks)) return new SuperRotationResult(0, 0); return SuperRotation.GetRotateL_I(blocks, angle); } static HardDrop() { let count = 0; count = this.GetHardDropCount(); this.PosY += count; this.DropPoint = count; this.FixTetrimino(); } static GetHardDropedBlocks() { let i = this.GetHardDropCount(); let blocks = this.GetBlocks(this.Angle).map(x => { let block = new Block(x.PosX, x.PosY + i); block.Color = x.Color; return block; }); return blocks; } static GetHardDropCount() { let blocks = this.GetMoveDown(); let i = 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 FixTetrimino() { let blocks = this.GetBlocks(this.Angle); blocks.forEach(block => { FixedTetrimino.Blocks.push(block); }); PlaySoundeffect.Drop(); FixedTetrimino.DeleteLines(); this.Type = this.PopNext(); this.TetriminoFixed = true; } 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() { return this._type; } static Init() { this.NextTetoroTypes = []; this.GetNext7(); this.Type = this.PopNext(); } static CreateBag() { let r; let types = []; 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); } static GetNext() { if (this.NextTetoroTypes.length == 0) this.CreateBag(); return this.NextTetoroTypes[0]; } static GetNext7() { if (this.NextTetoroTypes.length < 7) this.CreateBag(); return this.NextTetoroTypes.slice(0, 7); } static PopNext() { let ret = this.GetNext(); this.NextTetoroTypes.splice(0, 1); return ret; } static Draw(con) { if (this.Type == TetriminoTypes.None) return; let blocks = this.GetBlocks(this.Angle); blocks.forEach(block => { block.Draw(con); }); } static DrawGohst(con) { if (this.Type == TetriminoTypes.None) return; let blocks = 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; } static GetGohstBlocks() { return this.GetHardDropedBlocks(); } static MoveUp() { this.PosY--; } } MovingTetrimino.DropPoint = 0; MovingTetrimino.TetriminoFixed = false; MovingTetrimino._type = TetriminoTypes.None; MovingTetrimino.GameOver = false; MovingTetrimino.NextTetoroTypes = []; class TetriminoColor { } TetriminoColor.I = "aqua"; // I - テトリミノ(水色) TetriminoColor.O = "yellow"; // O - テトリミノ(黄色) TetriminoColor.S = "green"; // S - テトリミノ(緑) TetriminoColor.Z = "red"; // Z - テトリミノ(赤) TetriminoColor.J = "blue"; // J - テトリミノ(青) TetriminoColor.L = "orange"; // L - テトリミノ(オレンジ) TetriminoColor.T = "violet"; // T - テトリミノ(紫) |
SuperRotation.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 |
class SuperRotationResult { constructor(moveX, moveY) { this._moveX = 0; this._moveY = 0; this.MoveX = moveX; this.MoveY = moveY; } get MoveX() { return this._moveX; } set MoveX(value) { this._moveX = value; } get MoveY() { return this._moveY; } set MoveY(value) { this._moveY = value; } } class SuperRotation { static SuperRotationJuge(blocks, moveX, moveY) { let blocks2 = blocks.map(x => new Block(x.PosX + moveX, x.PosY + moveY)); if (MovingTetrimino.CanRotate(blocks2)) return true; else return false; } static GetRotateL_TSZLJ(blocks, angle) { let moveX = 0; let moveY = 0; if (angle == TetriminoAngle.Angle0 || angle == TetriminoAngle.Angle270) moveX = 1; else moveX = -1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); if (angle == TetriminoAngle.Angle90 || angle == TetriminoAngle.Angle270) moveY = -1; else moveY = 1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); 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); 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; } static GetRotateR_TSZLJ(blocks, angle) { let moveX = 0; let moveY = 0; if (angle == TetriminoAngle.Angle0 || angle == TetriminoAngle.Angle90) moveX = -1; else moveX = 1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); if (angle == TetriminoAngle.Angle90 || angle == TetriminoAngle.Angle270) moveY = -1; else moveY = 1; if (this.SuperRotationJuge(blocks, moveX, moveY)) return new SuperRotationResult(moveX, moveY); 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); 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; } static GetRotateL_I(blocks, angle) { if (angle == TetriminoAngle.Angle0) { if (this.SuperRotationJuge(blocks, 2, 0)) return new SuperRotationResult(2, 0); if (this.SuperRotationJuge(blocks, -1, 0)) return new SuperRotationResult(-1, 0); if (this.SuperRotationJuge(blocks, 2, -1)) return new SuperRotationResult(2, -1); if (this.SuperRotationJuge(blocks, -1, 2)) return new SuperRotationResult(-1, 2); return null; } else if (angle == TetriminoAngle.Angle90) { if (this.SuperRotationJuge(blocks, 1, 0)) return new SuperRotationResult(1, 0); if (this.SuperRotationJuge(blocks, -2, 0)) return new SuperRotationResult(-2, 0); if (this.SuperRotationJuge(blocks, 1, 2)) return new SuperRotationResult(1, 2); if (this.SuperRotationJuge(blocks, -2, -1)) return new SuperRotationResult(-2, -1); return null; } else if (angle == TetriminoAngle.Angle180) { if (this.SuperRotationJuge(blocks, 1, 0)) return new SuperRotationResult(1, 0); if (this.SuperRotationJuge(blocks, -2, 0)) return new SuperRotationResult(-2, 0); if (this.SuperRotationJuge(blocks, -2, 1)) return new SuperRotationResult(-2, 1); if (this.SuperRotationJuge(blocks, -2, -1)) return new SuperRotationResult(-2, -1); return null; } else //if (angle == TetriminoAngle.Angle270) { if (this.SuperRotationJuge(blocks, -1, 0)) return new SuperRotationResult(-1, 0); if (this.SuperRotationJuge(blocks, 2, 0)) return new SuperRotationResult(2, 0); if (this.SuperRotationJuge(blocks, -1, -2)) return new SuperRotationResult(-1, -2); if (this.SuperRotationJuge(blocks, 2, 1)) return new SuperRotationResult(2, 1); return null; } } static GetRotateR_I(blocks, angle) { if (angle == TetriminoAngle.Angle0) { if (this.SuperRotationJuge(blocks, -2, 0)) return new SuperRotationResult(-2, 0); if (this.SuperRotationJuge(blocks, 1, 0)) return new SuperRotationResult(1, 0); if (this.SuperRotationJuge(blocks, 1, 2)) return new SuperRotationResult(1, 2); if (this.SuperRotationJuge(blocks, -2, -1)) return new SuperRotationResult(-2, -1); return null; } else if (angle == TetriminoAngle.Angle90) { if (this.SuperRotationJuge(blocks, -2, 0)) return new SuperRotationResult(-2, 0); if (this.SuperRotationJuge(blocks, 1, 0)) return new SuperRotationResult(1, 0); if (this.SuperRotationJuge(blocks, -2, 1)) return new SuperRotationResult(-2, 1); if (this.SuperRotationJuge(blocks, 1, -2)) return new SuperRotationResult(1, -2); return null; } else if (angle == TetriminoAngle.Angle180) { if (this.SuperRotationJuge(blocks, -1, 0)) return new SuperRotationResult(-1, 0); if (this.SuperRotationJuge(blocks, 2, 0)) return new SuperRotationResult(2, 0); if (this.SuperRotationJuge(blocks, -1, -2)) return new SuperRotationResult(-1, -2); if (this.SuperRotationJuge(blocks, 2, 1)) return new SuperRotationResult(2, 1); return null; } else //if (angle == TetriminoAngle.Angle270) { if (this.SuperRotationJuge(blocks, 2, 0)) return new SuperRotationResult(2, 0); if (this.SuperRotationJuge(blocks, -1, 0)) return new SuperRotationResult(-1, 0); if (this.SuperRotationJuge(blocks, 2, -1)) return new SuperRotationResult(2, -1); if (this.SuperRotationJuge(blocks, -1, 2)) return new SuperRotationResult(-1, 2); return null; } } } |
FixedTetrimino.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 |
class FixedTetrimino { static Init() { this.Blocks = []; } static ExistsBlock(blocks) { 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 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, lineNums) { lineNums.forEach(num => { blocks.forEach(block => { if (block.PosY < num) block.PosY++; }); }); PlaySoundeffect.Delete(); } static GetDeleteLineNums() { let lineNums = []; for (let row = 0; row < Constant.FieldRowMax; row++) { if (this.Blocks.filter(block => block.PosY == row).length == Constant.FieldColumMax) lineNums.push(row); } return lineNums; } static Draw(con) { this.Blocks.forEach(block => { block.Draw(con); }); } } FixedTetrimino.Blocks = []; FixedTetrimino.DeleteLineCount = 0; |
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 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 |
let can = 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 = false; let isAllowHold = true; let holdTetriminoType = 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() { let outsideBlocks = []; 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(); FixedTetrimino.Init(); MovingTetrimino.Init(); } function ResetScore() { score = 0; } function InitNextImages() { let images = []; 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 = 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); } |