『我闪网~www.5shan.com』

Categories

-=推荐开发方面的Flash新闻、教程、下载、酷站、游戏、图片等=-
首页

Links

New Comments

Counter

Calendar

[教程]Flash骰子游戏,没有UI

Author:我闪 PublishTime:2004-9-21

这个游戏对学习类的朋友是一个不错的例子。

来源AOL

游戏规则:参加游戏的玩家,每个人掷两次骰子.两次之和谁大的,谁就是赢家.
主要是试一下用UML的方法来做游戏,但总觉得Actor只有一个玩家,顶多也就是多个Timer.这个游比较简单也看不出来什么,不过熟悉一下做游戏的这道流程也是一种收获.

class Player
{
        private var name:String;
        private var score:Number;
        function Player(name:String)
        {
                this.name = name;
        }
        public function play(die1:Die, die2:Die):Void
        {
                die1.roll();
                die2.roll();
                score = die1.getFaceValue()+die2.getFaceValue();
        }
        public function getName():String
        {
                return name;
        }
        public function getScore():Number
        {
                return score;
        }
}

class Die
{
        private var faceValue:Number;
        public function getFaceValue():Number
        {
                return faceValue;
        }
        public function roll():Void
        {
                faceValue = int(Math.random() * 6 + 1);
        }
}

class DiceGame
{
        private var die1, die2:Die;
        private var player1,player2:Player;
        
        public function DiceGame(name1:String,name2:String)
        {
                player1 = new Player(name1);
                player2 = new Player(name2);
                die1 = new Die();
                die2 = new Die();
        }
        public function play():Void
        {
                player1.play(die1, die2);
                player2.play(die1, die2);
        }
        public function getWinner():Player
        {
                var score1:Number = player1.getScore();
                var score2:Number = player2.getScore();
                trace(score1+":"+score2);
                if (score1== score2)
                {
                        return null;
                }
                else
                {
                        return (score1> score2)?player1:player2;
                }
        }
        
        //程序入口
        public static function main():Void
        {
                var game:DiceGame = new DiceGame("小强","张立");
                game.play();
                var winner:Player =game.getWinner();
                if(winner!=null)
                trace(winner.getName()+"胜!");
        }
}
//main.fla
DiceGame.main(); 
分类于:我闪推荐

Comments

{CommentAuthor} at {CommentTime} | {CommentEmail} {CommentUrl} {CommentIp}
{CommentContent}
Powered by 5DBlog.com