top of page

Game Dev

Dino Run!

<allow script>
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title>Dino Run Javascript Game</title>
   <link rel="stylesheet" href="style.css">
   <style>

       #score {
           text-align: right;
           position: relative;
           top: 5px;
           right: 5px;
           color: red;
       }

       #game {
           width: 600px;
           height: 300px;
           border: 2px solid black;
           margin: auto;
           background-image: url("https://static.wixstatic.com/media/4a98ea_08c59c3e7a3e4ffe8cd9bbf638707397~mv2.jpg");
           background-size: cover;
       }

       #playButton {
           width: 100px;
           height: 100px;
           margin: auto;
           position: relative;
           background-image: url("https://static.wixstatic.com/media/4a98ea_d48c4571eae54d2c8271c0464b872753~mv2.png");
           background-size: cover;
       }

       #dino {
           width: 75px;
           height: 75px;
           position: relative;
           top: 225px;
           left: 10px;
           background-image: url("https://static.wixstatic.com/media/4a98ea_c4bf4095142244c4a5db140bcc36f1d0~mv2.png");
           background-size: cover;
       }

       #rock {
           width: 50px;
           height: 50px;
           position: relative;
           top: 175px;
           left: 550px;
           background-image: url("https://static.wixstatic.com/media/4a98ea_fa03370a535a43a49fe7937681a7d4be~mv2.png");
           background-size: cover;
       }

       .rock-animation {
           animation: rockAnimation 2s;
       }

       @keyframes rockAnimation {
           0%{left: 550px;}
           100%{left: -50px;}
       }

       .jump-animation {
           animation: jump 0.75s;
       }

       @keyframes jump {
           0%{top: 225px;}
           40%{top: 75px;}
           60%{top: 75px;}
           100%{top: 225px;}
       }
   </style>
</head>

<body>
   <div id = "game">
       <div id = "dino"></div>
       <div id = "rock"></div>
       <div id = "playButton"></div>
       <h1 id = "score">0</h1>
   </div>
</body>

<script>
   const dino = document.getElementById('dino');
   const rock = document.getElementById('rock');
   const score = document.getElementById('score');
   const playButton = document.getElementById('playButton');
   let paused = true;

   rock.style.display = 'none';


   function jump() {
       dino.classList.add('jump-animation');
       setTimeout(() => {
           dino.classList.remove('jump-animation');
       }, 750);
   }

   function rockMove() {
       rock.classList.add('rock-animation');
   }


   document.addEventListener('keypress', () => {
       if (!paused) {
           if (!dino.classList.contains('jump-animation')) {
               jump();
           }
       }
   })

   document.addEventListener('click', () => {
       if (!paused) {
           if (!dino.classList.contains('jump-animation')) {
               jump();
           }
       }
   })

   document.addEventListener('click', () => {
       if (paused) {
           paused = false;
           rockMove();
       }
   })

   setInterval(() => {
       if (paused == true)
       {
           playButton.style.display = '';
       }
       else
       {
           playButton.style.display = 'none';
           score.innerText++;
           const dinoTop = parseInt(window.getComputedStyle(dino).getPropertyValue('top'));

           const rockLeft = parseInt(window.getComputedStyle(rock).getPropertyValue('left'));


           if (rockLeft < 0) {
               rock.style.display = 'none';
           } else {
               rock.style.display = '';
           }

           if (rockLeft < 50 && rockLeft > 0) {
               if (dinoTop > 150) {
                   //alert("Your score was: " + score.innerText + "\n\nPlay Again?");
                   location.reload();
               }        
           }
       }
   }, 50)

</script>
</html>

  • Instagram

© 2022 by Bryan Scott Ewing

bottom of page