Skip to content

Commit 07642e2

Browse files
Merge pull request Nivedita-coder#38 from bensvarnum/main
Tetris game
2 parents 0c8e676 + 4f67d01 commit 07642e2

4 files changed

Lines changed: 558 additions & 0 deletions

File tree

‎Tetris/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## This is your basic tetris game
2+
3+
### Object of the game
4+
5+
- move and rotate falling geometric shapes to form complete rows at the bottom of the game board.
6+
- maximize the number of rows cleared.
7+
-keep the height of the grid as low as possible.
8+
9+
### Buttons-Directional Keys on Keyboard
10+
11+
- Left Arrow key shifts geometric object to the left.
12+
- Right Arrow key shifts geometric object to the right.
13+
- Down Arrow key drops the geometric object faster as it falls.
14+
- Up Arrow key rotates the geometric object clock-wise.
15+
16+
### Enjoy!

‎Tetris/app.js‎

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
const grid = document.querySelector(".grid");
3+
let squares = Array.from(document.querySelectorAll(".grid div"));
4+
const scoreDisplay = document.querySelector("#score");
5+
const startBtn = document.querySelector("#start-button");
6+
const width = 10;
7+
let nextRandom = 0;
8+
let timerId;
9+
let score = 0;
10+
const colors = ["orange", "red", "purple", "green", "blue"];
11+
12+
//The Tetrominoes
13+
const lTetromino = [
14+
[1, width + 1, width * 2 + 1, 2],
15+
[width, width + 1, width + 2, width * 2 + 2],
16+
[1, width + 1, width * 2 + 1, width * 2],
17+
[width, width * 2, width * 2 + 1, width * 2 + 2],
18+
];
19+
20+
const zTetromino = [
21+
[0, width, width + 1, width * 2 + 1],
22+
[width + 1, width + 2, width * 2, width * 2 + 1],
23+
[0, width, width + 1, width * 2 + 1],
24+
[width + 1, width + 2, width * 2, width * 2 + 1],
25+
];
26+
27+
const tTetromino = [
28+
[1, width, width + 1, width + 2],
29+
[1, width + 1, width + 2, width * 2 + 1],
30+
[width, width + 1, width + 2, width * 2 + 1],
31+
[1, width, width + 1, width * 2 + 1],
32+
];
33+
34+
const oTetromino = [
35+
[0, 1, width, width + 1],
36+
[0, 1, width, width + 1],
37+
[0, 1, width, width + 1],
38+
[0, 1, width, width + 1],
39+
];
40+
41+
const iTetromino = [
42+
[1, width + 1, width * 2 + 1, width * 3 + 1],
43+
[width, width + 1, width + 2, width + 3],
44+
[1, width + 1, width * 2 + 1, width * 3 + 1],
45+
[width, width + 1, width + 2, width + 3],
46+
];
47+
48+
const theTetrominoes = [
49+
lTetromino,
50+
zTetromino,
51+
tTetromino,
52+
oTetromino,
53+
iTetromino,
54+
];
55+
56+
let currentPosition = 4;
57+
let currentRotation = 0;
58+
59+
console.log(theTetrominoes[0][0]);
60+
61+
//randomly select a Tetromino and its first rotation
62+
let random = Math.floor(Math.random() * theTetrominoes.length);
63+
let current = theTetrominoes[random][currentRotation];
64+
65+
//draw the Tetromino
66+
function draw() {
67+
current.forEach((index) => {
68+
squares[currentPosition + index].classList.add("tetromino");
69+
squares[currentPosition + index].style.backgroundColor = colors[random];
70+
});
71+
}
72+
73+
//undraw the Tetromino
74+
function undraw() {
75+
current.forEach((index) => {
76+
squares[currentPosition + index].classList.remove("tetromino");
77+
squares[currentPosition + index].style.backgroundColor = "";
78+
});
79+
}
80+
81+
//assign functions to keyCodes
82+
function control(e) {
83+
if (e.keyCode === 37) {
84+
moveLeft();
85+
} else if (e.keyCode === 38) {
86+
rotate();
87+
} else if (e.keyCode === 39) {
88+
moveRight();
89+
} else if (e.keyCode === 40) {
90+
moveDown();
91+
}
92+
}
93+
document.addEventListener("keyup", control);
94+
95+
//move down function
96+
function moveDown() {
97+
undraw();
98+
currentPosition += width;
99+
draw();
100+
freeze();
101+
}
102+
103+
//freeze function
104+
function freeze() {
105+
if (
106+
current.some((index) =>
107+
squares[currentPosition + index + width].classList.contains("taken")
108+
)
109+
) {
110+
current.forEach((index) =>
111+
squares[currentPosition + index].classList.add("taken")
112+
);
113+
//start a new tetromino falling
114+
random = nextRandom;
115+
nextRandom = Math.floor(Math.random() * theTetrominoes.length);
116+
current = theTetrominoes[random][currentRotation];
117+
currentPosition = 4;
118+
draw();
119+
displayShape();
120+
addScore();
121+
gameOver();
122+
}
123+
}
124+
125+
//move the tetromino left, unless is at the edge or there is a blockage
126+
function moveLeft() {
127+
undraw();
128+
const isAtLeftEdge = current.some(
129+
(index) => (currentPosition + index) % width === 0
130+
);
131+
if (!isAtLeftEdge) currentPosition -= 1;
132+
if (
133+
current.some((index) =>
134+
squares[currentPosition + index].classList.contains("taken")
135+
)
136+
) {
137+
currentPosition += 1;
138+
}
139+
draw();
140+
}
141+
142+
//move the tetromino right, unless is at the edge or there is a blockage
143+
function moveRight() {
144+
undraw();
145+
const isAtRightEdge = current.some(
146+
(index) => (currentPosition + index) % width === width - 1
147+
);
148+
if (!isAtRightEdge) currentPosition += 1;
149+
if (
150+
current.some((index) =>
151+
squares[currentPosition + index].classList.contains("taken")
152+
)
153+
) {
154+
currentPosition -= 1;
155+
}
156+
draw();
157+
}
158+
159+
//rotate the tetromino
160+
function rotate() {
161+
undraw();
162+
currentRotation++;
163+
if (currentRotation === current.length) {
164+
//if the current roation gets to 4, make it go back to 0
165+
currentRotation = 0;
166+
}
167+
current = theTetrominoes[random][currentRotation];
168+
draw();
169+
}
170+
171+
//show up-next tetromino in mini-grid display
172+
const displaySquares = document.querySelectorAll(".mini-grid div");
173+
const displayWidth = 4;
174+
const displayIndex = 0;
175+
176+
//the Tetrominos without rotations
177+
const upNextTetrominoes = [
178+
[1, displayWidth + 1, displayWidth * 2 + 1, 2], //lTetromino
179+
[0, displayWidth, displayWidth + 1, displayWidth * 2 + 1], //zTetromino
180+
[1, displayWidth, displayWidth + 1, displayWidth + 2], //tTetromino
181+
[0, 1, displayWidth, displayWidth + 1], //oTetromino
182+
[1, displayWidth + 1, displayWidth * 2 + 1, displayWidth * 3 + 1], //iTetromino
183+
];
184+
185+
//display the shape in the mini-grid display
186+
function displayShape() {
187+
//remove any trace of a tetromino form the entire grid
188+
displaySquares.forEach((square) => {
189+
square.classList.remove("tetromino");
190+
square.style.backgroundColor = "";
191+
});
192+
upNextTetrominoes[nextRandom].forEach((index) => {
193+
displaySquares[displayIndex + index].classList.add("tetromino");
194+
displaySquares[displayIndex + index].style.backgroundColor =
195+
colors[nextRandom];
196+
});
197+
}
198+
199+
//add functionality to the button
200+
startBtn.addEventListener("click", () => {
201+
if (timerId) {
202+
clearInterval(timerId);
203+
timerId = null;
204+
} else {
205+
draw();
206+
timerId = setInterval(moveDown, 1000);
207+
nextRandom = Math.floor(Math.random() * theTetrominoes.length);
208+
displayShape();
209+
}
210+
});
211+
212+
//add score
213+
function addScore() {
214+
for (let i = 0; i < 199; i += width) {
215+
const row = [
216+
i,
217+
i + 1,
218+
i + 2,
219+
i + 3,
220+
i + 4,
221+
i + 5,
222+
i + 6,
223+
i + 7,
224+
i + 8,
225+
i + 9,
226+
];
227+
228+
if (row.every((index) => squares[index].classList.contains("taken"))) {
229+
score += 10;
230+
scoreDisplay.innerHTML = score;
231+
row.forEach((index) => {
232+
squares[index].classList.remove("taken");
233+
squares[index].classList.remove("tetromino");
234+
squares[index].style.backgroundColor = "";
235+
});
236+
const squaresRemoved = squares.splice(i, width);
237+
squares = squaresRemoved.concat(squares);
238+
squares.forEach((cell) => grid.appendChild(cell));
239+
}
240+
}
241+
}
242+
243+
//game over
244+
function gameOver() {
245+
if (
246+
current.some((index) =>
247+
squares[currentPosition + index].classList.contains("taken")
248+
)
249+
) {
250+
scoreDisplay.innerHTML = "end";
251+
clearInterval(timerId);
252+
}
253+
}
254+
});

0 commit comments

Comments
 (0)