-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tetris.js
146 lines (133 loc) · 3.95 KB
/
tetris.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
// Define variables for the game
let board;
let cols = 10;
let rows = 20;
let blockSize = 30;
let currentPiece;
let gameSpeed = 500; // Speed of the game
let lastMove = 0;
function setup() {
createCanvas(cols * blockSize, rows * blockSize);
board = createBoard(rows, cols); // Initialize the game board
currentPiece = new Piece(); // Create the first piece
}
function draw() {
background(255);
let currentTime = millis();
if (currentTime - lastMove > gameSpeed) {
if (!currentPiece.moveDown(board)) {
handlePieceLanding(board, currentPiece);
currentPiece = new Piece();
}
lastMove = currentTime;
}
drawBoard(board);
currentPiece.show();
}
// Create the game board
function createBoard(rows, cols) {
let arr = [];
for (let i = 0; i < rows; i++) {
arr.push(new Array(cols).fill(0));
}
return arr;
}
// Draw the game board
function drawBoard(board) {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
fill(board[i][j] === 0 ? 255 : board[i][j]);
stroke(0);
rect(j * blockSize, i * blockSize, blockSize, blockSize);
}
}
}
// Define the Piece class
class Piece {
constructor() {
// Define the shape and color of the piece
this.shape = [[1, 1], [1, 1]]; // A simple 2x2 block for example
this.color = [255, 0, 0]; // Red color
this.x = 0; // X position
this.y = 0; // Y position
}
show() {
// Display the piece on the board
fill(this.color);
for (let i = 0; i < this.shape.length; i++) {
for (let j = 0; j < this.shape[i].length; j++) {
if (this.shape[i][j] === 1) {
rect((this.x + j) * blockSize, (this.y + i) * blockSize, blockSize, blockSize);
}
}
}
}
moveDown(board) {
// Move the piece down
this.y++;
// Check for collision with the board
for (let i = 0; i < this.shape.length; i++) {
for (let j = 0; j < this.shape[i].length; j++) {
if (this.shape[i][j] === 1 && board[this.y + i][this.x + j] !== 0) {
this.y--;
return false;
}
}
}
return true;
}
// Additional functions like rotate, moveLeft, moveRight, etc.
rotate() {
// Rotate the piece 90 degrees
this.shape = this.shape[0].map((val, index) => this.shape.map(row => row[index])).reverse();
}
moveLeft(board) {
// Move the piece to the left
this.x--;
// Check for collision with the board
for (let i = 0; i < this.shape.length; i++) {
for (let j = 0; j < this.shape[i].length; j++) {
if (this.shape[i][j] === 1 && board[this.y + i][this.x + j] !== 0) {
this.x++;
return false;
}
}
}
return true;
}
moveRight(board) {
// Move the piece to the right
this.x++;
// Check for collision with the board
for (let i = 0; i < this.shape.length; i++) {
for (let j = 0; j < this.shape[i].length; j++) {
if (this.shape[i][j] === 1 && board[this.y + i][this.x + j] !== 0) {
this.x--;
return false;
}
}
}
return true;
}
}
function handlePieceLanding(board, piece) {
// Handle what happens when a piece lands
for (let i = 0; i < piece.shape.length; i++) {
for (let j = 0; j < piece.shape[i].length; j++) {
if (piece.shape[i][j] === 1) {
board[piece.y + i][piece.x + j] = piece.color;
}
}
}
}
function keyPressed() {
// Handle keyboard inputs for controlling pieces
if (keyCode === LEFT_ARROW) {
currentPiece.x--;
} else if (keyCode === RIGHT_ARROW) {
currentPiece.x++;
} else if (keyCode === DOWN_ARROW) {
currentPiece.moveDown(board);
}
// Add more controls as needed
}