-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
144 lines (137 loc) 路 4.88 KB
/
Copy pathsnake.js
File metadata and controls
144 lines (137 loc) 路 4.88 KB
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
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const playerNameInput = document.getElementById("player-name");
const startButton = document.getElementById("start-game-btn");
const welcomeScreen = document.getElementById("welcome-screen");
const gameScreen = document.getElementById("game-screen");
const gameOverScreen = document.getElementById("game-over-screen");
const complimentMessage = document.getElementById("compliment-message");
const errorMessage = document.getElementById("error-message");
let snake = [{ x: 10, y: 10 }];
let snakeLength = 1;
let direction = 'RIGHT';
let food = { x: 15, y: 15 };
let score = 0;
let gameInterval;
let isGameOver = false;
let gameSpeed = 200;
const validNames = ["Junaid Nazar", "JUNAID NAZAR", "junaid nazar", "junaid"];
const compliments = [
"Mr. Junaid Nazar, you are an inspiration to everyone around you!",
"Your wisdom and guidance have truly shaped the minds of your students, Mr. Junaid Nazar.",
"Mr. Junaid Nazar, your dedication to teaching is unmatched.",
"You make learning fun and impactful, Mr. Junaid Nazar!",
"Your hard work and commitment to your students don't go unnoticed, Mr. Junaid Nazar.",
"Mr. Junaid Nazar, your passion for teaching is truly admirable.",
"The way you explain concepts makes even the most challenging topics seem easy, Mr. Junaid Nazar.",
"Your enthusiasm and energy are contagious, Mr. Junaid Nazar!",
"Mr. Junaid Nazar, your encouragement and support motivate us to do our best.",
"You are a remarkable teacher, Mr. Junaid Nazar, and we are lucky to learn from you!"
];
function startGame() {
const playerName = playerNameInput.value.trim();
if (validNames.includes(playerName)) {
welcomeScreen.classList.add("hidden");
gameScreen.classList.remove("hidden");
startGameLogic();
} else {
errorMessage.textContent = "Sorry, this game is only for Sir. Junaid Nazar";
}
}
function startGameLogic() {
document.addEventListener("keydown", changeDirection);
isGameOver = false;
snake = [{x: 10, y: 10}];
snakeLength = 1;
score = 0;
direction = 'RIGHT';
gameSpeed = 200; // Reset speed to initial value
startGameInterval();
}
function startGameInterval() {
clearInterval(gameInterval); // Clear any existing interval
gameInterval = setInterval(gameLoop, gameSpeed);
}
function gameLoop() {
if (isGameOver) return;
moveSnake();
if (checkCollision()) {
gameOver();
return;
}
if (eatFood()) {
score += 10;
generateFood();
}
drawGame();
}
function moveSnake() {
let head = {...snake[0]};
if (direction === 'UP') head.y -= 1;
if (direction === 'DOWN') head.y += 1;
if (direction === 'LEFT') head.x -= 1;
if (direction === 'RIGHT') head.x += 1;
snake.unshift(head);
if (snake.length > snakeLength) {
snake.pop();
}
}
function changeDirection(event) {
if (event.key === "ArrowUp" && direction !== "DOWN") direction = "UP";
if (event.key === "ArrowDown" && direction !== "UP") direction = "DOWN";
if (event.key === "ArrowLeft" && direction !== "RIGHT") direction = "LEFT";
if (event.key === "ArrowRight" && direction !== "LEFT") direction = "RIGHT";
}
function checkCollision() {
let head = snake[0];
if (head.x < 0 || head.x >= canvas.width / 10 || head.y < 0 || head.y >= canvas.height / 10) {
return true;
}
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
return true;
}
}
return false;
}
function eatFood() {
let head = snake[0];
if (head.x === food.x && head.y === food.y) {
snakeLength++;
increaseSpeed(); // Increase speed when food is eaten
return true;
}
return false;
}
function increaseSpeed() {
gameSpeed = Math.max(50, gameSpeed * 0.90);
startGameInterval(); // Restart the game loop with the new speed
}
function generateFood() {
food = {
x: Math.floor(Math.random() * (canvas.width / 10)),
y: Math.floor(Math.random() * (canvas.height / 10))
};
}
function drawGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? "green" : "lightgreen";
ctx.fillRect(segment.x * 10, segment.y * 10, 10, 10);
});
ctx.fillStyle = "red";
ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
ctx.fillStyle = "white";
ctx.fillText("Score: " + score, 10, 10);
}
function gameOver() {
clearInterval(gameInterval);
gameOverScreen.classList.remove("hidden");
let randomMessage = compliments[Math.floor(Math.random() * compliments.length)];
complimentMessage.textContent = randomMessage;
}
function restartGame() {
gameOverScreen.classList.add("hidden");
startGameLogic();
}
startButton.addEventListener("click", startGame);