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!
TABLE OF CONTENTS
JAVASCRIPT ROCK PAPER SCISSORS
All right, let us now get into more details on how this rock-paper-scissors game works.
ROCK-PAPER-SCISSORS DEMO
YOUR MOVE
COMPUTER’S MOVE
PART 1) HTML GAME INTERFACE
<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.
<div id="rps-you">
Rock-paper-scissor image that the player has chosen.<div id="rps-com">
Rock-paper-scissor image that the computer has chosen.<div id="rps-play">
Chose rock-paper-scissor.<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
// (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
// (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
// (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.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA 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
- Arrow Functions – CanIUse
- Template Literals – CanIUse
This example will work on all modern “Grade A” browsers.
LINKS & REFERENCES
- Javascript BlackJack – Code Boxx
- Javascript MineSweeper – Code Boxx
- Javascript Hangman – Code Boxx
- Javascript Quiz – Code Boxx
- Javascript Memory Game – Code Boxx
- Javascript Number Guessing Game – Code Boxx
- Javascript Tic-Tac-Toe – Code Boxx
- Javascript Rock Paper Scissors Game – Code Boxx
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!