-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-v1.html
188 lines (174 loc) · 5.64 KB
/
game-v1.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>俄罗斯方块</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #333;
font-family: Arial, sans-serif;
}
#game {
display: grid;
grid-template-columns: repeat(10, 20px);
grid-template-rows: repeat(20, 20px);
gap: 1px;
background-color: #222;
}
.cell {
width: 20px;
height: 20px;
background-color: #444;
}
.tetromino {
background-color: #f39c12;
}
#score {
color: white;
margin-top: 20px;
font-size: 24px;
}
</style>
</head>
<body>
<div>
<div id="game"></div>
<div id="score">Score: 0</div>
</div>
<script>
const game = document.getElementById('game');
const scoreDisplay = document.getElementById('score');
let score = 0;
let grid = Array.from({ length: 20 }, () => Array(10).fill(0));
let currentTetromino;
let currentPosition;
const tetrominoes = [
[[1, 1, 1, 1]], // I
[[1, 1], [1, 1]], // O
[[1, 1, 1], [0, 1, 0]], // T
[[1, 1, 0], [0, 1, 1]], // S
[[0, 1, 1], [1, 1, 0]], // Z
[[1, 1, 1], [1, 0, 0]], // L
[[1, 1, 1], [0, 0, 1]] // J
];
function createGrid() {
for (let row = 0; row < 20; row++) {
for (let col = 0; col < 10; col++) {
const cell = document.createElement('div');
cell.classList.add('cell');
game.appendChild(cell);
}
}
}
function draw() {
const cells = document.querySelectorAll('#game .cell');
cells.forEach((cell, index) => {
const row = Math.floor(index / 10);
const col = index % 10;
if (grid[row][col] === 1) {
cell.classList.add('tetromino');
} else {
cell.classList.remove('tetromino');
}
});
}
function spawnTetromino() {
const randomIndex = Math.floor(Math.random() * tetrominoes.length);
currentTetromino = tetrominoes[randomIndex];
currentPosition = { x: 3, y: 0 };
if (collision()) {
alert('Game Over! Your score: ' + score);
grid = Array.from({ length: 20 }, () => Array(10).fill(0));
score = 0;
scoreDisplay.textContent = 'Score: 0';
}
}
function collision() {
for (let row = 0; row < currentTetromino.length; row++) {
for (let col = 0; col < currentTetromino[row].length; col++) {
if (currentTetromino[row][col] === 1) {
const newX = currentPosition.x + col;
const newY = currentPosition.y + row;
if (newX < 0 || newX >= 10 || newY >= 20 || grid[newY][newX] === 1) {
return true;
}
}
}
}
return false;
}
function merge() {
for (let row = 0; row < currentTetromino.length; row++) {
for (let col = 0; col < currentTetromino[row].length; col++) {
if (currentTetromino[row][col] === 1) {
grid[currentPosition.y + row][currentPosition.x + col] = 1;
}
}
}
}
function clearLines() {
for (let row = 0; row < 20; row++) {
if (grid[row].every(cell => cell === 1)) {
grid.splice(row, 1);
grid.unshift(Array(10).fill(0));
score += 10;
scoreDisplay.textContent = 'Score: ' + score;
}
}
}
function moveDown() {
currentPosition.y++;
if (collision()) {
currentPosition.y--;
merge();
clearLines();
spawnTetromino();
}
}
function moveLeft() {
currentPosition.x--;
if (collision()) {
currentPosition.x++;
}
}
function moveRight() {
currentPosition.x++;
if (collision()) {
currentPosition.x--;
}
}
function rotate() {
const prevTetromino = currentTetromino;
currentTetromino = currentTetromino[0].map((val, index) =>
currentTetromino.map(row => row[index]).reverse()
);
if (collision()) {
currentTetromino = prevTetromino;
}
}
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') {
moveLeft();
} else if (e.key === 'ArrowRight') {
moveRight();
} else if (e.key === 'ArrowDown') {
moveDown();
} else if (e.key === 'ArrowUp') {
rotate();
}
draw();
});
createGrid();
spawnTetromino();
draw();
setInterval(moveDown, 1000);
setInterval(draw, 50);
</script>
</body>
</html>