JSFiddle - React, Tailwind, and code Playground

by Gurjit singh

HTML

<!--  THis div acts as a splashScreen  -->
<div id="splashScreen">
    <h1> Press <span>S</span> to play game</h1>
    <h1>Press <span>H</span> for scores list</h1>
    
</div>

<!--  This div will ask the person name and display it on the canvas element  -->
<div id="askName">
    <div id="textSetting">
    Enter Player Name :
    <input type="text" id="playerName" name="playerName"><br/>
    <input type="button" id="playerNameBtn" name="playerNameBtn" value="Enter">
    </div>    
</div>
    
<!--  This div is Canvas element  -->
<canvas id="gameCanvas" width="500" height="500">Your browser doesnot support canvas</canvas>
    
<!-- This div is for displaying the data   -->
<div id="highScores" > 
    <table id="scoresTable">
        <tr>
            <th>S.no</th>
            <th>Player Name</th>
            <th>Player Scores</th>
        </tr>
    </table>
</div>

<div id="moveBack">
    <== Go to main menu
</div>

CSS

h1 {
            margin : 0;
            padding: 0;
        }
    	#gameCanvas{
    		background:#FCF59B;
    		width: 500px;
            height: 500px;
            display: none;
            margin: 2% auto;
            opacity: 0;
    	}

        #splashScreen {
            position: absolute;
            background:#e3e3e3;
            width: 500px;
            height: 500px;
            display: block;
            margin: auto;
            left: 31.7%;
            border: 4px solid #1152f1;
            /*transition: all 1s ease-in-out;*/
        }

        #splashScreen h1 {
            text-align: center;
            line-height: 400px;
            height: 60px;
        }

        #splashScreen h1 span {
            text-transform: uppercase;
            position: relative;
            top: 11px;
            font-size: 60px;
            font-weight: bold;
            font-family: monospace;
            -webkit-text-stroke: 4px black;
            -moz-text-stroke: 4px black;
            text-stroke: 4px #1152f1;
            color: #1152f1;
        }
        
        #askName {
            position: absolute;
            width: 500px;
            height: 100px;
            background-color: rgba(0,0,0,.2);
            left: 31.4%;
            font-family: monospace;
            font-feature-settings: normal;
            font-size: 16px;
            letter-spacing: 1px;
            -webkit-transform: scale(1,0);
            -moz-transform: scale(1,0);
            transform: scale(1,0);
            transition: -webkit-transform .5s linear;
        }
        
        #textSetting {
            position: relative;
            left: 5%;
            top: 20%;
        }
        
        #textSetting input[type = text] {
            padding: 5px;
            font-size: 14px;
            font-family: monospace;
            letter-spacing: 1px;
            font-weight: bolder;
        }
        
        #playerNameBtn {
            position: relative;
           ...

JavaScript

//window.localStorage.clear();
var splashScreen = document.getElementById('splashScreen');
var gameCanvas = document.getElementById('gameCanvas');
var askName = document.getElementById('askName');
var playerName = document.getElementById('playerName');
var playerNameBtn = document.getElementById('playerNameBtn');
var highScores = document.getElementById('highScores');
var moveBack = document.getElementById('moveBack');
var nameOfPlayer = "Anonymous";
var jsonData = {};
var jsObjArray = {};

////// THis is the way to create sound on the collisions /////////
var collision = new Audio();
collision.src = "collide.mp3";


window.addEventListener('keypress',startScreen,false);


function startScreen(e) {
        switch(e.keyCode || e.charCode) { ///// code for s to start the game /////////
        //alert("Hi");
        case 115:   /////////In case of S letter     
            splashScreen.style.WebkitAnimation = "splashed 1s linear forwards";
            splashScreen.style.MozAnimation = "splashed 1s linear forwards";
            //splashScreen.style.Animation = "splashed 1s linear forwards";
            gameCanvas.style.display = "block";
            askName.style.zIndex = "2";
            askName.style.WebkitAnimation = "moveup .5s linear .6s forwards";
            askName.style.MozAnimation = "moveup .5s linear .6s forwards";
            gameCanvas.style.WebkitAnimation = "gameOn .5s linear .6s forwards";
            gameCanvas.style.MozAnimation = "gameOn .5s linear .6s forwards";
            break;
        
        case 104: /////////// In case of H letter
            splashScreen.style.WebkitAnimation = "splashed .3s linear forwards";
            splashScreen.style.MozAnimation = "splashed .3s linear forwards";

//            //alert("Control moving to the displayScores");
                displayScores();
            break;    
    }
}

moveBack.addEventListener('click',function(){
    highScores.style.display = "none";
    moveBack.style.display = "none";
   ...