-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
172 lines (145 loc) · 4.3 KB
/
game.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
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
console.log("Game JS loaded!");
// first, i will get the canvas and set up the context for drawing
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
//game settings
const gridSize = 20; // each grid square is going to be 20x20
const tileCount = canvas.width / gridSize // how many tiles will be on screen.
//game state
let snake = [
{ x: 10, y: 10 } // starting pos opf the snakes head with an array of objects
];
let food = {
x: 15,
y: 15
};
let dx = 0; //direction x (horizontal movement)
let dy = 0; // direction y (vert movement)
let score = 0;
let gameStarted = false;
let gameOver = false;
//this will get our buttons
const startButton = document.getElementById('startBtn');
const resetButton = document.getElementById('resetBtn');
// Debug: Check if elements are found
console.log("Canvas found:", !!canvas);
console.log("Start button found:", !!startButton);
console.log("Reset button found:", !!resetButton);
//add click listeners to the buttons
startButton.addEventListener('click', () => {
console.log("start button was clicked!");
startGame()
});
resetButton.addEventListener('click', () => {
console.log("reset button was clicked!");
resetGame()
});
function startGame() {
console.log("startGame function called");
if(!gameStarted) {
console.log("Game starting...");
gameStarted = true;
gameLoop();
}
}
function resetGame() {
//this is to reset everything back to the normal starting state
snake = [{ x: 10, y: 10 }];
dx = 0;
dy = 0;
score = 0;
gameStarted = false
gameOver = false
generateFood();
drawGame(); // this will draaw it back to inital state;
document.getElementById('score').textContent = `Score: ${score}`;
}
function generateFood() {
//generate random cords for food
food.x = Math.floor(Math.random() * tileCount);
food.y = Math.floor(Math.random() * tileCount);
// this is going to make sure the food doesnt spawn on the snake
snake.forEach(part => {
if(part.x === food.x && part.y === food.y) {
generateFood(); //try to spawn food again if it spawns on snake
}
})
}
// this will be the keyboard controls
document.addEventListener('keydown', function(event) {
if (!gameStarted) return;
switch(event.key) {
case 'ArrowUp':
if (dy === 1) return;
dx = 0;
dy = -1;
break;
case 'ArrowDown':
if (dy === -1) return;
dx = 0;
dy = 1;
break;
case 'ArrowLeft':
if (dx === 1) return;
dx = -1;
dy = 0;
break;
case 'ArrowRight':
if (dx === -1) return;
dx = 1;
dy = 0;
break;
}
});
function gameLoop() {
if (gameOver) return;
//this will move the snake
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head); // this adds a new head
// check if the snake eats the food
if (head.x === food.x && head.y === food.y) {
score += 10;
document.getElementById('score').textContent = `Score: ${score}`;
generateFood();
} else {
snake.pop(); // this will remove tail if no food gets eaten
}
// checks for collisions
if(checkCollision()) {
gameOver = true;
alert(`Game Over! Score: ${score}`);
return;
}
// clear canvas and draws everything
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGame();
//continue the loop
setTimeout(gameLoop, 100); // this adjust speed by changing the timeout
}
function checkCollision() {
const head = snake[0];
//check for wall collision
if(!snake.length || !head) {
return false;
}
if(head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount ) {
return true;
}
//check for self collision (starts from 1 to skip head)
for(let i = 1; i < snake.length; i++) {
if(snake[i] && head.x === snake[i].x && head.y === snake[i].y) {
return true;
}
}
return false;
}
function drawGame() {
//draws the snake
snake.forEach((part, index) => {
ctx.fillStyle = index === 0 ? '#4CAF50' : '#2E7D32'; // head is gonna be lighter green
ctx.fillRect(part.x * gridSize, part.y * gridSize, gridSize - 2, gridSize - 2);
});
// draw the food
ctx.fillStyle = '#FF5000';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2);
}