Skip to content

Commit ee503d8

Browse files
authored
Merge pull request #65 from Dhanush-Annam/snake-game-js
Added Snake Game in JavaScript
2 parents 48768b2 + 320f532 commit ee503d8

File tree

5 files changed

+314
-2
lines changed

5 files changed

+314
-2
lines changed

INDEX.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Welcome to Game_Scripts — an open-source collection of mini games!
44
This index automatically tracks all games across different programming languages.
5-
Last updated: Wed Oct 15 20:32:09 UTC 2025
5+
Last updated: Fri Oct 17 14:33:32 UTC 2025
66

7-
Tracked 21 games across 3 languages.
7+
Tracked 26 games across 3 languages.
88

99
## 📚 Table of Contents
1010
- [Java](#java-games)
@@ -19,6 +19,9 @@ A fun game built with core programming concepts
1919
### 🎯 [Hangman](./Java/Hangman/)
2020
🎮 Hangman GUI Game
2121

22+
### 🎯 [LogGame](./Java/LogGame/)
23+
A simple Java Swing application that lets users create and manage a live log through an interactive GUI.
24+
2225
### 🎯 [MemoryCardGame(GUI)](./Java/MemoryCardGame(GUI)/)
2326
🎮 Memory Card Matching Game (GUI)
2427

@@ -28,6 +31,9 @@ A fun game built with core programming concepts
2831
### 🎯 [PongGameGUI](./Java/PongGameGUI/)
2932
🎮 Pong Game GUI
3033

34+
### 🎯 [SnakeGame](./Java/SnakeGame/)
35+
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
36+
3137
### 🎯 [TicTacToe](./Java/TicTacToe/)
3238
The Number Guessing Game GUI is a simple and interactive game built using Java Swing.
3339

@@ -48,6 +54,12 @@ A fun game built with core programming concepts
4854
### 🎯 [Stopwatch App](./Javascript/Stopwatch App/)
4955
A simple yet elegant web-based stopwatch application with start, stop, and reset functionality.
5056

57+
### 🎯 [Typing Speed Game](./Javascript/Typing Speed Game/)
58+
A fast-paced browser game built with HTML, CSS, and JavaScript. Words fall from the top of the screen — type them before they hit the bottom! Designed to improve typing speed, accuracy, and reflexes.
59+
60+
### 🎯 [Weather Site](./Javascript/Weather Site/)
61+
A fun game built with core programming concepts
62+
5163
## Python Games
5264

5365
### 🎯 [Brick_Breaker](./Python/Brick_Breaker/)
@@ -65,6 +77,9 @@ A fun game built with core programming concepts
6577
### 🎯 [Snake_Game](./Python/Snake_Game/)
6678
- Language: Python
6779

80+
### 🎯 [snake_game_pygame](./Python/snake_game_pygame/)
81+
🐍 Snake Game – Pygame Edition
82+
6883
### 🎯 [Snake_Water_Gun](./Python/Snake_Water_Gun/)
6984
By now we have 2 numbers (variables), you and computer
7085

Javascript/Snake Game/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 🐍 Classic Retro Snake Game
2+
3+
A simple **retro-style Snake Game** built using **HTML, CSS, and JavaScript**.
4+
It features a clean pixel grid design inspired by old-school mobile games (like the Nokia Snake), with a welcome screen, pause/resume, and restart functionality.
5+
6+
---
7+
8+
## 🎮 How to Play
9+
- Use the **Arrow Keys** to move the snake:
10+
- ⬅️ Left
11+
- ➡️ Right
12+
- ⬆️ Up
13+
- ⬇️ Down
14+
- Eat the **red apple** 🍎 to grow longer and increase your score.
15+
- Avoid running into the walls or your own tail — that ends the game.
16+
- You can **pause** the game anytime using the Pause button.
17+
18+
---
19+
20+
## ✨ Features
21+
- 🕹️ Classic grid-based snake movement
22+
- ⏸️ Pause / Resume button
23+
- 🧾 Start and Game Over screens
24+
- 📊 Real-time score display
25+
- 🟩 Retro terminal-style design (green on black)
26+
27+
---
28+
29+
---
30+
31+
## 📦 Tech Stack
32+
33+
- **HTML**: Structure and layout
34+
- **CSS**: Styling and responsive design
35+
- **JavaScript**: Game logic, animation, and input handling
36+
37+
---
38+
39+
## 🛠️ How to Run
40+
41+
1. Clone the repository:
42+
```bash
43+
git clone https://github.com/devmalik7/Game_Scripts.git
44+
cd Javascript
45+
cd "Snake Game"
46+
```
47+
2. Run **index.html** in your browser.

Javascript/Snake Game/index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Snake Game</title>
7+
<link rel="stylesheet" href="style.css" />
8+
</head>
9+
<body>
10+
<div id="welcomescreen" class="screen">
11+
<h1>Welcome to Snake Game</h1>
12+
<p>
13+
Use the <b>Arrow Keys</b> to move.<br />
14+
Eat the red apples to grow.<br />
15+
Avoid hitting the walls or yourself!<br /><br />
16+
Click <b>Pause</b> to pause/resume.
17+
</p>
18+
<button id="startbtn">Start Game</button>
19+
</div>
20+
21+
<div id="gamescreen" class="screen hidden">
22+
<div class="topbar">
23+
<span id="score">Score: 0</span>
24+
<button id="pausebtn">Pause</button>
25+
</div>
26+
<div class="grid" id="grid"></div>
27+
</div>
28+
29+
<div id="gameoverscreen" class="screen hidden">
30+
<h1>Game Over</h1>
31+
<p id="finalscore">Your score: 0</p>
32+
<button id="restartbtn">Restart</button>
33+
</div>
34+
35+
<script src="script.js"></script>
36+
</body>
37+
</html>

Javascript/Snake Game/script.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
const gridElement = document.getElementById("grid");
2+
const startBtn = document.getElementById("startbtn");
3+
const pauseBtn = document.getElementById("pausebtn");
4+
const restartBtn = document.getElementById("restartbtn");
5+
const scoreElement = document.getElementById("score");
6+
const finalscoreElement = document.getElementById("finalscore");
7+
8+
const welcomeScreen = document.getElementById("welcomescreen");
9+
const gameScreen = document.getElementById("gamescreen");
10+
const gameoverScreen = document.getElementById("gameoverscreen");
11+
12+
let squares = [];
13+
let currentSnake = [2, 1, 0];
14+
let direction = 1;
15+
const width = 10;
16+
let appleIndex = 0;
17+
let score = 0;
18+
let intervalTime = 300;
19+
let speed = 0.9;
20+
let timerId;
21+
let paused = false;
22+
let started = false;
23+
24+
function createGrid() {
25+
gridElement.innerHTML = "";
26+
squares = [];
27+
for (let i = 0; i < width * width; i++) {
28+
const square = document.createElement("div");
29+
square.classList.add("square");
30+
gridElement.appendChild(square);
31+
squares.push(square);
32+
}
33+
}
34+
35+
function startGame() {
36+
welcomeScreen.classList.add("hidden");
37+
gameoverScreen.classList.add("hidden");
38+
gameScreen.classList.remove("hidden");
39+
40+
createGrid();
41+
currentSnake = [2, 1, 0];
42+
direction = 1;
43+
score = 0;
44+
intervalTime = 400;
45+
paused = false;
46+
started = true;
47+
scoreElement.textContent = `Score: ${score}`;
48+
pauseBtn.textContent = "Pause";
49+
50+
squares.forEach((i) => i.classList.remove("snake", "apple"));
51+
currentSnake.forEach((i) => squares[i].classList.add("snake"));
52+
53+
generateApple();
54+
55+
clearInterval(timerId);
56+
timerId = setInterval(move, intervalTime);
57+
}
58+
59+
function move() {
60+
if (paused) return;
61+
62+
if (
63+
(currentSnake[0] + width >= width * width && direction === width) ||
64+
(currentSnake[0] % width === width - 1 && direction === 1) ||
65+
(currentSnake[0] % width === 0 && direction === -1) ||
66+
(currentSnake[0] - width < 0 && direction === -width) ||
67+
squares[currentSnake[0] + direction].classList.contains("snake")
68+
) {
69+
clearInterval(timerId);
70+
gameOver();
71+
return;
72+
}
73+
74+
const tail = currentSnake.pop();
75+
squares[tail].classList.remove("snake");
76+
currentSnake.unshift(currentSnake[0] + direction);
77+
78+
if (squares[currentSnake[0]].classList.contains("apple")) {
79+
squares[currentSnake[0]].classList.remove("apple");
80+
squares[tail].classList.add("snake");
81+
currentSnake.push(tail);
82+
generateApple();
83+
score++;
84+
scoreElement.textContent = `Score: ${score}`;
85+
clearInterval(timerId);
86+
if (intervalTime > 200) {
87+
intervalTime = intervalTime * speed;
88+
}
89+
timerId = setInterval(move, intervalTime);
90+
}
91+
92+
squares[currentSnake[0]].classList.add("snake");
93+
}
94+
95+
function generateApple() {
96+
do {
97+
appleIndex = Math.floor(Math.random() * squares.length);
98+
} while (squares[appleIndex].classList.contains("snake"));
99+
squares[appleIndex].classList.add("apple");
100+
}
101+
102+
function control(k) {
103+
if (!started || paused) return;
104+
if (k.key === "ArrowRight" && direction !== -1) direction = 1;
105+
else if (k.key === "ArrowUp" && direction !== width) direction = -width;
106+
else if (k.key === "ArrowLeft" && direction !== 1) direction = -1;
107+
else if (k.key === "ArrowDown" && direction !== -width) direction = +width;
108+
}
109+
110+
document.addEventListener("keydown", control);
111+
112+
function stopGame() {
113+
if (!started) return;
114+
paused = !paused;
115+
pauseBtn.textContent = paused ? "Resume" : "Pause";
116+
}
117+
118+
function gameOver() {
119+
started = false;
120+
gameScreen.classList.add("hidden");
121+
gameoverScreen.classList.remove("hidden");
122+
finalscoreElement.textContent = `Your score: ${score}`;
123+
}
124+
125+
startBtn.addEventListener("click", startGame);
126+
restartBtn.addEventListener("click", startGame);
127+
pauseBtn.addEventListener("click", stopGame);

Javascript/Snake Game/style.css

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
body {
2+
background-color: #000;
3+
color: #0f0;
4+
font-family: "Courier New", monospace;
5+
display: flex;
6+
justify-content: center;
7+
align-items: center;
8+
height: 100vh;
9+
margin: 0;
10+
}
11+
12+
.screen {
13+
text-align: center;
14+
}
15+
16+
.hidden {
17+
display: none;
18+
}
19+
20+
.grid {
21+
width: 400px;
22+
height: 400px;
23+
display: grid;
24+
grid-template-columns: repeat(10, 1fr);
25+
grid-template-rows: repeat(10, 1fr);
26+
background-color: #111;
27+
border: 3px solid #0f0;
28+
margin: 20px auto;
29+
box-shadow: 0 0 0 3px #030, inset 0 0 10px #030;
30+
}
31+
32+
.snake {
33+
background-color: #0f0;
34+
border: 1px solid #090;
35+
box-sizing: border-box;
36+
}
37+
38+
.apple {
39+
background-color: #f00;
40+
border: 1px solid #800;
41+
box-sizing: border-box;
42+
}
43+
44+
.topbar {
45+
display: flex;
46+
justify-content: space-between;
47+
align-items: center;
48+
width: 400px;
49+
margin: 0 auto;
50+
font-size: 16px;
51+
color: #0f0;
52+
}
53+
54+
button {
55+
background-color: #0f0;
56+
color: #000;
57+
border: 2px solid #090;
58+
font-family: "Courier New", monospace;
59+
font-weight: bold;
60+
padding: 8px 18px;
61+
cursor: pointer;
62+
border-radius: 2px;
63+
transition: background-color 0.1s, color 0.1s;
64+
}
65+
66+
button:hover {
67+
background-color: #9f9;
68+
color: #000;
69+
}
70+
71+
h1 {
72+
color: #0f0;
73+
text-shadow: 0 0 8px #090;
74+
}
75+
76+
p {
77+
color: #8f8;
78+
font-size: 15px;
79+
line-height: 1.4;
80+
}
81+
82+
#finalscore {
83+
color: #f66;
84+
font-weight: bold;
85+
margin-top: 10px;
86+
}

0 commit comments

Comments
 (0)