<?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: Chipm0nk</title>
    <description>The latest articles on DEV Community by Chipm0nk (@chipm0nk).</description>
    <link>https://dev.to/chipm0nk</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%2F2194824%2Fba570014-32e1-42fe-889c-03c8343a6a3f.png</url>
      <title>DEV Community: Chipm0nk</title>
      <link>https://dev.to/chipm0nk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chipm0nk"/>
    <language>en</language>
    <item>
      <title>From Concept to Code: Building a Simple Endless Runner Game</title>
      <dc:creator>Chipm0nk</dc:creator>
      <pubDate>Tue, 05 Nov 2024 04:45:55 +0000</pubDate>
      <link>https://dev.to/chipm0nk/from-concept-to-code-building-a-simple-endless-runner-game-4gcm</link>
      <guid>https://dev.to/chipm0nk/from-concept-to-code-building-a-simple-endless-runner-game-4gcm</guid>
      <description>&lt;p&gt;Creating an endless runner game is a fun way to dive into game development. These games are straightforward, addictive, and allow for plenty of creative tweaks. Think about it: with just a few simple mechanics-running, jumping, dodging-you can make something that keeps players glued to their screens. &lt;/p&gt;

&lt;p&gt;Games like &lt;strong&gt;Temple Run&lt;/strong&gt;, &lt;strong&gt;Flappy Bird&lt;/strong&gt;, and &lt;strong&gt;Subway Surfers&lt;/strong&gt; all started with this same simple idea, yet they've entertained millions worldwide.&lt;/p&gt;

&lt;p&gt;In this guide, I'll show you how to build your own endless runner from scratch. We'll keep things simple and walk through the essential steps to get a basic game up and running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Understand the Core Mechanics
&lt;/h2&gt;

&lt;p&gt;Before diving into code, let's break down what makes an endless runner game work. The core mechanics are simple:&lt;/p&gt;

&lt;p&gt;Automatic movement: The main character moves forward automatically, so the player doesn't need to control the forward motion.&lt;/p&gt;

&lt;p&gt;Obstacles: These appear on the screen, and players need to react quickly to avoid them.&lt;/p&gt;

&lt;p&gt;Player actions: Usually, players can either jump, duck, or maybe shoot to interact with obstacles.&lt;/p&gt;

&lt;p&gt;These core mechanics give us a blueprint for what we'll need to build. The idea is to keep things as simple as possible while ensuring a smooth experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Start with the Basic Structure
&lt;/h2&gt;

&lt;p&gt;To build a basic endless runner, you can start by setting up a structure in HTML, CSS, and JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up the HTML Canvas
&lt;/h3&gt;

&lt;p&gt;First, create a basic HTML file with a canvas element. Here's what that might look like:&lt;/p&gt;

&lt;p&gt;First, create a basic HTML file with a canvas element:&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;Endless Runner&amp;lt;/title&amp;gt;
  &amp;lt;style&amp;gt;
    canvas {
      background-color: #eee;
      display: block;
      margin: 0 auto;
    }
  &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;canvas id="gameCanvas" width="800" height="400"&amp;gt;&amp;lt;/canvas&amp;gt;
  &amp;lt;script src="game.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;h2&gt;
  
  
  Configuring the Canvas in JavaScript
&lt;/h2&gt;

&lt;p&gt;Next, we'll need to create a basic game loop in JavaScript to keep our game running. The game loop is the "heart" of the game, updating the screen every frame.&lt;/p&gt;

&lt;p&gt;Here's a simple version of a game loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");

function gameLoop() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  // Game code goes here

  requestAnimationFrame(gameLoop); // keeps the loop going
}

gameLoop();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: We're using requestAnimationFrame to run the gameLoop function continuously, which will update the game every frame. This function clears the screen, and later we'll add our game objects here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Adding the Player Character
&lt;/h2&gt;

&lt;p&gt;In endless runners, the player character often only has one or two actions, like jumping or ducking. For this guide, let's keep it simple and focus on jumping.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Simple Player Object
&lt;/h3&gt;

&lt;p&gt;We'll define the player as an object with properties like position, size, and velocity. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const player = {
  x: 50,
  y: canvas.height - 60,
  width: 50,
  height: 50,
  color: "blue",
  velocityY: 0,
  isJumping: false
};

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

&lt;/div&gt;



&lt;p&gt;This gives us a square "player" who can jump. The &lt;code&gt;isJumping&lt;/code&gt; property will help us control whether the player can jump again or not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Drawing the Player on the Screen
&lt;/h3&gt;

&lt;p&gt;Add the following to the &lt;code&gt;gameLoop&lt;/code&gt; to draw the player:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function drawPlayer() {
  ctx.fillStyle = player.color;
  ctx.fillRect(player.x, player.y, player.width, player.height);
}

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

&lt;/div&gt;



&lt;p&gt;Call &lt;code&gt;drawPlayer()&lt;/code&gt; in the game loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function gameLoop() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  drawPlayer();
  requestAnimationFrame(gameLoop);
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Adding Jump Mechanics
&lt;/h2&gt;

&lt;p&gt;To make the player jump, we'll listen for keyboard input. When the player presses the "Space" key, the player character should jump.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling the Jump Action
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("keydown", (event) =&amp;gt; {
  if (event.code === "Space" &amp;amp;&amp;amp; !player.isJumping) {
    player.velocityY = -10;
    player.isJumping = true;
  }
});

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

&lt;/div&gt;



&lt;p&gt;When &lt;strong&gt;Space&lt;/strong&gt; is pressed, we set the player's vertical velocity to a negative value so it moves upward. We also set &lt;code&gt;isJumping&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;, preventing double jumps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing Gravity
&lt;/h3&gt;

&lt;p&gt;Gravity will bring the player back down after they jump. This can be done by adding a constant force pulling the player down each frame:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function updatePlayer() {
  player.y += player.velocityY;
  player.velocityY += 0.5; // Gravity effect

  // Ground detection
  if (player.y &amp;gt; canvas.height - player.height) {
    player.y = canvas.height - player.height;
    player.velocityY = 0;
    player.isJumping = false;
  }
}

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

&lt;/div&gt;



&lt;p&gt;Now, call &lt;code&gt;updatePlayer()&lt;/code&gt; in the &lt;code&gt;gameLoop&lt;/code&gt; to make the player fall back down after jumping.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Creating Obstacles
&lt;/h2&gt;

&lt;p&gt;Obstacles make endless runners challenging. They appear from the right side of the screen and move left. If the player hits an obstacle, it's game over.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining Obstacle Properties
&lt;/h3&gt;

&lt;p&gt;Here's a simple way to set up obstacles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obstacles = [];

function createObstacle() {
  obstacles.push({
    x: canvas.width,
    y: canvas.height - 50,
    width: 20,
    height: 50,
    color: "red"
  });
}

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

&lt;/div&gt;



&lt;p&gt;This function creates a new obstacle at the right edge of the canvas. We can then move it left each frame.&lt;/p&gt;

&lt;h3&gt;
  
  
  Moving Obstacles
&lt;/h3&gt;

&lt;p&gt;In gameLoop, add a function to move and draw obstacles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function updateObstacles() {
  obstacles.forEach((obstacle, index) =&amp;gt; {
    obstacle.x -= 5; // Speed of obstacle

    // Remove obstacles that go off-screen
    if (obstacle.x + obstacle.width &amp;lt; 0) {
      obstacles.splice(index, 1);
    }
  });
}

function drawObstacles() {
  obstacles.forEach(obstacle =&amp;gt; {
    ctx.fillStyle = obstacle.color;
    ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
  });
}

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

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;updateObstacles()&lt;/code&gt; and &lt;code&gt;drawObstacles()&lt;/code&gt; to &lt;code&gt;gameLoop&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Collision Detection
&lt;/h2&gt;

&lt;p&gt;Now, let's add collision detection. If the player hits an obstacle, the game will stop or restart.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function checkCollision() {
  obstacles.forEach(obstacle =&amp;gt; {
    if (
      player.x &amp;lt; obstacle.x + obstacle.width &amp;amp;&amp;amp;
      player.x + player.width &amp;gt; obstacle.x &amp;amp;&amp;amp;
      player.y &amp;lt; obstacle.y + obstacle.height &amp;amp;&amp;amp;
      player.y + player.height &amp;gt; obstacle.y
    ) {
      alert("Game Over!");
      window.location.reload();
    }
  });
}

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

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;checkCollision()&lt;/code&gt; inside the &lt;code&gt;gameLoop&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Bringing It All Together
&lt;/h2&gt;

&lt;p&gt;Here's the complete gameLoop after adding all these functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function gameLoop() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  updatePlayer();
  updateObstacles();
  checkCollision();

  drawPlayer();
  drawObstacles();

  requestAnimationFrame(gameLoop);
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Finally...
&lt;/h3&gt;

&lt;p&gt;Endless runner games are simple to create but offer a lot of room for creativity. You can add different obstacles, power-ups, and even level progression. Start small, and as you grow, add more features to make your game unique.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who is behind this post?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So this is &lt;a href="https://scratch.mit.edu/users/chipm0nk/" rel="noopener noreferrer"&gt;Chipm0nk&lt;/a&gt; a Scratch game developer, currently working on a project to make &lt;a href="https://scratchgeometrydash.org/" rel="noopener noreferrer"&gt;geometry dash&lt;/a&gt; an endless runner game. See you in the comment box!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>css</category>
      <category>gamedev</category>
    </item>
  </channel>
</rss>
