图片只能打包上传
百度云链接:https://pan.baidu.com/s/1bxHZYtsZxTzPFis4Fq4AvA 密码:xshc
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>飞机大战</title>
<style type="text/css">
canvas{
display: block;
margin: 150px auto;
cursor: none;
}
</style>
</head>
<body>
<!--创建基本的canvas及基本属性-->
<canvas id="canvas" width="480" height="600"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
// 创建基本的canvas及基本属性 以上
// 0,游戏初始化
// 0.1定义游戏的几个状态/阶段
var START = 0; //游戏开始阶段(欢迎界面)
var STARTING = 1; //游戏运行前阶段(加载界面)
var RUNNING = 2; //游戏运行中阶段(游戏中)
var PAUSE = 3; //游戏暂停阶段(游戏暂停)
var GAMEOVER = 4; //游戏结束阶段(死亡时刻)
// 0.2定义一个状态,与上面定义的状态对比,确定当前是处于哪个阶段
var status = START;
// 0.3定义当前画布宽高
var w = canvas.width;
var h = canvas.height;
// 0.4定义分数
var socre = 0;
// 0.5定义我方飞机血量
var HP = 50;
// 1游戏开始阶段
// 1.1加载背景图片
// 1.1.1创建背景图片对象
var bg = new Image();
bg.src = "img/background.png";
// 1.1.2初始化背景图片数据
var BG = {
imgs : bg,
width : 480,
height : 852,
}
// 1.1.3创建背景图片的构造器函数(构造对象)
function Bg(imagess){
this.imgs = imagess.imgs;
this.width = imagess.width;
this.height = imagess.height;
this.x1 = 0;
this.y1 = 0;
this.x2 = 0;
this.y2 = -this.height;
// 绘制背景图片 方法
this.paint = function(){
context.drawImage(this.imgs,this.x1,this.y1);
context.drawImage(this.imgs,this.x2,this.y2);
}
// 背景图片运动方法
this.imgMove = function(){
this.y1++;
this.y2++
// 判断图片临界点
if(this.y1 == this.height){
this.y1 = -this.height
}
if(this.y2 == this.height){
this.y2 = -this.height;
}
}
}
// 1.1.4创建背景图片对象
var back = new Bg(BG);
// 1.2绘制游戏名
var logo = new Image();
logo.src = "img/start.png";
// 2游戏运行前阶段
// 2.1绘制正在加载游戏的图片
var loadding = [];
loadding[0] = new Image();
loadding[0].src = "img/game_loading1.png";
loadding[1] = new Image();
loadding[1].src = "img/game_loading2.png";
loadding[2] = new Image();
loadding[2].src = "img/game_loading3.png";
loadding[3] = new Image();
loadding[3].src = "img/game_loading4.png";
// 2.2初始化图片的数据
var LOADING = {
imgs : loadding,
length : loadding.length,
width: 186,
height : 38,
}
// 2.3定义开始前动画构造器
function loading(imagess){
this.imgs = imagess.imgs;
this.length = imagess.length;
this.width = imagess.width;
this.height = imagess.height;
// 定义索引,判断对应图片
this.goIndex = 0;
// 定义绘制方法
this.paint = function(){
// goIndex++
context.drawImage(this.imgs[this.goIndex],0,h-this.height);
}
// 定义运动方法
this.time = 0;
this.imgMove = function(){
this.time++;
if(this.time%100 ==0){
this.goIndex++;
}
if(this.goIndex == this.length){ //索引等于长度,进入下一阶段
status = RUNNING;
}
}
}
// 创建游戏运行前的动画对象
var loading = new loading(LOADING);
// 第一阶段时,点击屏幕进入第二阶段
canvas.onclick = function(){
if(status == START){
status = STARTING;
}
}
// 3游戏运行阶段
// 3.1绘制我方飞机
// 3.1.1加载我方飞机图片
var hero = [];
hero[0] = new Image();
hero[0].src = "img/hero1.png";
hero[1] = new Image();
hero[1].src = "img/hero2.png";
hero[2] = new Image();
hero[2].src = "img/hero_blowup_n1.png";
hero[3] = new Image();
hero[3].src = "img/hero_blowup_n2.png";
hero[4] = new Image();
hero[4].src = "img/hero_blowup_n3.png";
hero[5] = new Image();
hero[5].src = "img/hero_blowup_n4.png";
// 3.1.2初始化我方飞机数据
var HERO = {
imgs : hero,
length : hero.length,
width : 99,
height : 124,
mark : 2, //飞机的两种状态
}
// 3.1.3我方飞机构造器
function Hero(imagess){
this.imgs = HERO.imgs;
this.length = HERO.length;
this.width = HERO.width;
this.height = HERO.height;
this.mark = HERO.mark;
// 定义索引,判断对应图片
this.goIndex = 0;
// 定义飞机位置
this.x = w/2 - this.width/2;
this.y = h/2 - this.height/2;
// 定义碰撞属性
this.on = false;
// 定义撞悔动画是否完成
this.off = false;
//定义绘制方法
this.paint = function(){
context.drawImage(this.imgs[this.goIndex],this.x,this.y);
}
// 定义运动方法
this.imgMove = function(){
// 两个,一个为碰撞时的状态
// 一个为正常移动状态
if(!this.on){ //正常移动
if(this.goIndex == 0){
this.goIndex = 1;
}else{
this.goIndex = 0;
}
}else{ //碰撞以后
this.goIndex++
if(this.goIndex == this.length){
HP--;
if(HP == 0){
status = GAMEOVER;
this.goIndex = this.length-1;
}else{
heros = new Hero(HERO);
}
}
}
}
// 飞机的碰撞方法
this.flow = function(){
this.on = true;
}
// 飞机射击方法
this.time = 0;
this.shoot = function(){
this.time++;
if(this.time % 2 == 0){
bullets.push(new Bullet(BULLET));
}
}
}
// 3.1.4创建我方飞机对象
var heros = new Hero(HERO);
// 3.1.5增加移动事件
canvas.onmousemove = function(event){
var event = event || window.event;
if(status == RUNNING){
heros.x = event.offsetX - heros.width/2;
heros.y = event.offsetY - heros.height/2;
}
}
// 3.2绘制子弹
// 3.2.1加载子弹图片
var bullet = new Image();
bullet.src = "img/bullet1.png";
// 3.2.2创建子弹对象
var BULLET = {
imgs : bullet,
width : 9,
height : 21,
}
// 3.2.3创建子弹构造器
function Bullet(imagess){
this.imgs = imagess.imgs;
this.width = imagess.width;
this.height = imagess.height;
// 子弹坐标
this.x = heros.x + heros.width/2 - this.width/2;
this.y = heros.y - this.height;
// 绘制子弹
this.paint = function(){
context.drawImage(this.imgs,this.x,this.y);
}
// 运动方法
this.imgMove = function(){
this.y -=15;
}
// 碰撞属性
this.on = false;
// 碰撞方法
this.flow = function(){
this.on = true;
}
}
// 3.2.4所有new出来的子弹放到一个数组中去
var bullets = [];
// 3.2.5遍历所有子弹,然后绘制
function bulletsPaint(){
for(var i = 0;i < bullets.length;i++){
bullets[i].paint();
}
}
// 3.2.6绘制所有子弹运动方法
function bulletsImgMove(){
for(var j = 0;j < bullets.length;j++){
bullets[j].imgMove();
}
}
// 3.2.7删除子弹
function myDel(){
// 两种状态:碰到敌方飞机或者超出画布
for(var k = 0;k < bullets.length;k++)
if(bullets[k].on || bullets[k].y < -bullets[k].height){
bullets.splice(k,1); //参数1选位置,参数2删除几个
}
}
// 3.3绘制地方飞机
// 3.3.1加载敌方飞机图片
var enemyl = []; //小飞机
enemyl[0] = new Image();
enemyl[0].src = "img/enemy1.png";
enemyl[1] = new Image();
enemyl[1].src = "img/enemy1_down1.png";
enemyl[2] = new Image();
enemyl[2].src = "img/enemy1_down2.png";
enemyl[3] = new Image();
enemyl[3].src = "img/enemy1_down3.png";
enemyl[4] = new Image();
enemyl[4].src = "img/enemy1_down4.png";
var enemym = []; //中飞机
enemym[0] = new Image();
enemym[0].src = "img/enemy2.png";
enemym[1] = new Image();
enemym[1].src = "img/enemy2_down1.png";
enemym[2] = new Image();
enemym[2].src = "img/enemy2_down1.png";
enemym[3] = new Image();
enemym[3].src = "img/enemy2_down1.png";
enemym[4] = new Image();
enemym[4].src = "img/enemy2_down1.png";
var enemyb =[]; //大飞机
enemyb[0] = new Image();
enemyb[0].src = "img/enemy3_n1.png";
enemyb[1] = new Image();
enemyb[1].src = "img/enemy3_n2.png";
enemyb[2] = new Image();
enemyb[2].src = "img/enemy3_down1.png";
enemyb[3] = new Image();
enemyb[3].src = "img/enemy3_down2.png";
enemyb[4] = new Image();
enemyb[4].src = "img/enemy3_down3.png";
enemyb[5] = new Image();
enemyb[5].src = "img/enemy3_down4.png";
enemyb[6] = new Image();
enemyb[6].src = "img/enemy3_down5.png";
enemyb[7] = new Image();
enemyb[7].src = "img/enemy3_down6.png";
// 3.3.2初始化敌方飞机数据
var ENEMYL = {
imgs : enemyl,
length : enemyl.length,
width : 57,
height : 51,
mark : 1,
HP : 1,
socre : 1,
type : 1,
}
var ENEMYM = {
imgs : enemym,
length : enemym.length,
width : 69,
height : 95,
mark : 1,
HP : 5,
socre : 10,
type : 2,
}
var ENEMYB = {
imgs : enemym,
length : enemym.length,
width : 166,
height : 261,
mark : 2,
HP : 10,
socre : 50,
type : 2,
}
// 3.3.3敌方飞机构造
function Enemy(imagess){
this.imgs = imagess.imgs;
this.length = imagess.length;
this.width = imagess.width;
this.height = imagess.height;
this.mark = imagess.mark;
this.HP = imagess.HP;
this.socre = imagess.socre;
this.type = imagess.type;
// 坐标
this.x = Math.random()*(w - this.width);
this.y = -this.height;
// 索引
this.goIndex = 0;
// 碰撞属性
this.on = false;
// 击毁动画是否完成
this.off = false;
// 绘制方法
this.paint = function(){
context.drawImage(this.imgs[this.goIndex],this.x,this.y);
}
// 移动方法
this.imgMove = function(){
if(!this.on){
//正常情况,大飞机在两个图片中来回转换,其他飞机图片不变
this.goIndex++;
this.goIndex = this.goIndex % this.mark;
this.y += 0.1 //飞机一直向下
}else{
//如果发生碰撞
this.goIndex++;
if(this.goIndex == this.length){
this.off = true;
this.goIndex = this.length-1;
}
}
}
// 发生碰撞方法
this.Hit = function(I){
return I.y + I.height > this.y
&& I.x + I.width > this.x
&& I.x < this.x + this.width
&& I.y < this.y + this.height
}
// 碰撞之后方法
this.afterHit = function(){
this.HP--;
if(this.HP == 0){
this.on = true;
socre += this.socre;
}
}
}
// 3.3.4创建数组,便于添加飞机
var enemys = [];
function inEnemys(){
var rad = Math.floor(Math.random()*500);
if(rad < 5){
enemys.push(new Enemy(ENEMYL));
}else if(rad >= 40 && rad < 45){
enemys.push(new Enemy(ENEMYM));
}else if(rad > 75 && rad < 80){
enemys.push(new Enemy(ENEMYB));
// if(enemys[0].type != 3 && enemys.length > 0){
// enemys.splice(0,0,new Enemy(ENEMYB))
// }
}
}
// 3.3.5绘制敌方飞机
function enemyPaint(){
for (var i = 0;i < enemys.length;i++) {
enemys[i].paint();
}
}
// 3.3.6地方飞机运动方法
function enemyImgMove(){
for (var j = 0;j < enemys.length;j++) {
enemys[j].imgMove();
}
}
// 3.3.7删除敌方飞机方法
function enemyDel(){
for(k = 0;k < enemys.length;k++){
if(enemys[k].y > h || enemys[k].off){
enemys.splice(k,1)
}
}
}
// 3.4碰撞后
function afterHitOn(){
for(var l = 0;l < enemys.length;l++){
// 如果我方飞机撞到
if(enemys[l].Hit(heros)){
enemys[l].afterHit()
heros.flow()
}
for (var m = 0;m < bullets.length;m++) {
// 如果子弹碰到地方飞机
if(enemys[l].Hit(bullets[m])){
enemys[l].afterHit()
bullets[m].flow()
}
}
}
}
// 3.5绘制分数和生命值
function paintText(){
context.font = "bold 30px 微软雅黑";
context.fillText("SOCRE:" + socre,10,30);
context.fillText("HP:" + HP,350,30)
}
// 4.暂停
canvas.onmouseover = function(){
if(status == PAUSE){
status = RUNNING;
}
}
canvas.onmouseout = function(){
if(status == RUNNING){
status = PAUSE;
}
}
var pause = new Image();
pause.src = "img/game_pause_nor.png";
function gameOver(){
context.font = "bold 50px 微软雅黑";
context.fillText("GAME OVER!",80,280);
}
// 创建定时器,将一直在运动的图片表现出来
setInterval(function(){
back.paint()
back.imgMove()
if(status == START){
context.drawImage(logo,30,0);
}else if(status == STARTING){
loading.paint()
loading.imgMove()
}else if(status == RUNNING){
heros.paint()
heros.imgMove()
heros.shoot()
bulletsPaint()
bulletsImgMove()
myDel()
inEnemys()
enemyPaint()
enemyImgMove()
enemyDel()
afterHitOn()
paintText()
}else if(status == PAUSE){
heros.paint()
bulletsPaint()
enemyPaint()
paintText()
context.drawImage(pause,220,270)
}else if(status == GAMEOVER){
heros.paint()
bulletsPaint()
enemyPaint()
paintText()
gameOver()
}
},1)
</script>
</body>
</html>