changed zoom to difficulty; removed zoom use

This commit is contained in:
2024-08-07 16:42:31 +02:00
parent 3163a65b1a
commit d3ae0abac4
3 changed files with 64 additions and 34 deletions

View File

@@ -5,7 +5,7 @@ class Tetris_CTX2D {
static get DEBUG_PARENT_OBJECT() { return true; };
static get AUTO_CONTINUE_ON_FOCUS() { return false; };
static get SHOW_FPS_INTERVAL() { return 1000 / 4; }; // four times per second
static get TICK_TIME() { return 1 / 4; };
// static get TICK_TIME() { return 1 / 4; };
static get STATE_RUNNING() { return 0; };
static get STATE_ENDED() { return 1; };
@@ -58,11 +58,12 @@ class Tetris_CTX2D {
running = false;
animationRequestId = 0;
constructor(divId, width, height, zoom = 1, showFps = false) {
this.createCanvas(divId, width, height, zoom);
constructor(divId, width, height, difficulty = 1, showFps = false) {
this.createCanvas(divId, width, height);
this.canvasEl.title = "Playing: Tetris";
this.ctx = this.canvasEl.getContext("2d");
this.ctx.textAlign = "center";
this.difficulty = difficulty;
this.BLOCK_WIDTH = this.width / Tetris_CTX2D.GRID_COLUMS;
this.BLOCK_HEIGHT = this.height / Tetris_CTX2D.GRID_ROWS;
@@ -103,7 +104,7 @@ class Tetris_CTX2D {
if (Tetris_CTX2D.DEBUG_PARENT_OBJECT) window.game = this;
if (typeof terminalAddCommand === "function") terminalAddCommand("restartgame", (t) => this.terminalRestartGame(t));
if (typeof terminalPrintLn === "function") terminalPrintLn("new Tetris_CTX2D: @", divId, ' ', width, 'x', height, ':', zoom, ' showFps=', showFps);
if (typeof terminalPrintLn === "function") terminalPrintLn("new Tetris_CTX2D: @", divId, ' ', width, 'x', height, ' difficulty=', difficulty, ' showFps=', showFps);
this.drawCanvas();
}
@@ -288,6 +289,12 @@ class Tetris_CTX2D {
newGame() {
this.gameState = Tetris_CTX2D.STATE_RUNNING;
this.tickTime = 0;
this.stepTime = 1/4;
if(this.difficulty == 0){
this.stepTime = 1/2;
}else{
this.stepTime = this.stepTime/ this.difficulty;
}
this.initBoard();
this.score = 0;
this.nextShape = this.newShape();
@@ -339,8 +346,8 @@ class Tetris_CTX2D {
//#state switch
if (this.gameState == Tetris_CTX2D.STATE_RUNNING) {
this.tickTime += timeDelta;
if (this.tickTime >= Tetris_CTX2D.TICK_TIME) {
this.tickTime -= Tetris_CTX2D.TICK_TIME;
if (this.tickTime >= this.stepTime) {
this.tickTime -= this.stepTime;
this.tick();
}
}
@@ -416,7 +423,7 @@ class Tetris_CTX2D {
}
// tickTime
ctx.strokeStyle = '#8080B0';
const rotation = this.tickTime / Tetris_CTX2D.TICK_TIME * Math.PI / 2;
const rotation = this.tickTime / this.stepTime * Math.PI / 2;
for (let i = 0; i < 4; i++) {
const r = rotation + i * Math.PI / 2;
ctx.beginPath();
@@ -498,9 +505,8 @@ class Tetris_CTX2D {
e.preventDefault();
return false;
} else if (e.key == Tetris_CTX2D.KEY_ARROW_DOWN || e.key == Tetris_CTX2D.KEY_S) {
if (this.isValidMove(0, 1)) {
this.currentY++;
}
this.tick();
this.tickTime = 0;
e.preventDefault();
return false;
} else if (e.key == Tetris_CTX2D.KEY_ARROW_LEFT || e.key == Tetris_CTX2D.KEY_A) {
@@ -555,6 +561,6 @@ class Tetris_CTX2D {
}
}
function startTetris(divId, width = 300, height = 600, zoom = 1, showFps = true) {
return new Tetris_CTX2D(divId, width, height, zoom, showFps);
function startTetris(divId, width = 300, height = 600, difficulty = 1, showFps = true) {
return new Tetris_CTX2D(divId, width, height, difficulty, showFps);
}