Rock Paper Scissors Game With Pure Javascript (Free Download)

Welcome to a tutorial on how to create a simple rock paper scissors game using pure Javascript. So you are interested to create a Javascript game, want to take one more step into game development, or maybe just do this for a school assignment. Well, it is actually not that difficult, and this guide will walk you through the exact steps to creating your perhaps first Javascript game. Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

TABLE OF CONTENTS

 

Download & Demo How It Works Useful Bits & Links
The End

 

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised. Plus a demo of the complete rock, paper, scissors game.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

ROCK-PAPER-SCISSORS DEMO

YOUR MOVE

COMPUTER’S MOVE

Win – 0 | Lose – 0 | Draw – 0

 

 

HOW IT WORKS

All right, let us now get into more details on how this rock-paper-scissors game works.

 

PART 1) HTML GAME INTERFACE

rps.html
<div id="rps-game">
  <!-- (A) YOUR MOVE -->
  <div id="rps-you">
    <h2>YOUR MOVE</h2>
    <img id="rps-you-move"/>
  </div>

  <!-- (B) COMPUTER'S MOVE -->
  <div id="rps-com">
    <h2>COMPUTER'S MOVE</h2>
    <img id="rps-com-move"/>
  </div>
 
  <!-- (C) SELECT ROCK/PAPER/SCISSORS -->
  <div id="rps-play">
    <select id="rps-you-sel" disabled>
      <option value="rock">Rock</option>
      <option value="paper">Paper</option>
      <option value="scissors">Scissors</option>
    </select>
    <input id="rps-you-go" type="button" value="Go!" disabled/>
  </div>
 
  <!-- (D) SCOREBOARD -->
  <div id="rps-score">
    Win - <span id="rps-win">0</span> |
    Lose - <span id="rps-lose">0</span> |
    Draw - <span id="rps-draw">0</span>
  </div>
</div>

This should be pretty straightforward once you trace through the HTML elements.

  1. <div id="rps-you"> Rock-paper-scissor image that the player has chosen.
  2. <div id="rps-com"> Rock-paper-scissor image that the computer has chosen.
  3. <div id="rps-play"> Chose rock-paper-scissor.
  4. <div id="rps-score"> Scoreboard.

Also, take note that the selector <select id="rps-you-sel"> and button <input id="rps-you-go" type="button" value="Go!"/> are disabled. We will only enable these when everything is fully loaded.

 

 

PART 2) PRELOAD IMAGES

rps.js
// (A) PRELOAD IMAGES
load : () => {
  let loaded = 0;
  for (let i of ["game-rock.png", "game-paper.png", "game-scissors.png"]) {
    let img = new Image();
    img.onload = () => {
      loaded++;
      if (loaded == 3) { rps.init(); }
    };
    img.src = i;
  }
},
window.addEventListener("load", rps.load);

rps.load() is the first thing that runs on window load. This is just a small technique to preload the rock, paper, scissor images before we proceed.

 

PART 3) INITIALIZE GAME

rps.js
// (B) INIT GAME
eYou : null, // your move html image
eCom : null, // computer move html image
eSel : null, // html rock, scissors, paper selector
eGo : null, // html go button
eWin : null, wins : 0, // wins counter
eLose: null, loses : 0, // loses counter
eDraw : null, draws : 0, // draws counter
init : () => {
  // (B1) GET HTML ELEMENTS
  rps.eYou = document.getElementById("rps-you-move");
  rps.eCom = document.getElementById("rps-com-move");
  rps.eSel = document.getElementById("rps-you-sel");
  rps.eGo = document.getElementById("rps-you-go");
  rps.eWin = document.getElementById("rps-win");
  rps.eLose = document.getElementById("rps-lose");
  rps.eDraw = document.getElementById("rps-draw");
 
  // (B2) UPDATE IMAGE WHEN CHANGING ROCK/PAPER/SCISSORS
  rps.eSel.onchange = () => { rps.eYou.src = `game-${rps.eSel.value}.png`; };
  rps.eYou.src = `game-${rps.eSel.value}.png`;
 
  // (B3) WHEN USER HITS "GO!"
  rps.eGo.onclick = rps.game;
 
  // (B4) UNLOCK CONTROLS
  rps.eSel.disabled = false;
  rps.eGo.disabled = false;
},

The game initialization looks confusing at first, but keep calm and look carefully – All this does is to set up the HTML interface. Nothing more.

 

 

PART 4) INITIALIZE GAME

rps.js
// (C) GAME "ENGINE"
game : () => {
  // (C1) RANDOM COMPUTER MOVE
  var comMove = Math.random();
  if (comMove <= 0.33) { comMove = "rock"; }
  else if (comMove <= 0.67) { comMove = "paper"; }
  else { comMove = "scissors"; }
 
  // (C2) UPDATE COMPUTER MOVE GRAPHIC
  rps.eCom.src = `game-${comMove}.png`;
 
  // (C3) WIN, LOSE, OR DRAW?
  var youMove = rps.eSel.value;
 
  // (C3-1) DRAW
  if (youMove == comMove) {
    rps.draws++; rps.eDraw.innerHTML = rps.draws;
    alert("DRAW");
  } else {
    // (C3-2) CHECK
    let win = true;
    switch (youMove) {
      case "rock":
        if (comMove=="paper") { win = false; }
        break;
      case "paper":
        if (comMove=="scissors") { win = false; }
        break;
      case "scissors":
        if (comMove=="rock") { win = false; }
        break;
    }
 
    // (C3-3) UPDATE SCOREBOARD
    if (win) {
      rps.wins++; rps.eWin.innerHTML = rps.wins;
      alert("YOU WIN");
    } else {
      rps.loses++; rps.eLose.innerHTML = rps.loses;
      alert("YOU LOSE");
    }
  }
}

Finally, rps.game() is called whenever the player hits the “go” button. The computer will choose a rock/paper/scissors randomly, and we determine if it is a win/lose/draw. The end.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

GAME RULES

 

Eh… This is a short section for those who do not know how to play the game or maybe have forgotten how rock, paper, scissors work.

  • It is a simple game for 2 players.
  • Each you and your competitor picks rock, paper, or scissors together at the same time.
  • Scissors will beat paper but lose to rock.
  • Paper will beat rock but loses to scissors.
  • Rock will beat scissors but loses to paper.

If you are interested in the serious history of this game – Check out Rock-Paper-Scissors on Wikipedia.

 

COMPATIBILITY CHECKS

This example will work on all modern “Grade A” browsers.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand Javascript, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *