Jump to content

catch game code


phpuserlogin
 Share

Recommended Posts

<!DOCTYPE html>
<div style='background-color:gold; width:640px;'>
<canvas id="gameCanvas" width="640" height="480"></canvas>
</div>
<div style='display:none;'>
<img id='dog' src='smuffy.png'>
<img id='donut' src='donut.png'>
</div>
<script>
   var ctx=gameCanvas.getContext("2d");
   var x=[100,300,500];
   var y=[0,0,0];	 
   var speed=[2,1,3];
   var dogX=300, changeX=0, score=0;

   var gameTimer=setInterval(mainloop,20);
   function mainloop() {
     ctx.clearRect(0,0,640,480);
	ctx.font="30px Arial";
	ctx.fillText("Score: "+score,10,30);
        for(var n=0; n<3; n++){
	ctx.drawImage(donut,x[n],y[n],50,50);
	y[n]+=speed[n];
	checkForHits();
	if(y[n]>400){
	y[n]=-80; x[n]=Math.random()*600;
 }

}
ctx.drawImage(dog,dogX,400,60,60);
dogX+=changeX;
}
    document.onkeydown=keyPressed;
    function keyPressed(e){
    var k=e.keyCode;
    if(k==37){changeX=-4;}
    if(k==39){changeX=4;}
}

Does this code work?

function checkForHits(n) {
   if((Math.abs(400-y[n])<60)&&
   (Math.abs(dogX-x[n])<60)){
   score+=1;
   y[n]=-80; x[n]=Math.random()*600;
   }
}
</script>
</html>

 

Thanks

donut.png

smuffy.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...