var cols:int = 4;//列数 var rows:int = 4;//行数 var totalCards:int = 16;//所有卡片的数量 var matchesNeededToWin:int = totalCards*0.5;//最大匹配数,用来判断游戏是否胜利 var matchesMade:int = 0; var cardW:int = 100; var cardH:int = 100; var aCards:Array;//用来保存所有的牌,要洗的时候,所有的东西放到这个里面 var aGrid:Array; var aCardsFlipped:ArrayList;//翻开的牌的序列 var playerCanClick:boolean;//能否翻拍(开始的时刻木有翻牌,所以开的牌是0 //,翻开第一张的时候,可以翻开第二张,但是翻开第二张的时候,那么不可以再翻牌) var playerHasWon:boolean = false;//用户是否胜利 //创建一个类 class Card extends System.Object{ var isFaceUp:boolean = false; var isMatched:boolean = false; var img:String; var id:int; function Card(img:String,id:int){ this.img = img; this.id = id; //img = "robot"; } } //洗牌 function BuildDeck(){ var totalRobots:int = 4;//一共有四种机器人,红绿蓝黄 var card:Object; var id:int = 0; for(i=0; i<totalRobots; i++)//一共四轮 { var aRobotParts:Array = ["Head", "Arm", "Leg"]; for(j=0; j<2; j++) { var someNum:int = Random.Range(0, aRobotParts.length);//生成一个0到3的随机数 var theMissingPart:String = aRobotParts[someNum];//拼凑一个字符串,用来表示一张图片 aRobotParts.RemoveAt(someNum);//这个已经用过了,就从数组中移除 card = new Card("robot" + (i+1) + "Missing" + theMissingPart,id); aCards.Add(card); card= new Card("robot" + (i+1) + theMissingPart,id); aCards.Add(card); id++; } } } function Start(){ //可以翻牌 playerCanClick = true; aCards = new Array(); aGrid = new Array(); aCardsFlipped = new ArrayList(); BuildDeck();//开始洗牌 //就一直在这里产生,产生一个,移除一个,再产生一个再移除一个。知道为空 for(i=0; i<rows; i++) { aGrid[i] = new Array();//数组初始化1维 for(j=0; j<cols; j++) { //aGrid[i][j] = new Card();//数组初始化2维 var someNum:int = Random.Range(0,aCards.length); aGrid[i][j] = aCards[someNum]; aCards.RemoveAt(someNum); } } } //翻牌 function FlipCardFaceUp(card:Object){ card.isFaceUp = true; if(aCardsFlipped.IndexOf(card)<0) { aCardsFlipped.Add(card); if(aCardsFlipped.Count == 2) { playerCanClick = false; yield WaitForSeconds(1); if(aCardsFlipped[0].id == aCardsFlipped[1].id) { aCardsFlipped[0].isMatched = true; aCardsFlipped[1].isMatched = true; matchesMade++; if(matchesMade>=matchesNeededToWin) { playerHasWon = true; } } else { aCardsFlipped[0].isFaceUp = false; aCardsFlipped[1].isFaceUp = false; } aCardsFlipped = new ArrayList(); playerCanClick = true; } } } function BuildGrid(){ GUILayout.BeginVertical(); GUILayout.FlexibleSpace(); for(i=0; i<rows; i++) { GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); for(j=0; j<cols;j++) { var card:Object = aGrid[i][j]; var img:String; //如果图片已经匹配了,那么就做成空白 if(card.isMatched) { img = "blank"; } else { if(card.isFaceUp) { img = card.img; } else { img = "wrench";//牌的背面 } } GUI.enabled = !card.isMatched; //点击了某一个按钮 if(GUILayout.Button(Resources.Load(img),GUILayout.Width(cardW))) { if(playerCanClick) { //翻牌 FlipCardFaceUp(card); } Debug.Log(card.img); } GUI.enabled = true; } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } GUILayout.FlexibleSpace(); GUILayout.EndVertical(); } //创建赢的时候的画面(就是两个正方形,让后让用户看的) function BulidWinPrompt(){ var winPromptW:int = 100; var winPromptH:int = 90; var halfScreenW:float = Screen.width/2; var halfScreenH:float = Screen.height/2; var halfPromptW:int = winPromptW/2; var halfPromptH:int = winPromptH/2; GUI.BeginGroup(Rect(halfScreenW-halfPromptW,halfScreenH-halfPromptH, winPromptW, winPromptH)); GUI.Box (Rect (0,0,winPromptW,winPromptH),"You Win!!"); if(GUI.Button(Rect(10,40,80,20),"Play Again")) { Application.LoadLevel(0); } GUI.EndGroup(); } //这种布局类似于html function OnGUI(){ GUILayout.BeginArea(Rect(0,0,Screen.width,Screen.height)); BuildGrid(); if(playerHasWon) BulidWinPrompt(); GUILayout.EndArea(); print("building grid!"); }