Fix timer & improve graphics
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
<div id="lang-de" class="lang" data-lang="de">Deutsch</div>
|
<div id="lang-de" class="lang" data-lang="de">Deutsch</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="background" class="blurred">
|
<div id="background" class="blurred">
|
||||||
<div id="time">0:00</div>
|
<div id="time">00:00</div>
|
||||||
<div id="score" data-prefix="Score: ">0</div>
|
<div id="score" data-prefix="Score: ">0</div>
|
||||||
<canvas id="tetris" width="240" height="400"></canvas>
|
<canvas id="tetris" width="240" height="400"></canvas>
|
||||||
<div id="controls"></div>
|
<div id="controls"></div>
|
||||||
|
1
menu.js
1
menu.js
@@ -127,6 +127,7 @@ function hideMenu() {
|
|||||||
document.getElementById("game-title").style.opacity = "0";
|
document.getElementById("game-title").style.opacity = "0";
|
||||||
document.getElementById("game-play").style.opacity = "0";
|
document.getElementById("game-play").style.opacity = "0";
|
||||||
document.getElementById("game-reset").style.opacity = "0";
|
document.getElementById("game-reset").style.opacity = "0";
|
||||||
|
lastTimeUpdate = Date.now();
|
||||||
fadeBlurOut();
|
fadeBlurOut();
|
||||||
if(!firstRun) {
|
if(!firstRun) {
|
||||||
update(lastTime);
|
update(lastTime);
|
||||||
|
@@ -148,3 +148,8 @@ body {
|
|||||||
#corner-buttons > a:hover, #corner-buttons > a:active {
|
#corner-buttons > a:hover, #corner-buttons > a:active {
|
||||||
color: rgba(255, 255, 255, 1);
|
color: rgba(255, 255, 255, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#time {
|
||||||
|
margin-top: 5px;
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
50
tetris.js
50
tetris.js
@@ -2,14 +2,12 @@ const canvas = document.getElementById('tetris');
|
|||||||
const context = canvas.getContext('2d');
|
const context = canvas.getContext('2d');
|
||||||
|
|
||||||
const fieldSize = {x: 12, y: 20};
|
const fieldSize = {x: 12, y: 20};
|
||||||
|
const tileGap = .05;
|
||||||
|
|
||||||
let isPaused = true;
|
let isPaused = true;
|
||||||
|
|
||||||
let startTime = 0;
|
let startTime = 0;
|
||||||
|
let prevUpdateScore = 0;
|
||||||
function clearScreen() {
|
|
||||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
function arenaSweep() {
|
function arenaSweep() {
|
||||||
let rowCount = 1;
|
let rowCount = 1;
|
||||||
@@ -27,8 +25,15 @@ function arenaSweep() {
|
|||||||
player.score += rowCount * 10;
|
player.score += rowCount * 10;
|
||||||
rowCount *= 2;
|
rowCount *= 2;
|
||||||
}
|
}
|
||||||
dropInterval -= player.score / 10;
|
if (player.score - prevUpdateScore > 50) {
|
||||||
dropInterval = dropInterval > 50 ? dropInterval : 50;
|
dropInterval -= -20;
|
||||||
|
dropInterval = dropInterval > 100 ? dropInterval : 100;
|
||||||
|
prevUpdateScore = player.score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearScreen() {
|
||||||
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
function collide(arena, player) {
|
function collide(arena, player) {
|
||||||
@@ -111,12 +116,22 @@ function drawMatrix(matrix, offset) {
|
|||||||
row.forEach((value, x) => {
|
row.forEach((value, x) => {
|
||||||
if (value !== 0) {
|
if (value !== 0) {
|
||||||
context.fillStyle = colors[value];
|
context.fillStyle = colors[value];
|
||||||
context.fillRect(x + offset.x, y + offset.y, 1, 1);
|
context.fillRect(x + offset.x + tileGap / 2, y + offset.y + tileGap / 2, 1 - tileGap, 1 - tileGap);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function gameOver() {
|
||||||
|
arena.forEach(row => row.fill(0));
|
||||||
|
passedTime = 0;
|
||||||
|
lastTimeUpdate = Date.now();
|
||||||
|
updateTime();
|
||||||
|
player.score = 0;
|
||||||
|
dropInterval = 1000;
|
||||||
|
updateScore();
|
||||||
|
}
|
||||||
|
|
||||||
function merge(arena, player) {
|
function merge(arena, player) {
|
||||||
player.matrix.forEach((row, y) => {
|
player.matrix.forEach((row, y) => {
|
||||||
row.forEach((value, x) => {
|
row.forEach((value, x) => {
|
||||||
@@ -144,6 +159,7 @@ function playerMove(dir) {
|
|||||||
if (collide(arena, player)) {
|
if (collide(arena, player)) {
|
||||||
player.pos.x -= dir;
|
player.pos.x -= dir;
|
||||||
}
|
}
|
||||||
|
dropCounter *= .75;
|
||||||
}
|
}
|
||||||
|
|
||||||
function playerReset() {
|
function playerReset() {
|
||||||
@@ -151,11 +167,9 @@ function playerReset() {
|
|||||||
player.matrix = createPiece(pieces[pieces.length * Math.random() | 0]);
|
player.matrix = createPiece(pieces[pieces.length * Math.random() | 0]);
|
||||||
player.pos.y = 0;
|
player.pos.y = 0;
|
||||||
player.pos.x = (arena[0].length / 2 | 0) - (player.matrix[0].length / 2 | 0);
|
player.pos.x = (arena[0].length / 2 | 0) - (player.matrix[0].length / 2 | 0);
|
||||||
|
|
||||||
if (collide(arena, player)) {
|
if (collide(arena, player)) {
|
||||||
arena.forEach(row => row.fill(0));
|
gameOver();
|
||||||
player.score = 0;
|
|
||||||
dropInterval = 1000;
|
|
||||||
updateScore();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,8 +214,6 @@ let dropInterval = 1000;
|
|||||||
|
|
||||||
let lastTime = 0;
|
let lastTime = 0;
|
||||||
|
|
||||||
const timeEl = document.getElementById("time");
|
|
||||||
|
|
||||||
function update(time = 0) {
|
function update(time = 0) {
|
||||||
if(!isPaused) {
|
if(!isPaused) {
|
||||||
const deltaTime = time - lastTime;
|
const deltaTime = time - lastTime;
|
||||||
@@ -212,7 +224,7 @@ function update(time = 0) {
|
|||||||
playerDrop();
|
playerDrop();
|
||||||
}
|
}
|
||||||
|
|
||||||
timeEl.innerHTML = formatMillis(Date.now() - startTime);
|
updateTime();
|
||||||
|
|
||||||
draw();
|
draw();
|
||||||
requestAnimationFrame(update);
|
requestAnimationFrame(update);
|
||||||
@@ -233,6 +245,16 @@ function updateScore() {
|
|||||||
document.getElementById('score').innerText = player.score.toString();
|
document.getElementById('score').innerText = player.score.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const timeEl = document.getElementById("time");
|
||||||
|
let passedTime = 0;
|
||||||
|
let lastTimeUpdate = Date.now();
|
||||||
|
|
||||||
|
function updateTime() {
|
||||||
|
passedTime += Date.now() - lastTimeUpdate;
|
||||||
|
timeEl.innerHTML = formatMillis(passedTime);
|
||||||
|
lastTimeUpdate = Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
const colors = [
|
const colors = [
|
||||||
null,
|
null,
|
||||||
'#FF0D72',
|
'#FF0D72',
|
||||||
|
Reference in New Issue
Block a user