【AS3代码】小游戏打飞机源代码

package
{
     import flash.display. MovieClip;
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.geom.Rectangle;
     import flash.media.Sound;
     import flash.text.*;

    
     public  class Main  extends Sprite
    {    
         public  static const STATE_INIT:int = 10;
         public  static const STATE_START_PLAYER:int = 20;
         public  static const STATE_PLAY_GAME:int = 30;
         public  static const STATE_REMOVE_PLAYER:int = 40;
         public  static const STATE_END_GAME:int = 50;
        
         public  var gameState:int = 0;     // 游戏状态
         public  var score:int = 0;         // 玩家的分数
         public  var chances:int = 0;         // 漏掉的气球数            
         public  var bg: MovieClip;         // 游戏背景
         public  var enemies:Array;         // 存储敌人的数组
         public  var missiles:Array;         // 玩家发射的导弹
         public  var explosions:Array;     // 爆炸特效
         public  var player: MovieClip;     // 引用玩家的图片
         public  var level: Number;         // 等级            
        
         public  var scoreLabel:TextField = new TextField();     // 分数标题
         public  var levelLabel:TextField = new TextField();     // 等级标题
         public  var chancesLabel:TextField = new TextField(); // 漏网标题
         public  var scoreText:TextField = new TextField();     // 分数值
         public  var levelText:TextField = new TextField();     // 等级值
         public  var chancesText:TextField = new TextField();     // 漏网值
         public const SCOREBOARD_Y: Number = 5;     // 计分板位于游戏顶部
        
         public  function Main():void
        {
            init();
        }
         private  function init():void
        {
            this.addEventListener(Event.ENTER_FRAME, gameLoop);
            
             // 显示背景
            bg = new BackImage();
            this.addChild(bg);
            
             // 设置计分板标题和默认值
            scoreLabel.text = "得分:";            
            levelLabel.text = "等级:";    
            chancesLabel.text = "生命:";            
            scoreText.text = "0";            
            levelText.text = "1";
            chancesText.text = "3";
            
            scoreLabel.textColor = 0xffffff;
            levelLabel.textColor = 0xffffff;
            chancesLabel.textColor = 0xffffff;
            scoreText.textColor = 0xffffff;
            levelText.textColor = 0xffffff;
            chancesText.textColor = 0xffffff;
            
             // 放置游戏底部
            scoreLabel.y = SCOREBOARD_Y;
            levelLabel.y = SCOREBOARD_Y;
            chancesLabel.y = SCOREBOARD_Y;
            scoreText.y = SCOREBOARD_Y;
            levelText.y = SCOREBOARD_Y;            
            chancesText.y = SCOREBOARD_Y;
            
             // 设置标题和值横向坐标
            scoreLabel.x = 5;
            scoreText.x = 50;
            chancesLabel.x = 105;
            chancesText.x = 155;
            levelLabel.x = 205;
            levelText.x = 260;
            
             // 显示计分板
            this.addChild(scoreLabel);
            this.addChild(levelLabel);
            this.addChild(chancesLabel);
            this.addChild(scoreText);
            this.addChild(levelText);
            this.addChild(chancesText);
            
            gameState = STATE_INIT;
        }
        
         // 调整游戏状态的循环函数
         public  function gameLoop(e:Event):void
        {
             switch(gameState)
            {
                 case STATE_INIT:
                    initGame();
                     break;
                
                 case STATE_START_PLAYER:
                    startPlayer();
                     break;
                
                 case STATE_PLAY_GAME:
                    playGame();
                     break;
                
                 case STATE_REMOVE_PLAYER:
                    removePlayer();
                     break;
                
                 case STATE_END_GAME:
                    endGame();
                     break;
            }
        }
        
         // 初始化游戏
         public  function initGame():void
        {
             // 当点击鼠标时,创建玩家发射的导弹
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownEvent);
            
             // 初始化游戏
            score = 0;                     // 分数
            chances = 3;                 // 飞船数    
            enemies = new Array();         // 敌人数组
            missiles = new Array();
            explosions = new Array();
            level = 1;
            
            levelText.text = level.toString();            
            player = new PlayerImage();
            gameState = STATE_START_PLAYER;
        }
        
         public  function startPlayer():void
        {
            this.addChild(player);
            player.startDrag( true, new Rectangle(0,320,550,365));
            gameState = STATE_PLAY_GAME;
        }
        
         public  function removePlayer():void
        {
             // 从敌人数组反向遍历出一个个敌人
             for( var i:int = enemies.length-1; i>=0; i--)
            {
                removeEnemy(i);
            }
             for(i = missiles.length-1; i>= 0; i--)
            {
                removeMissile(i);
            }
            this.removeChild(player);
            gameState = STATE_START_PLAYER;
        }
        
         // 开始游戏
         public  function playGame():void
        {
             // 创建敌人函数
            makeEnemies();
            
             // 更新祈求Y坐标以及飘出屏幕时移除掉
            moveEnemies();
            
             // 检测玩家是否与敌人发生了碰撞,如果是的话就触发一事件
            testCollisions();
            
             // 查看等级(level)是否可增加或游戏是否可结束
            testForEnd();
        }
        
         public  function makeEnemies():void
        {
             // 获取一个0-99之间的随机数
             var chance: Number = Math.floor(Math.random() * 100);
             var tempEnemy: MovieClip;
            
             // 等级越大,气球出现的概率越高
             if(chance < level+2)
            {
                 // 创建敌人                
                 var feiji: Number = Math.floor(Math.random() * 45);
                
                 if(feiji < 17)
                {
                    tempEnemy = new EnemyImage();
                }
                 else  if(feiji >= 17 && feiji < 34)
                {
                    tempEnemy = new EnemyImage1();
                }
                 else  if(feiji >= 34 && feiji < 45)
                {
                    tempEnemy = new EnemyImageBoss();
                }
                 // 等级越高,气球速度越快
                tempEnemy.speed = level + 1;
                
                 // 敌人出现的y坐标
                tempEnemy.y = -25;
                
                 // 气球X轴出现的坐标,由于屏幕是0-540,所以敌人(本身有30个宽度)从0-514中随机刷出
                tempEnemy.x = Math.floor(Math.random() * 515);
                
                 // 敌人显示到场景
                this.addChild(tempEnemy);
                
                 // 添加到敌人数组中,以便跟踪
                enemies.push(tempEnemy);
            }
        }
        
         // 遍历敌人数组,更新每个敌人Y坐标
         public  function moveEnemies():void
        {
             var tempEnemy: MovieClip;
             for( var i:int = enemies.length - 1; i >= 0; i--)
            {
                tempEnemy = enemies[i];
                tempEnemy.y += tempEnemy.speed;
                
                 var pp: Number = Math.floor(Math.random() * 4);
                 if(pp < 3)
                {
                    tempEnemy.x -= pp;
                }
                 else
                {
                    tempEnemy.x += pp;
                }
                
                
                 // 若敌人飞出屏幕底部了
                 if(tempEnemy.y > 435)
                {
                    removeEnemy(i);
                }
            }
            
             // 移除子弹
             var tempMissile: MovieClip;
             for(i = missiles.length-1; i>=0; i--)
            {
                tempMissile = missiles[i];
                tempMissile.y -= tempMissile.speed;
                 if(tempMissile.y < -35)
                {
                    removeMissile(i);
                }
            }
            
             // 移除爆炸效果
             var tempExplosion: MovieClip;
             for(i = explosions.length-1; i>= 0;i--)
            {
                tempExplosion = explosions[i];
                 if(tempExplosion.currentFrame >= tempExplosion.totalFrames)
                {
                    removeExplosion(i);
                }
            }
        }
        
         // 子弹和敌人碰撞检测、自己和敌人碰撞检测
         public  function testCollisions():void
        {
             var tempEnemy: MovieClip;     // 当前敌人
             var tempMissile: MovieClip;     // 当前发射的导弹
            
             // 遍历敌人数组
             for( var i:int = enemies.length - 1; i >= 0; i--)
            {
                 // 循环到的当前敌人对象
                tempEnemy = enemies[i];
                
                 // 遍历发射的导弹数组
                 for( var j:int = missiles.length-1; j>=0; j--)
                {
                     // 循环到的当前导弹对象
                    tempMissile = missiles[j];
                    
                     // 如果当前敌人和当前导弹发生碰撞
                     if(tempEnemy.hitTestObject(tempMissile))
                    {
                        score++;     // 积分+1
                        scoreText.text = score.toString();
                        
                         // 调用爆炸特效(传递敌人的x,y坐标,在该坐标上爆炸)
                        
                        makeExplosion(tempEnemy.x + (tempEnemy.width / 2), tempEnemy.y + (tempEnemy.height / 2));
                        
                        removeEnemy(i);         // 移除当前敌人
                        removeMissile(j);     // 移除当前导弹
                         break;
                    }
                }
            }
            
             // 检测自己和敌人是否有碰撞
             for(i = enemies.length-1; i>=0;i--)
            {
                 // 循环到的当前敌人
                tempEnemy = enemies[i];
                
                 // 如果当前敌人和自己发生碰撞
                 if(tempEnemy.hitTestObject(player))
                {
                    chances--;     // 生命-1
                    chancesText.text = chances.toString();
                    
                    makeExplosion(player.x + (player.width / 2), player.y + (player.height / 2));
                    
                    gameState = STATE_REMOVE_PLAYER;
                }
            }
        }
        
         // 创建并播放爆炸动画
         public  function makeExplosion(ex: Number, ey: Number)
        {
             // 创建爆炸对象
             var tempExplosion: MovieClip = new ExplosionImage();
            tempExplosion.x = ex;
            tempExplosion.y = ey;
            this.addChild(tempExplosion);
            
             // 由于爆炸动画有7帧,会自动播放,所以放到爆炸数组里以后控制它们
            explosions.push(tempExplosion);
            
             // 播放爆炸声音
             var sound:Sound = new Explode();
            sound.play();
        }
        
        
         // 查看等级(level)是否可增加或游戏是否可结束
         public  function testForEnd():void
        {
             if(chances <= 0)
            {
                removePlayer();
                gameState = STATE_END_GAME;
            }
             else  if(score > level * 30)
            {
                level++;                
                levelText.text = level.toString();
            }
        }    
        
         public  function removeEnemy(idx:int)
        {
            this.removeChild(enemies[idx]);
            enemies.splice(idx,1);
        }
        
         public  function removeMissile(idx:int)
        {
            this.removeChild(missiles[idx]);
            missiles.splice(idx,1);
        }
        
         public  function removeExplosion(idx:int)
        {
            this.removeChild(explosions[idx]);
            explosions.splice(idx,1);
        }
        
         public  function onMouseDownEvent(e:MouseEvent)
        {
             if(gameState == STATE_PLAY_GAME)
            {
                 // 创建子弹对象
                 var tempMissile: MovieClip = new MissileImage();
                tempMissile.x = player.x + (player.width / 2);
                tempMissile.y = player.y;
                tempMissile.speed = 20;
                missiles.push(tempMissile);
                this.addChild(tempMissile);
                
                 var sound:Sound = new Shoot();
                sound.play();
            }
        }
        
         // 结束游戏
         public  function endGame():void
        {
            trace("游戏结束!");
        }
    }
}

你可能感兴趣的:(as3)