cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <canvas id="mycanvas" width="640" height="360" ></canvas>
<h1> Opal</h1>
            
          
!
            
               canvas {
/*   border: 1px solid black; */
  width: 100%;
  box-shadow: 1px 2px 2px #000000;
  
    }
body {
/*   background:linear-gradient(#37F7FF, #3081FF); */
  background: #3081FF;   
}
h1 {
  text-align: center;
}
            
          
!
            
               window.addEventListener("load",function() {
     
      //constants
      var GAME_WIDTH = 640;
      var GAME_HEIGHT = 360;
      const LEFT_ARROW = 37; // use e.which!
      const RIGHT_ARROW = 39; // use e.which!
      //keep the game going
      var gameLive = true;

      //enemies
      var enemies = [
        {
          x: 100, //x coordinate
          y: 100, //y coordinate
          speedY: 2, //speed in Y
          w: 40, //width
          h: 40 //heght
        },
        {
          x: 200,
          y: 0,
          speedY: 2,
          w: 40,
          h: 40
        },
        {
          x: 330,
          y: 100,
          speedY: 3,
          w: 40,
          h: 40
        },
        {
          x: 450,
          y: 100,
          speedY: 5,
          w: 40,
          h: 40
        }
      ];
//seaweed
      var seaweed = [ 
        {
          x: 40, //x coordinate
          y: 130, //y coordinate 
          w: 40, //width
          h: 60 //heght
        },
        {
          x: 130,
          y: 60, 
          w: 40,
          h: 80
        },
        {
          x: 380,
          y: 130, 
          w: 40,
          h: 60
        }           
        ];
       

      /*
      CHALLENGE SOLUTION
      */

      //the player object
      var player = {
        x: 10,
        y: 160,
        speedX: 2.5,
        isMoving: 0,
        w: 40,
        h: 40
      };

      //the goal object
      var goal = {
        x: 580,
        y: 160,
        w: 50,
        h: 36
      }

    

 
      
      
      var sprites = {};

      var movePlayer = function() {
        player.isMoving = true;
      }

      var stopPlayer = function() {
        player.isMoving = false;
      }
      
      //grab the canvas and context
      var canvas = document.getElementById("mycanvas");
      var ctx = canvas.getContext("2d");

      //event listeners to move player
      canvas.addEventListener('mousedown', movePlayer);
      canvas.addEventListener('mouseup', stopPlayer);   
      canvas.addEventListener('touchstart', movePlayer);
      canvas.addEventListener('touchend', stopPlayer);   

      var load = function() {
        sprites.player = new Image();
        sprites.player.src = 'http://i65.tinypic.com/2ajb1aw.jpg';

        sprites.background = new Image();
        sprites.background.src = 'http://i67.tinypic.com/sv6q21.jpg';

        sprites.enemy = new Image();
        sprites.enemy.src = 'http://i63.tinypic.com/2z55dt1.jpg';

        sprites.goal = new Image();
        sprites.goal.src = 'https://static.tumblr.com/ebzd90g/phson9w4m/octomove.png';
        
        sprites.seaweed = new Image();
        sprites.seaweed.src = 'https://static.tumblr.com/ebzd90g/0Dsonas9e/seaweed.png';
        
         
      };

      //update the logic
      var update = function() {

        //check if you've won the game
        if(checkCollision(player, goal)) {
          //stop the game
            gameLive = false;

            alert('You\'ve won!');

            //reload page
            window.location = "";
        }

        //update player
        if(player.isMoving) {
          player.x = player.x + player.speedX;
        }

        //update enemies
        var i = 0;
        var n = enemies.length;
        
        enemies.forEach(function(element, index){

          //check for collision with player
          if(checkCollision(player, element)) {
            //stop the game
            gameLive = false;

            alert('Game Over!');

            //reload page
            window.location = "";
          }

          //move enemy
          element.y += element.speedY;
          
          //check borders
          if(element.y <= 10) {
            element.y = 10;
            //element.speedY = element.speedY * -1;
            element.speedY *= -1;
          }
          else if(element.y >= GAME_HEIGHT - 50) {
            element.y = GAME_HEIGHT - 50;
            element.speedY *= -1;
          }
        });
      };

      //show the game on the screen
      var draw = function() {
        //clear the canvas
        ctx.clearRect(0,0,GAME_WIDTH,GAME_HEIGHT);        

        //draw background
        ctx.drawImage(sprites.background, 0, 0);

        //draw player
        ctx.drawImage(sprites.player, player.x, player.y);

        //draw enemies
        enemies.forEach(function(element, index){
          ctx.drawImage(sprites.enemy, element.x, element.y);
        });

        //draw goal
        ctx.drawImage(sprites.goal, goal.x, goal.y);
        
        // seaweed.forEach(function(element, index){
        // ctx.drawImage(sprites.seaweed, element.x, element.y);
        //   });
      };

      //gets executed multiple times per second
      var step = function() {
  
        update();
        draw();

        if(gameLive) {
          window.requestAnimationFrame(step); 
            
        }     
      };

      //check the collision between two rectangles
      var checkCollision = function(rect1, rect2) {

        var closeOnWidth = Math.abs(rect1.x - rect2.x) <= Math.max(rect1.w, rect2.w);
        var closeOnHeight = Math.abs(rect1.y - rect2.y) <= Math.max(rect1.h, rect2.h);
        return closeOnWidth && closeOnHeight;
      }

      //initial kick
      load();
      step();
    });
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console