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.

            
              <button id="left">left</button>
<button id="right">right</button>
<button id="startrotate">start</button>
<button id="stoprotate">stop</button>

 


<div id="main"></div>
  <div id="player"></div>
  <div id="food"></div>
</div>
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
<!--<script src="js/jquery-rotate/rotate.js"></script>
<script src="jquery-css-transform.js" type="text/javascript"></script>
<script src="jquery-animate-css-rotate-scale.js" type="text/javascript"></script>-->
            
          
!
            
              #player {
  position:absolute;
  background-color:#add8e6;
  left:50px;
  width:150px;
  height:150px;
  margin:5px;
  border-radius:250px;
  transition:all .3s;
  border-style:dotted;
  border:4px solid #2c3888 dotted;
  -webkit-animation: rotation 2s linear infinite;
    -moz-animation: rotation 2s linear infinite;
    -ms-animation: rotation 2s linear infinite;
  
}

@keyframes rotate360 {
  to { transform: rotate(360deg); }
}
 #player{ animation: 2s rotate360 infinite linear; }

#food {
  position:absolute;
  background-color:#2c3888;
  left:250px;
  width:150px;
  height:150px;
  margin:5px;
  border:1px solid #fff;
}
#main{
  margin-top:55px;  
}
body{  
  background:#000;
}
            
          
!
            
               $("#player").css("-webkit-animation-play-state", "paused");

$.noConflict();

$("#startrotate").click(function(){
  
   $("#player").css("-webkit-animation-play-state", "running", checkCollisions);
  
   });
  

$("#stoprotate").click(function(){
  $("#player").stop(true);
   $("#player").css("-webkit-animation-play-state", "paused");
   });

$("#right").click(function(){
  $("#player").animate({"left": "+=50px"}, "fast", checkCollisions);
  
   });

$("#left").click(function(){
  $("#player").animate({"left": "-=50px"}, "fast", checkCollisions);
  
});



function getPositions(player) {
  var $player = $(player);
  var pos = $player.position();
  var width = $player.width();
  var height = $player.height();
  return [ [ pos.left, pos.left + width ], [ pos.top, pos.top + height ] ];
}
        
function comparePositions(p1, p2) {
  var x1 = p1[0] < p2[0] ? p1 : p2;
  var x2 = p1[0] < p2[0] ? p2 : p1;
  return x1[1] > x2[0] || x1[0] === x2[0] ? true : false;
}

function checkCollisions(){
  var box = $("#food")[0];
  var pos = getPositions(box);

  var pos2 = getPositions(this);
  var horizontalMatch = comparePositions(pos[0], pos2[0]);
  var verticalMatch = comparePositions(pos[1], pos2[1]);            
  var match = horizontalMatch && verticalMatch;
 
  if (match) { 
    $("#food").css("display","none");
  }
  else{
    $("#food").css("display","block");
  }
}
            
          
!
999px
Loading ..................

Console