【AS3代碼】翻牌游戲源碼


文檔類:

 package

{
     import com.ui.Card;
    
     import flash.display. MovieClip;
     import flash.display.Sprite;
     import flash.display.StageScaleMode;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.events.TimerEvent;
     import flash.media.Sound;
     import flash.media.SoundChannel;
     import flash.text.TextField;
     import flash.text.TextFormat;
     import flash.utils.Timer;
     import flash.utils.getTimer;
    
     public  class Main  extends Sprite
    {
         private  static const boardWidth:uint = 6;         // 卡片列數
         private  static const boardHeight:uint = 5;         // 行數
         private  static const cardHorizontalSpacing: Number = 62;     // 卡片水平間隔
         private  static const cardVerticalSpacing: Number = 62;      // 垂直間隔
         private  static const boardOffsetX: Number = 10;     // 卡片左上角橫坐標
         private  static const boardOffsetY: Number = 85;     // 卡片左上角縱坐標
         private  static const pointsForMatch:int = 100;     // 游戲分數,匹配+100
         private  static const pointsForMiss:int = -5;     // 游戲分數,不匹配-5
        
         // 兩個臨時Card變量
         private  var firstCard:Card;
         private  var secondCard:Card;
         private  var cardsLeft:uint;         // 用於卡片計數,為0則結束游戲
         private  var gameScore:int;         // 分數
         private  var gameStartTime:uint;     // 初始化時間
         private  var gameTime:uint;         // 獲取游戲時間
        
         // 聲明兩文本
         private  var gameScoreField:TextField;
         private  var gameTimeField:TextField;
        
         // 卡片自動翻轉時間變量
         private  var flipBackTimer:Timer;
        
         // 聲音變量
         private  var theFirstCardSound:FirstCardSound = new FirstCardSound();
         private  var theMissSound:MissSound = new MissSound();
         private  var theMatchSound:MatchSound = new MatchSound();        
        
         // 卡片數組
         var cardlist:Array;
        
         public  function Main():void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.stageWidth = 550;
            stage.stageHeight = 400;
            init();
        }
         private  function init():void
        {
             var ym:YingMu = new YingMu();
            this.addChild(ym);
            
             // 舞台上生成15對卡片列表的索引值,並存入到數組cardlist中
            cardlist = new Array();
             for( var i:uint=0; i<(boardWidth*boardHeight) / 2; i++)
            {
                cardlist.push(i);
                cardlist.push(i);
            }
            
             // 把30張卡片添加到舞台
            cardsLeft = 0;
             for( var x:uint=0; x<boardWidth; x++)
            {
                 for( var y:uint=0; y<boardHeight; y++)
                {
                     var c:Card = new Card();     // 卡片副本
                    c.x = x * cardHorizontalSpacing + boardOffsetX;     // 卡片在屏幕上的左上角坐標
                    c.y = y * cardVerticalSpacing +boardOffsetY;
                    
                     // 得到一個隨機卡片索引值
                     var r:uint = Math.floor(Math.random() *  cardlist.length);
                    c.cardface = cardlist[r];     // 把卡片索引值賦給card
                    cardlist.splice(r,1);         // 從數組中移除卡片                
                    
                    c.buttonMode =  true;
                    this.addChild(c);     // 卡片添加到屏幕
                    cardsLeft++             // 生成卡片數自動+1
                        
                     // 點擊卡片偵聽函數
                    c.addEventListener(MouseEvent.CLICK, clickCard);
                }
            }
            
             // 設置文本格式
             var format:TextFormat = new TextFormat();
            format.size = 16;
            format.font = "宋體";
            format.bold =  true;
            
             // 設置游戲分數文本並復制
            gameScoreField = new TextField();
            this.addChild(gameScoreField);
            gameScoreField.defaultTextFormat = format;
            gameScoreField.selectable =  false;
            gameScoreField.width = 200;
            gameScoreField.height = 50;
            gameScoreField.x = 400;
            gameScoreField.y = 110;
            gameScore = 0;
            
             // 調用計分函數
            showGameScore();
            
             // 設置時鍾文本
            gameTimeField = new TextField();
            this.addChild(gameTimeField);
            gameTimeField.defaultTextFormat = format;
            gameTimeField.selectable =  false;
            gameTimeField.width = 200;
            gameTimeField.height = 50;
            gameTimeField.x = 400;
            gameTimeField.y = 85;
            gameTimeField.text =  String(getTimer());            
            gameTime = 0;
            
             // 調用計時函數
            this.addEventListener(Event.ENTER_FRAME, showTime);            
        }
        
         // 點擊卡片函數
         public  function clickCard(event:MouseEvent):void
        {
             // 當第一張牌和第二張牌都翻開時,其余牌點擊無效
             if(firstCard !=  null && secondCard !=  null)
            {
                 return;
            }
            
             // 把點擊的卡片對象賦值給它的實例
             var thisCard:Card = (event.currentTarget as Card);
            
             // 翻開第一張牌
             if(firstCard ==  null)
            {
                firstCard = thisCard;
                
                 // 翻轉到對應的牌面上,thisCard.cardface + 2表示所在的哪一幀
                 // 因為cardlist保存着0-14,Card的第一幀是牌背面,所以0和1幀都無效,所以+2才是第一張牌的正面
                thisCard.startFlip(thisCard.cardface + 2);
                
                 // 播放翻牌聲音
                playSound(theFirstCardSound);
            }
             // 重復翻開第一張牌
             else  if(firstCard == thisCard)
            {
                 // 第二次點擊后,牌翻轉回去,參數1表示Card的第一幀
                firstCard.startFlip(1);
                firstCard =  null;
                playSound(theMissSound);
            }
             // 翻開第二張牌
             else  if(secondCard ==  null)
            {
                secondCard = thisCard;
                thisCard.startFlip(thisCard.cardface + 2);
                
                 // 播放翻牌聲音
                playSound(theFirstCardSound);
                
                 // 開始比較兩個卡片
                 if(firstCard.cardface == secondCard.cardface)
                {
                    showScoreText(secondCard, 0);
                    
                     // 如果匹配,都移除屏幕
                    this.removeChild(firstCard);
                    this.removeChild(secondCard);
                    
                     // 兩個變量值復位
                    firstCard =  null;
                    secondCard =  null;
                    
                     // 計算分數,匹配+100
                    gameScore += pointsForMatch;
                    
                     // 調用顯示分數文本函數
                    showGameScore();
                    playSound(theMatchSound);
                    
                     // 若匹配一次,總數量-2
                    cardsLeft -= 2;
                    
                     // 通過cardsLeft檢測游戲是否結束
                     if(cardsLeft == 0)
                    {
                        trace("恭喜!游戲成功通關!");
                        trace("--------------------------");
                        trace("游戲分數:" + gameScore);
                        trace("游戲時間:" + clockTime(gameTime));
                        trace("--------------------------");
                    }
                }
                 // 兩個卡片不同
                 else
                {
                    showScoreText(secondCard, 1);
                    
                     // 匹配錯誤,-5分
                    gameScore += pointsForMiss;
                    showGameScore();
                    
                     // 兩張不匹配,0.8秒后自動翻轉回去
                    flipBackTimer = new Timer(800,1);
                    flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE, returnCards);
                    flipBackTimer.start();
                }
            }
             else
            {
                returnCards( null);
                firstCard = thisCard;
                firstCard.startFlip(thisCard.cardface + 2);
            }
        }
        
         // 匹配或不匹配時飄出的得分文字.type =0匹配,1不匹配
         public  function showScoreText(secondCard:Card, type:uint):void
        {
             var sTxt: MovieClip;
             if(type == 0)
            {
                sTxt = new FanOk();
            }
             else  if(type == 1)
            {
                sTxt = new FanEroor();
            }
            sTxt.x = secondCard.x;
            sTxt.y = secondCard.y;
            this.addChild(sTxt);
        }
        
         // 卡片自動翻轉回去函數
         public  function returnCards(event: String):void
        {
            firstCard.startFlip(1);         // 第1張牌翻回到背面
            secondCard.startFlip(1);     // 第2張牌翻回到背面
            firstCard =  null;
            secondCard =  null;
            flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
        }
        
         // 顯示分數文本函數
         public  function showGameScore():void
        {
            gameScoreField.text = "游戲得分:" +  String(gameScore);
        }
        
         // 顯示游戲時間毫秒數函數
         public  function showTime(event:Event):void
        {
            gameTime = getTimer() - gameStartTime;
            gameTimeField.text = "已用時間:" + clockTime(gameTime);
        }
        
         // 時間轉換函數
         public  function clockTime(ms:int): String
        {
             var seconds:int = Math.floor(ms/1000);
             var minutes:int = Math.floor(seconds/60);
            seconds -= minutes * 60;
             var timeString: String = minutes + ":"+ String(seconds+100).substr(1,2);
             return timeString;
        }
        
         // 播放聲音函數
         public  function playSound(soundObject:Sound):void
        {
            soundObject.play();
        }
    }
}

 

Card類:

package com.ui
{
     import flash.display. MovieClip;
     import flash.events.Event;
     import flash.text.TextField;
    
     public  dynamic  class Card  extends  MovieClip
    {
         private  var flipStep:uint;
         private  var isFlipping: Boolean =  false;
         private  var flipToFrame:uint;
         private  var c:Card_FF;
        
         public  function Card()
        {            
            c = new Card_FF();
            c.stop();     // 停在第一幀
            this.addChild(c);
        }
        
         public  function startFlip(flipToWhichFrame:uint):void
        {
            isFlipping =  true;
            flipStep = 10;
            flipToFrame = flipToWhichFrame;
            this.addEventListener(Event.ENTER_FRAME, flip);            
        }
        
         // 需要10幀完成卡片翻轉
         public  function flip(event:Event):void
        {
            flipStep--;     // 幀數遞減
            
             // 前5幀翻轉時放大狀態
             if(flipStep > 5)
            {
                this.scaleX = 0.2 * (flipStep - 6);
            }
             else
            {
                this.scaleX = 0.2 * (5 - flipStep);
            }
            
            c.gotoAndStop(flipToFrame);
            
             // 反轉完后移除偵聽
             if(flipStep == 0)
            {
                this.removeEventListener(Event.ENTER_FRAME,flip);
            }
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM