A JavaScript based Spock Rock Game feat. Confetti.js, Modules
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
Rock Paper Scissors Lizard Spock is an extension of the classic game of chance, Rock Paper Scissors, created by Sam Kass and Karen Bryla. Sam Kass explains he created the expanded game because it seemed like most games of Rock Paper Scissors with people you know would end in a tie. Rock Paper Scissors Lizard Spock was first mentioned in the Season 2 episode, The Lizard-Spock Expansion, the title of which references the game.
There is also a Confetti falling animation across the screen over the win. Just play & enjoy! 😄
This is an example of how you may start on setting up your project locally. To get a local copy up and running follow these simple example steps.
This is an example of how to list things you need to use the software and how to install them.
- First of all install
Live Server
extension byRitwick Dey
. - Once the extension was installed, then
right-click
onindex.html
and selectOpen with Live Server
. - It'll open a new tab in browser & start serving the game. Enjoy!.
- First of all, we need to gather all
functional requirements
of the game like: -
Functional Requirements:
- Two players play the game by each picking a symbol randomly: the selected symbols determine who the winner is.
-
The allowed moves are Paper, Rock, Scissors, Lizard, Spock and they interact as follows: “Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors, scissors decapitates lizard, lizard eats paper, paper disproves Spock, Spock vaporizes rock, rock crushes scissors.”
- If both players pick the same symbol, your application will not select a winner and declare the game a tie
- Then break the whole game into small
Ui Components
& start building indivisual components.
Ui Components:
-
Game Board
- The whole game'll be inside of a Game-Board which receives certain width on different viewports.
- There'll be two players: User & Computer.
- The icons are outsourced from FontAwesome's regular icon collection & Each icon represents a player's move
- The player container divided into two sections - one for User & one for computer.
- Each section'll contain "player-name" followed by "score" and then "name-of-the-choice" the player selects.
- Next, all the "move-icons" sits in line.
-
Player's Move Icon
- For user's selection, when user click on an icon, it's color turns into black & updates the "name-of-the-choice" portion from "choice" (by-default) to the selected one.
- For computer's choice, generate a number between 0 & 1 and divided whole (0, 1) interval into 5 parts - each of width 0.2. According as where does the generated number lie, we assign one from 'rock', 'paper', 'scissors', 'lizard', 'spock' respectively.
- Once computer chooses it's move, the respected icon's color on the game board turns into black & updates the "name-of-the-choice" portion from "choice" (by-default) to the selected one.
- Before allowing user to click again, the game board automatically reset previous choices of both player and computer.
- Here "Switch Statement" is used to implement such feature in both cases.
-
Determining Winner
- First of all, it's very crucial to choose a right data structure to hold choices:
- Corresponding to the player's choice, if defeats array inside of that choice contains Computer's choice, then player wins. Otherwise Computer wins.
- Finally, update the score of both player & computer in the game board.
-
Confetti falling animation over Win
- Here we used confetti.js, a vanilla JS library for creating a configurable, high-performance confetti falling animation using HTML5 canvas and requestAnimFrame API.
- The confetti.js is modified little bit to bring the code under anonymous function into global scope. So that these three functions: 'startConfetti', 'stopConfetti', 'removeConfetti' become globally available.
- The good news is that modern browsers have started to support module functionality natively. This can only be a good thing — browsers can optimize loading of modules, making it more efficient than having to use a library and do all of that extra client-side processing and extra round trips.
- In order to do that, firstly we have to export the above three specified functions & import them inside main script file. Then execute main script file as the top-level module:
- But type="module" makes main script self-contained. That means the main script no longer shares in global scope with the elements in index.html
- To make things globally available, pass it into Window object like this:
To see in action, please visit to the Link
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Prasenjit Sutradhar - @twitter_handle - prasenjitsutradhar3433@gmail.com
Project Link: https://github.com/Prasenjit-3433/Spock-Rock-Game