-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (71 loc) · 2.78 KB
/
script.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
document.addEventListener('DOMContentLoaded', () => {
const introBox = document.getElementById('introBox');
const gameBox = document.getElementById('gameBox');
const beginButton = document.getElementById('beginButton');
const pauseButton = document.getElementById('pauseButton');
const applyButton = document.getElementById('applyButton');
const speedInput = document.getElementById('speedInput');
const gameCanvas = document.getElementById('gameCanvas');
const ctx = gameCanvas.getContext('2d');
let isPaused = false;
let speed = 500;
let ant = { x: 20, y: 20, direction: 'right' };
const grid = [];
const gridSize = 40;
const cellSize = 10;
for (let i = 0; i < gridSize; i++) {
grid[i] = [];
for (let j = 0; j < gridSize; j++) {
grid[i][j] = 'white';
}
}
function moveAnt() {
if (isPaused) return;
setTimeout(() => {
let { x, y, direction } = ant;
if (grid[x][y] === 'white') {
ant.direction = direction === 'right' ? 'down' : direction === 'down' ? 'left' : direction === 'left' ? 'up' : 'right';
grid[x][y] = 'black';
} else {
ant.direction = direction === 'right' ? 'up' : direction === 'up' ? 'left' : direction === 'left' ? 'down' : 'right';
grid[x][y] = 'white';
}
drawGrid();
switch (ant.direction) {
case 'right': ant.x = (x + 1) % gridSize; break;
case 'down': ant.y = (y + 1) % gridSize; break;
case 'left': ant.x = (x - 1 + gridSize) % gridSize; break;
case 'up': ant.y = (y - 1 + gridSize) % gridSize; break;
}
moveAnt();
}, speed);
}
function drawGrid() {
ctx.clearRect(0, 0, gameCanvas.width, gameCanvas.height);
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
ctx.fillStyle = grid[i][j];
ctx.fillRect(i * cellSize, j * cellSize, cellSize, cellSize);
}
}
ctx.fillStyle = 'red';
ctx.fillRect(ant.x * cellSize, ant.y * cellSize, cellSize, cellSize);
}
beginButton.addEventListener('click', () => {
introBox.style.opacity = '0';
setTimeout(() => {
introBox.style.display = 'none';
gameBox.style.display = 'block';
gameBox.style.opacity = '1';
moveAnt();
}, 500);
});
pauseButton.addEventListener('click', () => {
isPaused = !isPaused;
pauseButton.textContent = isPaused ? 'Start' : 'Pause';
if (!isPaused) moveAnt();
});
applyButton.addEventListener('click', () => {
speed = parseInt(speedInput.value, 10);
});
});