微信打飞机

微信打飞机.gif

对象:主机、子弹、敌机
动画:画、动
碰撞:主机碰撞敌机、子弹碰撞敌机(像素碰撞--性能问题)
玩法、声音:(选做)

普通碰撞

图片预加载

var imgArr = {
            "bg":"img/background.png",
            "herofly":"img/herofly.png",
            "bullet1":"img/bullet1.png",
            "bullet2":"img/bullet2.png",
            "enemy1":"img/enemy1.png",
            "enemy2":"img/enemy2.png",
            "enemy3":"img/enemy3.png",
            "prop":"img/prop.png"
        };
        var imgLength = 0;
        for(var i in imgArr){
            imgLength++;
        }
        var loadArr = {};
        var num = 0;    
        for(let i in imgArr){
            var img = new Image();
            img.src = imgArr[i];
            img.onload = function(){
                loadArr[i] = this;
                num++;
                if(num>=imgLength){
                    main(loadArr);
                }
            }
        }
function main(loadArr){
    var canvas = document.getElementById("mycanvas");
    var context = canvas.getContext("2d");
    canvas.width = loadArr.bg.width;
    canvas.height = loadArr.bg.height;     
            
    var alive = document.querySelector("#progress div");
    var aliveStyle =alive.currentStyle ? alive.currentStyle : window.getComputedStyle(alive,null);
    var scoreObj = document.querySelector("#score span");
    var score = 0;
            
    //主机对象构造函数
    function Hero(x,y,blood,obj){
        this.x = x;
        this.y = y;
        this.blood = blood;
        this.obj = obj;
        this.w = loadArr.herofly.width/5;
        this.h = loadArr.herofly.height;
        this.index = 0;
        this.num = 0;
        this.dis = "";
    }
    Hero.prototype.draw = function(){
        this.num++;
        //主机爆炸
        if(this.blood<=0){
            if(this.num%10 == 0){
                this.index++;
            }
        }
        context.drawImage(this.obj,(loadArr.herofly.width/5)*this.index,0,this.w,this.h,this.x,this.y,this.w,this.h);
    }
            
    Hero.prototype.move = function(){
        switch(this.dis){
            case "right":
                this.x+=3;
                if(this.x>=canvas.width-this.w){
                    this.x = canvas.width-this.w;
                }
                break;
            case "left":
                this.x-=3;
                if(this.x<=0){
                    this.x=0;
                }
                break;
            case "up":
                this.y-=3;
                if(this.y<=0){
                    this.y = 0;
                }
                break;
            case "down":
                this.y+=3;
                if(this.y>=canvas.height - this.h){
                    this.y = canvas.height-this.h;
                }
                break;
            default:
                break;
        }
    }
            
    //子弹对象构造函数
    function Bullet(x,y,power,obj,speed){
        this.x = x;
        this.y = y;
        this.power = power;
        this.obj = obj;
        this.w = obj.width;
        this.h = obj.height;
        this.speed = speed;
    }
    Bullet.prototype.draw = function(){
        context.drawImage(this.obj,0,0,this.w,this.h,this.x,this.y,this.w,this.h)
    }
    Bullet.prototype.move = function(){
        this.y -= this.speed;
        for(var j=0;j<enemy.length;j++){
            //子弹与敌机碰撞
            if(this.x<enemy[j].x+enemy[j].w&&this.x+this.w>enemy[j].x&&this.y<enemy[j].y+enemy[j].h&&this.y+this.h>enemy[j].y){
                var auEnemy = new Audio();
                auEnemy.autoplay = "autoplay";
                if(enemy[j].count==5){
                    enemy[j].blood-=30;
                    auEnemy.src = "audio/enemy1_down.mp3";
                }else if(enemy[j].count == 6){
                    enemy[j].blood -=25;
                    auEnemy.src = "audio/enemy2_down.mp3";
                }else if(enemy[j].count == 10){
                    enemy[j].blood -= 10;
                    auEnemy.src = "audio/enemy3_down.mp3";
                }  
                document.getElementsByTagName("body")[0].appendChild(auEnemy);
                setTimeout(function(){
                    auEnemy.remove();
                },1000)
                if(enemy[j].count>3){
                    this.y = -this.h;
                }
                if(enemy[j].index>=enemy[j].count-2&&enemy[j].count>3){
                    if(enemy[j].count == 5){
                        score += 10;
                    }else if(enemy[j].count == 6){
                        score += 20;
                    }else if(enemy[j].count == 10){
                        score += 30;
                    }
                    enemy[j].y = canvas.height;
//                  console.log(scoreObj.innerHTML);
                }
                
            }
        } 
    }
    //清除子弹
    Bullet.prototype.isClear = function(){
        if(this.y<=-this.h){
            return true;
        }else{
            return false;
        }
    }
    
    //敌机对象构造函数
    function Enemy(obj,x,y,w,h,speed){
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.obj = obj;
        this.speed = speed;
        this.blood = 100;
        this.index = 0;
        this.num = 0;
        this.count = 0;
    }
    Enemy.prototype.draw = function(){
        this.num++;
        if(this.blood<=50&&this.count>3){
            if(this.num%3==0){
                this.index++;
            }
        }
        context.drawImage(this.obj,this.index*this.w,0,this.w,this.h,this.x,this.y,this.w,this.h);
    }
    Enemy.prototype.move = function(){
        this.y += this.speed;
        //敌机与主机碰撞
        if(this.x<newHero.x+newHero.w&&this.x+this.w>newHero.x&&this.y<newHero.y+newHero.h&&this.y+this.h>newHero.y){
            if(isHero){
                if(_this.count>3){
                    newHero.blood -= 20;
                    alive.style.width = parseInt(aliveStyle.width) - 30 +"px";
                    isHero = false;
                }else{
                    if(_this.count == 0){
                        bulletObj = loadArr.bullet1;
                        for(var k=0;k<enemy.length;k++){
                            enemy[k].y = canvas.height;
                            if(enemy[k].count == 1){
                                bulletObj = loadArr.bullet2;
                            }else if(enemy[k].count == 2){
                                if(newHero.blood < 100){
                                    newHero.blood +=20;
                                    alive.style.width = parseInt(aliveStyle.width) + 30 +"px";
                                }
                            }else if(enemy[k].count == 5){
                                score += 10;
                            }else if(enemy[k].count == 6){
                                score += 20; 
                            }else if(enemy[k].count == 10){
                                    score += 30;
                            }
                        }
                    }else if(_this.count == 1){
                        bulletObj = loadArr.bullet2;
                    }else if(_this.count == 2){
                        if(newHero.blood < 100){
                            newHero.blood +=20;
                            alive.style.width = parseInt(aliveStyle.width) + 30 +"px";
                        }
                    }
                }
            }
            _this.y = canvas.height;
        }
    }
    Enemy.prototype.isClear = function(){
        if(this.y>=canvas.height){
            return true;
        }else{
            return false;
        }
    }
    //主机实例化
    var x = 120;
    var y = canvas.height-loadArr.herofly.height;
    newHero = new Hero(x,y,100,loadArr.herofly);
    console.log(newHero);
    newHero.draw();
    //键盘控制上下左右
    document.onkeydown = function(e){
        var ev = window.event || e;
        switch(ev.keyCode){
            case 37:
                newHero.dis = "left";
                break;
            case 38:
                newHero.dis = "up";
                break;
            case 39:
                newHero.dis = "right";
                break;
            case 40:
                newHero.dis = "down";
                break;
            default:
                newHero.dis="";
                break;
        }
    }
    document.onkeyup = function(){
        newHero.dis = "";
    }
    //子弹实例化
    bulletObj = loadArr.bullet1;
    var newBullet = new Bullet(newHero.x+(newHero.w/2)-bulletObj.width/2,y-10,2,loadArr.bullet1,2);
    newBullet.draw();
    //存放子弹的容器
    bullet = [];
    enemy = [];
    var count = 0;
    var bgY = 0;    
    isHero = true;
    function act(){
        count++;
        //清空画布
        context.clearRect(0,0,canvas.width,canvas.height);
        //背景
        drawBg();
        //主机
        newHero.draw();
        newHero.move();
        //敌机
        if(count%60==0){
            var rand = randNum(0,11);
//          console.log(rand);
            if(rand<=5){
                var enemyW = loadArr.enemy1.width/5;
                var enemyX =  randNum(0,canvas.width-enemyW+1);
                var newEnemy = new Enemy(loadArr.enemy1,enemyX,-loadArr.enemy1.height,enemyW,loadArr.enemy1.height,3);
                newEnemy.count = 5;
            }else if(rand>5 && rand<8){
                var enemyW = loadArr.enemy2.width/6;
                var enemyX =  randNum(0,canvas.width-enemyW+1);
                var newEnemy = new Enemy(loadArr.enemy2,enemyX,-loadArr.enemy2.height,enemyW,loadArr.enemy2.height,2);
                newEnemy.count = 6;
            }else if(rand==8){
                var enemyW = loadArr.enemy3.width/10;
                var enemyX = randNum(0,canvas.width-enemyW+1);
                var newEnemy = new Enemy(loadArr.enemy3,enemyX,-loadArr.enemy3.height,enemyW,loadArr.enemy3.height,1);
                newEnemy.count = 10;
            }else{
                var rands = randNum(0,3);
                var enemyW = loadArr.prop.width/3;
                var enemyX = randNum(0,canvas.width-enemyW+1);
                var newEnemy = new Enemy(loadArr.prop,enemyX,-loadArr.prop.height,enemyW,loadArr.prop.height,2);
                newEnemy.count = rands;
                newEnemy.index = rands;
            }
            enemy.push(newEnemy);
        }
        for(var i=0;i<enemy.length;i++){
            enemy[i].draw();
            enemy[i].move();
            if(enemy[i].isClear()){
                //判断是否出界清除
                enemy.splice(i,1);
                isHero = true;
            }
        }
        //子弹
        if(count%20==0){
            var newBullet = new Bullet(newHero.x+(newHero.w/2)-bulletObj.width/2,newHero.y-10,2,bulletObj,2);
            bullet.push(newBullet);
            var audios = new Audio();
            audios.src = "audio/bullet.mp3";
            audios.autoplay = "autoplay";
            document.getElementsByTagName("body")[0].appendChild(audios);
            setTimeout(function(){
                audios.remove();
            },100)
        }
        for(var i=0;i<bullet.length;i++){
            bullet[i].draw();
            bullet[i].move();
            if(bullet[i].isClear()){
                bullet.splice(i,1);
            }
        }
        scoreObj.innerHTML = score;
        var res = window.requestAnimationFrame(act);
        if(newHero.index>=4){
            cancelAnimationFrame(res);
            document.getElementById("mask").style.display = "block";
            document.querySelector("#mask audio").autoplay = "autoplay";
            document.querySelector("#gameStart").autoplay = "";
            document.querySelector("#endScore span").innerHTML = score;
        }
    }
    act();

    function drawBg(){
        //画背景
        bgY++;
        if(bgY>canvas.height){
            bgY=0;
        }
        context.drawImage(loadArr.bg,0,bgY);
        context.drawImage(loadArr.bg,0,bgY-canvas.height);
    }
}

随机函数

function randNum(min,max){
    return parseInt(Math.random()*(max-min)+min);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容