Тетрис
26.04.2024, 19:24. Показов 405. Ответов 0
решил тут попробовать написать тетрис но что то не могу разобраться с переворотом фигуры, после того как нажимаю на стрелку вверх фигура пропадает с игрового поля кто что может посоветовать
HTML5 | 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
| <!DOCTYPE html>
<html>
<head>
<title>Тетрис</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #000;
}
canvas {
border: 1px solid #fff;
}
</style>
</head>
<body>
<canvas id="tetris" width="240" height="400"></canvas>
<script>
const canvas = document.getElementById('tetris');
const context = canvas.getContext('2d');
// Размеры игрового поля
const cellSize = 20;
const rows = canvas.height / cellSize;
const cols = canvas.width / cellSize;
// Инициализация игрового поля
let board = Array.from({ length: rows }, () => Array(cols).fill(0));
// Фигуры Тетрис
const shapes = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]]
];
// Текущая фигура
let currentShape = shapes[Math.floor(Math.random() * shapes.length)];
let currentX = 5;
let currentY = 0;
// Отрисовка игрового поля
function drawBoard() {
context.clearRect(0, 0, canvas.width, canvas.height);
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
if (board[y][x] !== 0) {
context.fillStyle = 'blue';
context.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
}
// Отрисовка фигуры
function drawShape() {
currentShape.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
context.fillStyle = 'red';
context.fillRect(
(currentX + x) * cellSize,
(currentY + y) * cellSize,
cellSize,
cellSize
);
}
});
});
}
// Проверка столкновений
function collision(board, shape, x, y) {
for (let yy = 0; yy < shape.length; yy++) {
for (let xx = 0; xx < shape[yy].length; xx++) {
if (shape[yy][xx] !== 0 &&
(board[y + yy] && board[y + yy][x + xx]) !== 0) {
return true;
}
}
}
return false;
}
// Вращение фигуры
function rotate(shape) {
const newShape = shape[0].map((val, index) => shape.map(row => row[index]));
return newShape[0].reverse();
}
// Обработка нажатий клавиш
function keyDown(e) {
switch (e.keyCode) {
case 37: // Влево
move(-1);
break;
case 39: // Вправо
move(1);
break;
case 40: // Вниз
drop();
break;
case 38: // Вверх
const rotatedShape = rotate(currentShape);
if (!collision(board, rotatedShape, currentX, currentY)) {
currentShape = rotatedShape;
}
break;
}
}
// Функция падения
function drop() {
if (!collision(board, currentShape, currentX, currentY + 1)) {
currentY++;
} else {
updateBoard();
currentShape = shapes[Math.floor(Math.random() * shapes.length)];
currentX = 5;
currentY = 0;
}
}
// Перемещение фигуры
function move(dx) {
if (!collision(board, currentShape, currentX + dx, currentY)) {
currentX += dx;
}
}
// Обновление игрового поля
function updateBoard() {
currentShape.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
board[currentY + y][currentX + x] = 1;
}
});
});
}
// Основной игровой цикл
function gameLoop() {
drawBoard();
drawShape();
requestAnimationFrame(gameLoop);
}
// Запуск игры
gameLoop();
// Автоматическое падение фигур
setInterval(drop, 1000);
// Добавление обработчика событий клавиатуры
document.addEventListener('keydown', keyDown);
</script>
</body>
</html> |
|
Добавлено через 2 часа 5 минут
Я сделал !! правда есть баги но это мелочи, вот код кому интересно
HTML5 | 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
| <!DOCTYPE html>
<html>
<head>
<title>Тетрис</title>
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #000;
}
canvas {
border: 1px solid #fff;
}
#score {
color: #fff;
font-size: 24px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<canvas id="tetris" width="240" height="400"></canvas>
<script>
const canvas = document.getElementById('tetris');
const context = canvas.getContext('2d');
const cellSize = 20;
const rows = canvas.height / cellSize;
const cols = canvas.width / cellSize;
let board = Array.from({ length: rows }, () => Array(cols).fill(0));
const shapes = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]]
];
let currentShape = shapes[Math.floor(Math.random() * shapes.length)];
let currentX = 5;
let currentY = 0;
let score = 0;
function drawBoard() {
context.clearRect(0, 0, canvas.width, canvas.height);
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
if (board[y][x] !== 0) {
context.fillStyle = 'blue';
context.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
}
function drawShape() {
currentShape.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
context.fillStyle = 'red';
context.fillRect(
(currentX + x) * cellSize,
(currentY + y) * cellSize,
cellSize,
cellSize
);
}
});
});
}
function collision(board, shape, x, y) {
for (let yy = 0; yy < shape.length; yy++) {
for (let xx = 0; xx < shape[yy].length; xx++) {
if (shape[yy][xx] !== 0 &&
(board[y + yy] && board[y + yy][x + xx]) !== 0) {
return true;
}
}
}
return false;
}
function rotate(shape) {
const transposed = shape[0].map((_, i) => shape.map(row => row[i]));
const rotated = transposed.map(row => row.reverse());
for (let y = 0; y < rotated.length; y++) {
for (let x = 0; x < rotated[y].length; x++) {
if (rotated[y][x] !== 0 &&
(currentX + x >= cols || currentY + y >= rows || board[currentY + y][currentX + x] !== 0)) {
return shape;
}
}
}
return rotated;
}
function keyDown(e) {
switch (e.keyCode) {
case 37: // Влево
move(-1);
break;
case 39: // Вправо
move(1);
break;
case 40: // Вниз
drop();
break;
case 38: // Вверх
const rotatedShape = rotate(currentShape);
if (!collision(board, rotatedShape, currentX, currentY)) {
currentShape = rotatedShape;
}
break;
}
}
function drop() {
if (!collision(board, currentShape, currentX, currentY + 1)) {
currentY++;
} else {
updateBoard();
currentShape = shapes[Math.floor(Math.random() * shapes.length)];
currentX = 5;
currentY = 0;
}
}
function move(dx) {
if (!collision(board, currentShape, currentX + dx, currentY)) {
currentX += dx;
}
}
function updateBoard() {
currentShape.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
board[currentY + y][currentX + x] = 1;
}
});
});
clearCompletedRows();
}
function clearCompletedRows() {
let rowsCleared = 0;
outer: for (let y = rows - 1; y >= 0; --y) {
for (let x = 0; x < cols; ++x) {
if (board[y][x] === 0) {
continue outer;
}
}
board.splice(y, 1);
board.unshift(new Array(cols).fill(0));
rowsCleared++;
}
score += rowsCleared;
updateScoreDisplay();
}
function updateScoreDisplay() {
document.getElementById('score').textContent = `Score: ${score}`;
}
function gameLoop() {
drawBoard();
drawShape();
requestAnimationFrame(gameLoop);
}
gameLoop();
setInterval(drop, 1000);
document.addEventListener('keydown', keyDown);
// Initial score display
updateScoreDisplay();
</script>
</body>
</html> |
|
0
|