用HTML 5实现爱心小鱼的游戏

本文介绍了如何使用HTML 5创建一款大鱼吃小鱼的游戏。通过控制大鱼吃掉屏幕上的果实,果实再喂给小鱼。游戏包含HTML、CSS和JavaScript代码,展示了如何实现游戏的基本功能,如鼠标控制、碰撞检测和游戏得分。
摘要由CSDN通过智能技术生成

本游戏的示例来源于果壳网,用鼠标可以控制大鱼吃果实,然后用大鱼将果实喂养给小鱼。

演示效果如下:


 HTML代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tinyHeart</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
    <div class="all_bg">
    <div id="allcanvas">
    <canvas id="canvas1" width="800" height="600"></canvas>
    <canvas id="canvas2" width="800" height="600"></canvas>
    </div>
    </div>
    <script type="text/javascript" src="js/commonFunctions.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>


JS代码如下:

var can1=document.getElementById('canvas1');   //fish, dust, UI, circle
var can2=document.getElementById('canvas2');   //background, ane, fruits


var ctx1=can1.getContext("2d");
var ctx2=can2.getContext("2d");




var lastTime=Date.now();
var deltaTime=0;


var can1Width=can1.width;
var can1Height=can1.height;




var bgPic = new Image();
bgPic.src="images/background.jpg";


function drawBackground(){
ctx2.drawImage(bgPic,0,0,can1Width,can1Height);
}


var mx= can1Width *0.5;
var my= can1Height *0.5;
can1.addEventListener("mousemove",onMouseMove,false);
function onMouseMove(e){
if(!data.gameOver){
if(e.offsetX){
mx=e.offsetX;
my=e.offsetY;
}
}
}


function momFruitsCollision(){
if(!data.gameOver){
for(var i=0;i<fruit.num;i++){
if(fruit.alive[i]){
var l=calLength2(fruit.x[i],fruit.y[i], mom.x, mom.y);
if(l<900){
fruit.dead(i);
data.fruitNum++;
mom.momBodyCount++;
if(mom.momBodyCount>7){
mom.momBodyCount=7;
}
if(fruit.fruitType[i]=="blue"){
data.double=2;
}
wave.born(fruit.x[i],fruit.y[i]);
}
}
}
}
}


function momBabyCollision(){
if(data.fruitNum>0&&!data.gameOver){
var l=calLength2(mom.x,mom.y, baby.x, baby.y);
if(l<900){
baby.babyBodyCount=0;
mom.momBodyCount=0;
data.addScore();
}
}
}


var babyTail=[];
for(var i=0;i<8;i++){
babyTail[i]=new Image();
babyTail[i].src="images/babyTail"+i+".png";
}
var babyEye=[];
for(var i=0;i<2;i++){
babyEye[i]=new Image();
babyEye[i].src="images/babyEye"+i+".png";
}
var babyBody=[];
for(var i=0;i<20;i++){
babyBody[i]=new Image();
babyBody[i].src="images/babyFade"+i+".png";
}


var momTail=[];
for(var i=0;i<8;i++){
momTail[i]=new Image();
momTail[i].src="images/bigTail"+i+".png";
}
var momEye=[];
for(var i=0;i<2;i++){
momEye[i]=new Image();
momEye[i].src="images/bigEye"+i+".png";
}
var momBodyOra=[];
var momBodyBlue=[];
for(var i=0;i<8;i++){
momBodyOra[i]=new Image();
momBodyOra[i].src="images/bigSwim"+i+".png";
momBodyBlue[i]=new Image();
momBodyBlue[i].src="images/bigSwimBlue"+i+".png";
}


var dustPic=[];
for(var i=0;i<7;i++){
dustPic[i]=new Image();
dustPic[i].src="images/dust"+i+".png";
}




//ane
var aneObj=function(){
this.rootx=[];
this.headx=[];
this.heady=[];
this.amp=[];
this.alpha=0;
}
aneObj.prototype.num=50;
aneObj.prototype.init=function(){
var h=can1.height;
for(var i=0;i<this.num;i++){
this.rootx[i]=i*16 + Math.random() * 20;
this.headx[i]=this.rootx[i];
this.heady[i]=h-250+Math.random() * 50;
this.amp[i]=50+Math.random() * 50;
}
}
aneObj.prototype.draw=function(){
this.alpha+=deltaTime *0.0008;
var l=Math.sin(this.alpha);
ctx2.save();
ctx2.globalAlpha=0.6;
ctx2.lineCap= "round";
ctx2.strokeStyle= "#3b154e";
ctx2.lineWidth=20;
for(var i=0;i<this.num;i++){
ctx2.beginPath();
ctx2.moveTo(this.rootx[i],can1Height);
ctx2.quadraticCurveTo(this.rootx[i],can1Height-100,this.headx[i]+l*this.amp[i],this.heady[i]);
ctx2.stroke();
ctx2.closePath();
}
ctx2.restore();
}


var ane=new aneObj();
ane.init();


//fruit
var fruitObj=function(){
this.alive=[];
this.x=[];
this.y=[];
this.l=[];
this.spd=[];
this.fruitType=[];
this.orange =new Image();
this.blue =new Image();
}
fruitObj.prototype.num= 30;
fruitObj.prototype.init= function(){
for(var i=0;i<this.num;i++){
this.alive[i]=false;
this.spd[i]=Math.random() *0.017 + 0.003;
}
this.orange.src="images/fruit.png";
this.blue.src="images/blue.png";
}
fruitObj.prototype.draw=function(){
for(var i=0;i<this.num;i++){
if(this.alive[i]){
if(this.fruitType[i]=="blue"){
var pic=this.blue;
}else{
var pic=this.orange;
}
if(this.l[i]<=14){
this.l[i]+=this.spd[i] * deltaTime;
}else{
this.y[i]-=this.spd[i] *7 *deltaTime;
}
ctx2.drawImage(pic, this.x[i] -this.l[i] *0.5,
this.y[i] - this.l[i] *0.5, this.l[i] , this.l[i]);
if(this.y[i]<10){
this.alive[i] =false;
}
}
}
}
fruitObj.prototype.born=function(i){
var aneID=Math.floor(Math.random() * ane.num);
this.alive[i]=true;
this.l[i]=0;
this.x[i]=ane.headx[aneID];
this.y[i]=ane.heady[aneID];
var ran=Math.random();
if(ran<0.2){
this.fruitType[i]="blue";
}else{
this.fruitType[i]="orange";
}

}
fruitObj.prototype.dead=function(i){
this.alive[i]=false;
}
function fruitMonitor(){
var num=0;
for(var i=0;i<fruit.num;i++){
if(fruit.alive[i]) num++;
}
if(num<15){
sendFruit();
return;
}
}
function sendFruit(){
for(var i=0;i<fruit.num;i++){
if(!fruit.alive[i]){
fruit.born(i);
return;
}
}
}
var fruit=new fruitObj();
fruit.init();


//mom
var momObj=function(){
this.x;
this.y;
this.angle;
this.bigEye=new Image();
this.bigTail=new Image();
this.momBodyCount=0;
}
momObj.prototype.init = function(){
this.x=can1Width *0.5;
this.y=can1Height *0.5;
this.angle=0;
this.bigEye.src="images/bigEye0.png";
this.bigTail.src="images/bigTail0.png";
}
momObj.prototype.draw=function(){
this.x=lerpDistance(mx, this.x, 0.96);
this.y=lerpDistance(my, this.y, 0.96);
var deltaY=my-this.y;
var deltaX=mx-this.x;
var beta = Math.atan2(deltaY,deltaX)+Math.PI;
this.angle=lerpAngle(beta, this.angle, 0.6);


ctx1.save();
ctx1.translate(this.x,this.y);
ctx1.rotate(this.angle);
ctx1.drawImage(this.bigTail,-this.bigTail.width*0.5+30,-this.bigTail.height*0.5);
var momBodyCount=this.momBodyCount;
if(data.double==1){
ctx1.drawImage(momBodyOra[momBodyCount],-momBodyOra[momBodyCount].width*0.5,-momBodyOra[momBodyCount].height*0.5);
}else{
ctx1.drawImage(momBodyBlue[momBodyCount],-momBodyBlue[momBodyCount].width*0.5,-momBodyBlue[momBodyCount].height*0.5);
}
ctx1.drawImage(this.bigEye,-this.bigEye.width*0.5,-this.bigEye.height*0.5);
ctx1.restore();
}
var mom=new momObj();
mom.init();


//baby
var babyObj=function(){
this.x;
this.y;
this.angle;
this.babyEye=new Image();
this.babyBody=new Image();
this.babyTail=new Image();
this.babyTailTimer= 0;
this.babyTailCount= 0;


this.babyEyeTimer=0;
this.babyEyeCount=0;
this.babyEyeInterval =1000;


this.babyBodyTimer= 0;
this.babyBodyCount= 0;
}
babyObj.prototype.init=function(){
this.x=can1Width * 0.5-50;
this.y=can1Height * 0.5+50;
this.angle=0;
this.babyBody.src="images/babyFade0.png";
}
babyObj.prototype.draw=function(){
this.x=lerpDistance(mom.x, this.x, 0.996);
this.y=lerpDistance(mom.y, this.y, 0.996);
var deltaY=mom.y-this.y;
var deltaX=mom.x-this.x;
var beta = Math.atan2(deltaY,deltaX) +Math.PI;
this.angle=lerpAngle(beta, this.angle, 0.6);


this.babyTailTimer +=deltaTime;
if(this.babyTailTimer>50){
this.babyTailCount=(this.babyTailCount+1) % 8;
this.babyTailTimer %= 50;
}




this.babyEyeTimer +=deltaTime;
if(this.babyEyeTimer>this.babyEyeInterval){
this.babyEyeCount=(this.babyEyeCount+1) % 2;
this.babyEyeTimer %= this.babyEyeInterval;
if(this.babyEyeCount==0){
this.babyEyeInterval= Math.random() * 1500 + 2000;
}else{
this.babyEyeInterval=200;
}
}


this.babyBodyTimer +=deltaTime;
if(this.babyBodyTimer>300){
this.babyBodyCount=this.babyBodyCount+1;
this.babyBodyTimer %=300;
if(this.babyBodyCount>19){
this.babyBodyCount=19;
data.gameOver=true;
}
}




ctx1.save();
ctx1.translate(this.x,this.y);
ctx1.rotate(this.angle);
var babyTailCount=this.babyTailCount;
ctx1.drawImage(babyTail[babyTailCount],-babyTail[babyTailCount].width*0.5+23,-babyTail[babyTailCount].height*0.5);
var babyBodyCount=this.babyBodyCount;
ctx1.drawImage(babyBody[babyBodyCount],-babyBody[babyBodyCount].width*0.5,-babyBody[babyBodyCount].height*0.5);
var babyEyeCount=this.babyEyeCount;
ctx1.drawImage(babyEye[babyEyeCount],-babyEye[babyEyeCount].width*0.5,-babyEye[babyEyeCount].height*0.5);


ctx1.restore();
}
var baby=new babyObj();
baby.init();


//data
var dataObj=function(){
this.fruitNum=0;
this.double=1;
this.score=0;
this.gameOver=false;
this.Alpha=0;
}
dataObj.prototype.reset=function(){
this.double=1;
}
dataObj.prototype.draw=function(){
var w=can1.width;
var h=can1.height;
ctx1.fillStyle="white";
ctx1.font="25px Verdana";
ctx1.textAlign="center";
ctx1.fillText("SCORE:"+this.score,w * 0.5,h-20);
if(this.gameOver){
this.Alpha+=deltaTime *0.0005;
if(this.Alpha>1)
this.Alpha=1;
ctx1.fillStyle="rgba(255,255,255,"+this.Alpha+")";
ctx1.fillText("GAMEOVER",w * 0.5,h * 0.5);
}
}
dataObj.prototype.addScore=function(){
this.score+=this.fruitNum*100*this.double;
this.fruitNum=0;
this.double=1;
}
var data=new dataObj();


//wave
var waveObj=function(){
this.x=[];
this.y=[];
this.alive=[];
this.r=[];
}
waveObj.prototype.num=10;
waveObj.prototype.init=function(){
for (var i = 0; i < this.num; i++) {
this.alive[i]=false;
this.r[i]=0;
}
}
waveObj.prototype.draw=function(){
for (var i = 0; i < this.num; i++) {
if(this.alive[i]){
this.r[i]+=deltaTime *0.04;
if(this.r[i]>50)
this.alive[i]=false;
var alpha=1-this.r[i] / 100;
ctx1.beginPath();
ctx1.arc(this.x[i], this.y[i], this.r[i], 0,Math.PI*2);
ctx1.closePath();
ctx1.strokeStyle = 'rgba(255,255,255,' + alpha + ')';
ctx1.stroke();
}
}
}
waveObj.prototype.born=function(x,y){
for (var i = 0; i < this.num; i++) {
if(!this.alive[i]){
this.alive[i]=true;
this.r[i]=10;
this.x[i]=x;
this.y[i]=y;
               return;
}
}
}
var wave=new waveObj();
wave.init();


//dust
var dustObj=function(){
this.x=[];
this.y=[];
this.amp=[];
this.NO=[];
this.alpha;
}
dustObj.prototype.num=30;
dustObj.prototype.init=function(){
for (var i = 0; i < this.num; i++) {
this.x[i]=Math.random()*can1Width;
this.y[i]=Math.random()*can1Height;
this.amp[i]=20+Math.random()*15;
this.NO[i]=Math.floor(Math.random()*7);
}
this.alpha=0;
}
dustObj.prototype.draw=function(){
this.alpha+=deltaTime*0.0008;
var l=Math.sin(this.alpha);
for (var i = 0; i < this.num; i++) {
var no=this.NO[i];
ctx1.drawImage(dustPic[no],this.x[i]+this.amp[i]*l,this.y[i]);
}
}
var dust=new dustObj();
dust.init();




gameloop();


function gameloop(){
window.requestAnimFrame(gameloop);
var now=Date.now();
deltaTime=now-lastTime;
lastTime=now;
if(deltaTime>40) deltaTime=40;
drawBackground();
ane.draw();
fruitMonitor();
fruit.draw();


ctx1.clearRect(0,0,can1Width,can1Height);
mom.draw();
momFruitsCollision();
baby.draw();
momBabyCollision();
data.draw();
wave.draw();
dust.draw();
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值