zoukankan      html  css  js  c++  java
  • 【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_PLAY:int = 20;
            public static const STATE_GAME_OVER:int = 30;
            public var gameState:int = 0;                    //游戏状态
            
            public var score:int = 0;        //玩家的分数
            public var chances:int = 0;        //漏掉的气球数
            public var level:Number = 1;    //等级
            
            public var bg:MovieClip;
            public var enemies:Array;        //存储敌人(气球)的数组
            public var player:MovieClip;    //引用玩家的图片
            public var tempEnemy:MovieClip;    //随机出来的气球
            
            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 = 380;    //计分板位于游戏底部
            
            public function Main():void
            {
                init();
            }
            private function init():void
            {
                this.addEventListener(Event.ENTER_FRAME, gameLoop);
                
                //显示背景
                bg = new BackImage();
                this.addChild(bg);
                
                //设置计分板标题和默认值
                scoreLabel.text = "得分:";
                scoreText.text = "0";
                levelLabel.text = "等级:";
                levelText.text = "1";
                chancesLabel.text = "碰撞:";
                chancesText.text = "0";
                
                //放置游戏底部
                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_PLAY:
                        playGame();
                        break;
                    
                    case STATE_GAME_OVER:
                        endGame();
                        break;
                }
            }
            
            //初始化游戏
            public function initGame():void
            {
                //初始化游戏
                score = 0;                    //分数
                chances = 0;                //漏网数            
                //level = 1;
                
                levelText.text = level.toString();
                
                player = new PlayerImage();
                this.addChild(player);
                player.startDrag(true, new Rectangle(0,0,550,400));
                
                //存储气球们
                enemies = new Array();
                
                gameState = STATE_PLAY;
            }
            
            //开始游戏
            public function playGame():void
            {
                //旋转飞镖
                player.rotation += 20;
                
                //创建气球(敌人)函数
                makeEnemies();
                
                //更新祈求Y坐标以及飘出屏幕时移除掉
                moveEnemies();
                
                //检测玩家是否与敌人发生了碰撞,如果是的话就触发一事件
                testCollisions();
                
                //查看等级(level)是否可增加或游戏是否可结束
                testForEnd();
            }
            
            public function makeEnemies():void
            {
                //获取一个0-99之间的随机数
                var chance:Number = Math.floor(Math.random() * 100);
                
                //等级越大,气球出现的概率越高
                if(chance < level+2)
                {
                    //创建气球
                    tempEnemy = new EnemyImage();
                    
                    //等级越高,气球速度越快
                    tempEnemy.speed = level + 3;
                    
                    //随机刷出1-5帧中对应颜色的气球出来
                    var xxxx = Math.floor(Math.random() * 5) + 1;                
                    tempEnemy.gotoAndStop(xxxx);
                    
                    //气球Y轴出现的坐标,从底端开始移动
                    tempEnemy.y = 435;
                    
                    //气球X轴出现的坐标,由于屏幕是0-540,所以气球(本身有30个宽度)从0-514中随机刷出
                    tempEnemy.x = Math.floor(Math.random() * 515);
                    
                    var tt:TextField = new TextField();
                    var ttf:TextFormat = new TextFormat();
                    ttf.bold = true;
                    ttf.align = "center";
                    ttf.size = 30;
                    tt.text = xxxx;
                    tt.setTextFormat(ttf);
                    tempEnemy.addChild(tt);
                    
                    //气球显示到场景
                    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;
                    
                    //若气球由下而上飞出屏幕顶部了
                    if(tempEnemy.y < -35)
                    {
                        chances++;    //漏网数+1
                        chancesText.text = chances.toString();
                        enemies.splice(i,1);    //该气球从气球数组中删掉
                        removeChild(tempEnemy);    //把气球从显示列表中移除
                    }
                }
            }
            
            //飞镖和气球碰撞检测
            public function testCollisions():void
            {
                //调用声音对象
                var sound:Sound = new Pop();
                var tempEnemy:MovieClip;
                
                //从气球数组里遍历剩下的气球
                for(var i:int = enemies.length - 1; i >= 0; i--)
                {
                    tempEnemy = enemies[i];
                    
                    //检测当前气球是否和飞镖有碰撞
                    if(tempEnemy.hitTestObject(player))
                    {
                        score++;
                        scoreText.text = score.toString();
                        sound.play();
                        enemies.splice(i,1);    //该气球从气球数组中删掉
                        removeChild(tempEnemy);    //把气球从显示列表中移除
                    }
                }
            }
            
            //查看等级(level)是否可增加或游戏是否可结束
            public function testForEnd():void
            {
                if(chances >= 5)
                {
                    gameState = STATE_GAME_OVER;
                }
                else if(score >= level * 5)
                {
                    level++;                
                    levelText.text = level.toString();
                    
                    var level_up:TextField = new TextField();
                    level_up.text = "恭喜你!升到" + level + "级了!!!";
                    this.addChild(level_up);
                }
            }        
            
            //结束游戏
            public function endGame():void
            {
                //把气球一个个的从显示列表中移除
                for(var i:int = 0; i < enemies.length; i++)
                {
                    removeChild(enemies[i]);
                    enemies= [];
                }
                
                player.stopDrag();
            }
        }
    }
  • 相关阅读:
    【mysql】添加对emoji的支持
    【mysql】关于innodb中MVCC的一些理解
    【mysql】关于硬件方面的一些优化
    关于php中的spl_autoload_register
    【mysql】高可用集群之MMM
    【mysql】数据库使用的一些规范
    使用sysbench进行压力测试
    【mysql】关于乐观锁
    【linux】spinlock 的实现
    【mysql】关于悲观锁
  • 原文地址:https://www.cnblogs.com/kingfly/p/2557797.html
Copyright © 2011-2022 走看看