Skip to content

Commit a1d8619

Browse files
committed
Fix snake game boundary mode and added boundary feature such that if the snake touched the boundary it looses
1 parent 8d4ded5 commit a1d8619

3 files changed

Lines changed: 64 additions & 5 deletions

File tree

projects/snake/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ <h1>Snake Game</h1>
2626
<div class="controls">
2727
<button id="startBtn">Start</button>
2828
<label>Speed <input id="speed" type="range" min="1" max="10" value="5"></label>
29+
30+
31+
</div>
32+
<div class="mode-container">
33+
<label> Mode
34+
<select id="mode">
35+
<option value="normal">Normal</option>
36+
<option value="boundary">Boundary Mode</option>
37+
</select>
38+
</label>
2939
</div>
3040
</div>
3141

projects/snake/main.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ScoreManager } from "./score.js";
33
const canvas = document.getElementById('board');
44
const ctx = canvas.getContext('2d');
55
const size = 20;
6+
let gameMode = "normal";
67

78
// ------------------ INITIALIZE SNAKES ------------------ //
89
let snakeA = [{ x: 3, y: 3 }];
@@ -28,6 +29,7 @@ function reset() {
2829
gameRunning = true;
2930
gamePaused = false;
3031
updateButtonText();
32+
gameMode = document.getElementById("mode").value;
3133
draw();
3234
}
3335

@@ -41,8 +43,8 @@ function spawnFood() {
4143
function tick() {
4244
if (!gameRunning || gamePaused) return;
4345

44-
moveSnake(snakeA, dirA, "A");
45-
moveSnake(snakeB, dirB, "B");
46+
if(!moveSnake(snakeA, dirA, "A")) return;
47+
if(!moveSnake(snakeB, dirB, "B")) return;
4648

4749
if (checkCollision(snakeA) || checkCollision(snakeB) || checkSnakeCollision(snakeA, snakeB)) {
4850
let { scoreA, scoreB } = ScoreManager.getScores();
@@ -57,8 +59,30 @@ function tick() {
5759
function moveSnake(snake, dir, player) {
5860
const head = { x: snake[0].x + dir.x, y: snake[0].y + dir.y };
5961

60-
head.x = (head.x + canvas.width / size) % (canvas.width / size);
61-
head.y = (head.y + canvas.height / size) % (canvas.height / size);
62+
// head.x = (head.x + canvas.width / size) % (canvas.width / size);
63+
// head.y = (head.y + canvas.height / size) % (canvas.height / size);
64+
if (gameMode === "normal") {
65+
66+
head.x = (head.x + canvas.width / size) % (canvas.width / size);
67+
head.y = (head.y + canvas.height / size) % (canvas.height / size);
68+
} else {
69+
70+
if (
71+
head.x < 0 ||
72+
head.x >= canvas.width / size ||
73+
head.y < 0 ||
74+
head.y >= canvas.height / size
75+
) {
76+
const winner = player === "A" ? "Player B" : "Player A";
77+
stop();
78+
gameRunning = false;
79+
gamePaused = false;
80+
updateButtonText();
81+
gameOver(winner);
82+
return false;
83+
}
84+
}
85+
6286

6387
snake.unshift(head);
6488

@@ -68,6 +92,8 @@ function moveSnake(snake, dir, player) {
6892
} else {
6993
snake.pop();
7094
}
95+
96+
return true;
7197
}
7298

7399
function checkCollision(snake) {
@@ -112,6 +138,12 @@ function draw() {
112138
for (let y = 0; y < canvas.height; y += size) {
113139
ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(canvas.width, y); ctx.stroke();
114140
}
141+
if (gameMode === "boundary") {
142+
ctx.strokeStyle = "#ff0000";
143+
ctx.lineWidth = 3;
144+
ctx.strokeRect(1, 1, canvas.width - 2, canvas.height - 2);
145+
}
146+
115147

116148
// Pause overlay
117149
if (gamePaused) {
@@ -182,7 +214,9 @@ document.addEventListener('keydown', (e) => {
182214
});
183215

184216
// ------------------ EVENT LISTENERS ------------------ //
185-
217+
document.getElementById("mode").addEventListener("change", function () {
218+
gameMode = this.value;
219+
});
186220
document.getElementById('startBtn').addEventListener('click', handleButtonClick);
187221
document.getElementById('speed').addEventListener('input', function () {
188222
if (gameRunning && !gamePaused) {

projects/snake/styles.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ input[type="range"] {
8383
width: 120px;
8484
margin-left: 10px;
8585
}
86+
#mode {
87+
background-color: #2b2b44;
88+
color: #fff;
89+
border: 2px solid #00ff00;
90+
border-radius: 10px;
91+
padding: 6px 12px;
92+
font-size: 14px;
93+
cursor: pointer;
94+
transition: 0.2s ease;
95+
96+
}
97+
98+
.mode-container{
99+
margin-left: 7.5rem;
100+
}
86101

87102

88103
.rules {

0 commit comments

Comments
 (0)