きのこ遊びクマ衝突キノコ処理
5533 ワード
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<title> </title>
<script type="text/javascript">
var backgroundImg=new Image();
var mushroomImg=new Image();
//
var bearEyesClosedImg=new Image();
var speed=2;//
var horizontalSpeed=speed;//
var verticalSpeed=-speed;//
var bearAngle=2;//
var ctx;
var screenWidth;
var screenHeight;
//
function GameObject(){
this.x=0;
this.y=0;
this.image=null;
}
function Mushroom(){};
Mushroom.prototype=new GameObject();// GameObject
// Amimal GameObject
function Animal(){};
Animal.prototype=new GameObject();
Animal.prototype.angle=0;// , ( )
var animal=new Animal();//
var mushroom=new Mushroom();
function gameLoop(){
//
ctx.clearRect(0, 0, screenWidth, screenHeight);
ctx.save();
//
ctx.drawImage(backgroundImg,0,0);
//
ctx.drawImage(mushroom.image,mushroom.x,mushroom.y);
animal.x+=horizontalSpeed;
animal.y+=verticalSpeed;
animal.angle+=bearAngle;
ctx.translate(animal.x+(animal.image.width/2),animal.y+(animal.image.height/2));// 。
ctx.rotate(animal.angle*Math.PI/180);//
ctx.drawImage(animal.image,-(animal.image.width/2),-(animal.image.height/2));
ctx.restore();
HasAnimalHitEdge();
HasAnimalHitMushroom();
}
function loadImages(){
mushroomImg.src="mushroom.png";
backgroundImg.src="forest1.jpg";
bearEyesClosedImg.src="bear_eyesclosed.png";
mushroom.image=mushroomImg;
animal.image=bearEyesClosedImg;
}
function AddEventHandlers(){
$("#container").mousemove(function(e){
mushroom.x=e.pageX-(mushroom.image.width/2);
});
}
//
function HasAnimalHitEdge(){
//
if(animal.x>screenWidth-animal.image.width){
if(horizontalSpeed>0){
horizontalSpeed=-horizontalSpeed;
}
}
//
if(animal.x<-10){
if(horizontalSpeed<0){
horizontalSpeed=-horizontalSpeed;
}
}
if(animal.y>screenHeight-animal.image.height){
setTimeout(function(){
horizontalSpeed=speed;
verticalSpeed=-speed;
animal.x=parseInt(screenWidth/2);
animal.y=parseInt(screenHeight/2);
gameLoop();
},2000);
}
if(animal.y<0){
verticalSpeed=-verticalSpeed;
}
}
// 2
function CheckIntersect(object1, object2, overlap){
A1=object1.x+overlap;
B1=object1.x+object1.image.width-overlap;
C1=object1.y+overlap;
D1=object1.y+object1.image.height-overlap;
A2=object2.x+overlap;
B2=object2.x+object2.image.width-overlap;
C2=object2.y+overlap;
D2=object2.y+object2.image.height-overlap;
// x-
if(A1>A2&&A1<B2||B1>A2&&B1<B2){
if(C1>C2&&C1<D1||D1>C2&&D1<D2){
return true;
}
}
return false;
}
function HasAnimalHitMushroom(){
//
if(CheckIntersect(animal, mushroom, 5))
{
//
if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.25))
{
horizontalSpeed = -speed;//
}
//
else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.5))
{
//
horizontalSpeed = -speed/2;
}
//
else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.75))
{
horizontalSpeed = speed/2;
}
else
{
horizontalSpeed = speed;
}
verticalSpeed = -speed;// 。
}
}
$(window).ready(function(){
AddEventHandlers();
loadImages();
ctx=document.getElementById("canvas").getContext('2d');;
screenWidth=parseInt($("#canvas").attr("width"));
screenHeight = parseInt($("#canvas").attr("height"));
mushroom.image = mushroomImg;
mushroom.x = parseInt(screenWidth/2);// X
mushroom.y = screenHeight - 40;// Y
animal.x = parseInt(screenWidth/2);// X
animal.y = screenHeight - 40;// Y
setInterval(gameLoop, 10);
});
</script>
</head>
<body>
<div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">
<canvas id="canvas" width="480" height="320" >
</canvas>
</div>
</body>
</html>