<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alexya</title>
    <description>The latest articles on DEV Community by Alexya (@alexya).</description>
    <link>https://dev.to/alexya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2640571%2F51768b91-0534-42e6-abf2-ba16ecdd1c2b.png</url>
      <title>DEV Community: Alexya</title>
      <link>https://dev.to/alexya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexya"/>
    <language>en</language>
    <item>
      <title>The old school snake game with just 100 lines of JS code</title>
      <dc:creator>Alexya</dc:creator>
      <pubDate>Thu, 02 Jan 2025 16:19:21 +0000</pubDate>
      <link>https://dev.to/alexya/the-old-school-snake-game-with-just-100-lines-of-js-code-2c62</link>
      <guid>https://dev.to/alexya/the-old-school-snake-game-with-just-100-lines-of-js-code-2c62</guid>
      <description>&lt;p&gt;If you grew up in the late '90s or early 2000s, you probably remember the classic Snake game.&lt;/p&gt;

&lt;p&gt;It was simple, addictive, and a PUBG of early mobile gaming. I recently decided to recreate it using just 100 lines of Javascript.&lt;/p&gt;

&lt;p&gt;Here is the HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset="UTF-8"&amp;gt;
  &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
  &amp;lt;title&amp;gt;Snake Game&amp;lt;/title&amp;gt;
  &amp;lt;link rel="stylesheet" href="styles.css"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;div class="container"&amp;gt;
    &amp;lt;h1&amp;gt;Snake Game&amp;lt;/h1&amp;gt;
    &amp;lt;div id="score"&amp;gt;Score: 0&amp;lt;/div&amp;gt;
    &amp;lt;div id="game-board"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;button id="start-restart"&amp;gt;Start / Restart&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f3f3f3;
}

.container {
  text-align: center;
}

#game-board {
  display: grid;
  grid-template-columns: repeat(20, 20px);
  grid-template-rows: repeat(20, 20px);
  gap: 1px;
  background-color: #e0e0e0;
  margin: 20px auto;
  width: 400px;
  height: 400px;
  position: relative;
}

.cell {
  width: 20px;
  height: 20px;
  background-color: #f3f3f3;
}

.snake {
  background-color: green;
}

.food {
  background-color: red;
}

#score {
  font-size: 20px;
  margin: 10px 0;
}

button {
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  border: none;
  background-color: #007BFF;
  color: white;
  border-radius: 5px;
}

button:hover {
  background-color: #0056b3;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const board = document.getElementById('game-board');
const scoreDisplay = document.getElementById('score');
const startRestartButton = document.getElementById('start-restart');

const gridSize = 20;
const boardSize = gridSize * gridSize;
let snake = [42, 41, 40]; // Starting position
let direction = 1; // Moving right
let nextDirection = 1; // Buffer for next direction
let food = null;
let score = 0;
let intervalId = null;
const speed = 100;

function createBoard() {
  board.innerHTML = '';
  for (let i = 0; i &amp;lt; boardSize; i++) {
    const cell = document.createElement('div');
    cell.classList.add('cell');
    board.appendChild(cell);
  }
}

function updateBoard() {
  const cells = document.querySelectorAll('.cell');
  cells.forEach(cell =&amp;gt; cell.classList.remove('snake', 'food'));

  snake.forEach(index =&amp;gt; cells[index].classList.add('snake'));
  if (food !== null) cells[food].classList.add('food');
}

function spawnFood() {
  let newFood;
  do {
    newFood = Math.floor(Math.random() * boardSize);
  } while (snake.includes(newFood));
  food = newFood;
}

function moveSnake() {
  const head = snake[0];
  const newHead = getNewHead(head);

  // Check for collisions with itself
  if (snake.includes(newHead)) {
    clearInterval(intervalId);
    alert(`Game Over! Your final score is ${score}.`);
    return;
  }

  // Move snake
  snake.unshift(newHead);
  if (newHead === food) {
    score++;
    scoreDisplay.textContent = `Score: ${score}`;
    spawnFood();
  } else {
    snake.pop();
  }
  direction = nextDirection;
  updateBoard();
}

function getNewHead(head) {
  let newHead = head + nextDirection;

  if (nextDirection === 1 &amp;amp;&amp;amp; head % gridSize === gridSize - 1) {
    newHead = head - (gridSize - 1); // Wrap to the left
  } else if (nextDirection === -1 &amp;amp;&amp;amp; head % gridSize === 0) {
    newHead = head + (gridSize - 1); // Wrap to the right
  } else if (nextDirection === gridSize &amp;amp;&amp;amp; head + gridSize &amp;gt;= boardSize) {
    newHead = head % gridSize; // Wrap to the top
  } else if (nextDirection === -gridSize &amp;amp;&amp;amp; head - gridSize &amp;lt; 0) {
    newHead = head + boardSize - gridSize; // Wrap to the bottom
  }

  return newHead;
}

function changeDirection(e) {
  const key = e.key;

  // Prevent reversing direction
  if (key === 'ArrowUp' &amp;amp;&amp;amp; direction !== gridSize) nextDirection = -gridSize;
  if (key === 'ArrowDown' &amp;amp;&amp;amp; direction !== -gridSize) nextDirection = gridSize;
  if (key === 'ArrowLeft' &amp;amp;&amp;amp; direction !== 1) nextDirection = -1;
  if (key === 'ArrowRight' &amp;amp;&amp;amp; direction !== -1) nextDirection = 1;
}

function startGame() {
  snake = [42, 41, 40];
  direction = 1;
  nextDirection = 1;
  score = 0;
  scoreDisplay.textContent = `Score: ${score}`;
  spawnFood();
  updateBoard();

  if (intervalId) clearInterval(intervalId);
  intervalId = setInterval(moveSnake, speed);
}

createBoard();
startRestartButton.addEventListener('click', startGame);
document.addEventListener('keydown', changeDirection);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can make changes to above code according to what more creativity you can add.&lt;/p&gt;

&lt;p&gt;Try playing here: &lt;a href="https://codepen.io/alexya99/pen/EaYbaxo" rel="noopener noreferrer"&gt;https://codepen.io/alexya99/pen/EaYbaxo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a game developer I have worked on many gaming projects. You can review our one of gaming portfolio site such as &lt;a href="https://geometrydashspam.com/" rel="noopener noreferrer"&gt;geometry dash spam&lt;/a&gt; full of such web based games.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
