-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.html
156 lines (144 loc) · 4.84 KB
/
game.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Game of Life</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
transition: background-color 0.5s, color 0.5s;
}
body.dark-mode {
background-color: #000000;
color: #fff;
}
#game-container {
text-align: center;
}
canvas {
border: 1px solid #000;
transition: border-color 0.3s;
}
body.dark-mode canvas {
border-color: #fff;
}
button {
margin: 10px;
padding: 5px 10px;
font-size: 16px;
background-color: #f0f0f0;
border: 1px solid #000;
cursor: pointer;
transition: background-color 0.5s, color 0.5s, border-color 0.5s;
}
body.dark-mode button {
background-color: #000000;
color: #fff;
border-color: #fff;
}
</style>
</head>
<body>
<div id="game-container">
<canvas id="gameCanvas" width="1200" height="600"></canvas>
<br>
<button id="startStop">Start</button>
<button id="clear">Clear</button>
<button id="random">Random</button>
<button id="darkMode">Colorscheme</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const cellSize = 10;
const cols = canvas.width / cellSize;
const rows = canvas.height / cellSize;
let grid = createGrid();
let isRunning = false;
let intervalId;
let isDarkMode = false;
function createGrid() {
return new Array(cols).fill(null)
.map(() => new Array(rows).fill(false));
}
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (grid[i][j]) {
ctx.fillStyle = isDarkMode ? 'white' : 'black';
ctx.fillRect(i * cellSize, j * cellSize, cellSize - 1, cellSize - 1);
}
}
}
}
function countNeighbors(x, y) {
let count = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue;
const col = (x + i + cols) % cols;
const row = (y + j + rows) % rows;
if (grid[col][row]) count++;
}
}
return count;
}
function updateGrid() {
const newGrid = createGrid();
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
const neighbors = countNeighbors(i, j);
if (grid[i][j]) {
newGrid[i][j] = neighbors === 2 || neighbors === 3;
} else {
newGrid[i][j] = neighbors === 3;
}
}
}
grid = newGrid;
}
function gameLoop() {
updateGrid();
drawGrid();
}
canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect();
const x = Math.floor((event.clientX - rect.left) / cellSize);
const y = Math.floor((event.clientY - rect.top) / cellSize);
grid[x][y] = !grid[x][y];
drawGrid();
});
document.getElementById('startStop').addEventListener('click', () => {
if (isRunning) {
clearInterval(intervalId);
document.getElementById('startStop').textContent = 'Start';
} else {
intervalId = setInterval(gameLoop, 100);
document.getElementById('startStop').textContent = 'Stop';
}
isRunning = !isRunning;
});
document.getElementById('clear').addEventListener('click', () => {
grid = createGrid();
drawGrid();
});
document.getElementById('random').addEventListener('click', () => {
grid = grid.map(row => row.map(() => Math.random() > 0.9));
drawGrid();
});
document.getElementById('darkMode').addEventListener('click', () => {
isDarkMode = !isDarkMode;
document.body.classList.toggle('dark-mode');
drawGrid();
});
drawGrid();
</script>
</body>
</html>