• 简单游戏:九宫双片互换游戏


    先上结果:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class SudokuGame  {
     
    	public List<SudokuItem>   items ;
    
    	public void  Init()
    	{
    		items = getNewList (); 
    	}
    
    	SudokuItem preItem ;
     
    	public void  changeItem(   SudokuItem curItem )
    	{
    		if (preItem == null) {
    			preItem = curItem ;
    			return ;
    		}
    
    		if (curItem == preItem) {
    			Debug.Log("curItem == preItem");
    			preItem = curItem ;
    			return ;
    		}
    		SudokuItem tempItem = preItem;
    		int preIndex = items.IndexOf (preItem);
    		int curIndex = items.IndexOf (curItem); 
    
    
    		Debug.Log ("-----" + preIndex + "---" + curIndex);
     
    
    		items [preIndex] = curItem;
    		items [curIndex] = tempItem;
     
    
    		preItem = null;
    	}
    
    	public bool  checkMatch()
    	{
    		for (int i = 0; i < 9; i ++) {
     			if( items[i].index != i )
    			{
    				Debug.Log(i);
    				return false ;
    			}
    		}
    
    		return true;
    	}
    
    
    
    	 List<SudokuItem> getNewList()
    	{
    		List<SudokuItem> list = new List<SudokuItem> ();
    
    		for (int i = 0; i < 9; i ++) {
    			SudokuItem  item = new SudokuItem() ;
    			item.index = i ; 
    			list.Add( item ) ;
    		}
    
    		//return ShuffleItem(list);
    		return list;
    	}
    
    
    	public void  ShuffleItem(  )
    	{
    		List<SudokuItem> newlist = new List<SudokuItem> ();
    
    		for (int i = 0; i < 9 ; i ++) {
    			SudokuItem  item =  items[Random.Range( 0 , items.Count)] ;
    			items.Remove( item ) ;
    			newlist.Add( item ) ;
    
    		}
    
    		items = newlist;
     
    	}
    
    	void log()
    	{
    		log (items);
    	}
    
    	void  log(List<SudokuItem>   list )
    	{ 
    		for (int i = 0; i < 9 ; i ++) { 
    			Debug.Log("index:" + i  + "   listIndex:" + list[i].index  );
    		}
    		 
    	}
    
    }
    
    
    public class  SudokuItem
    {
    	/// <summary>
    	/// 0-9
    	/// </summary>
    	public int  index ;
     
     
    	public Texture2D  texture ;
    }
     
    
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class SudokuView : MonoBehaviour {
    
    	SudokuGame game  ;
    
    	List< GameObject>   goItems  ;
    
    
    	public Texture2D  HoldImage ;
    
    	// Use this for initialization
    	void Start () {
    		game = new SudokuGame ();
    		game.Init ();
    
    		//initView
    		InitView ();
    	}
    	
    	// Update is called once per frame
    	void Update () {
    	
    	}
    
    	void InitView()
    	{
    		int with = 64;
    		int space = 5;
    
    		GameObject  prefab = Resources.Load("Prefab_ItemTexture") as GameObject ;
    
    		List<Texture2D> pics = getPicsByImage (HoldImage);
    
    		goItems = new List<GameObject> ();
    
    		for (int i = 0; i <  game.items.Count; i ++) {
    
    			SudokuItem  curItem = game.items[i] ;
    
    			GameObject  go = NGUITools.AddChild( gameObject , prefab ) ;
    			//go.GetComponent<UISprite>().spriteName = curItem.picName ;  
    
    			go.GetComponent<UITexture>().mainTexture =  pics[i] ;
    			curItem.texture = pics[i] ;
    
    			go.GetComponent<SudokuItemView>().InitView( curItem , 
    			                      delegate(SudokuItem  item) {
    				//Debug.Log( item.index);
    
    				this.game.changeItem( item ) ;
    				this.ReflashView();
     
    				if( this.game.checkMatch() )
    				{
    					Debug.Log("wwwwwwwwwwwww");
    				} else
    				{
    					Debug.Log("fffffffffff");
    
    				}
    			});
    
    			go.transform.localPosition = new Vector3(  (with + space) * (i%3) , -(with +  space) * (i/3) ,  0 ) ;
    			goItems.Add(  go  ) ;
    		}
    
    		game.ShuffleItem ();
    		ReflashView();
    	}
    
    
    	void ReflashView()
    	{
    		//Debug.Log ("xx");
    		for (int i = 0; i <  goItems.Count; i ++) {
    			
    			SudokuItem curItem = game.items [i]; 
    			//Debug.Log("---A-" + curItem.view.GetComponent<UISprite> ().spriteName );
    			//curItem.view.GetComponent<UISprite> ().spriteName = curItem.picName;  
    			//goItems[i].GetComponent<UISprite>().spriteName = curItem.picName ;
    			goItems[i].GetComponent<UITexture>().mainTexture =  curItem.texture ;
    
    			goItems[i].GetComponent<SudokuItemView>().refleshView( curItem);
    			//Debug.Log("---B-" + curItem.view.GetComponent<UISprite> ().spriteName );
    		}
    	}
    
    
    	List<Texture2D>  getPicsByImage( Texture2D   image)
    	{
    		List<Texture2D>   pics = new List<Texture2D> ();
    		
    		for (int i = 2 ; i >= 0; i --) {
    			for( int  j = 0 ; j <3 ; j++ )
    			{
    				Color[] pixels = image.GetPixels ( j * 64 , i* 64 , 64, 64);
    				//Color[] pixels = image.GetPixels (64 ,  0 , 64, 64);
    				//Debug.Log (pixels.Length);
    				
    				Texture2D tt = new Texture2D (64, 64);
    				tt.SetPixels (pixels);
    				tt.Apply (); 
    				
    				pics.Add( tt) ;
    			}
    		}
    		
    		return pics;
    	}
    }
    
    using UnityEngine;
    using System.Collections;
    using System;
    
    public class SudokuItemView : MonoBehaviour {
    
    	public UILabel  lbIndex ;
    
    	// Use this for initialization
    	void Start () {
    	
    	} 
    	
    	// Update is called once per frame
    	void Update () {
    	
    	}
    
    	SudokuItem  item ;
    	Action<SudokuItem>  back ;
    
    	public void refleshView(SudokuItem  item )
    	{
    		this.item = item;
    		lbIndex.text = item.index.ToString();
    	}
    
    	public void InitView( SudokuItem  item  , Action<SudokuItem>  back )
    	{
    		this.item = item;
    		this.back = back;
    		lbIndex.text = item.index.ToString();
    	}
    
    
    	void OnClick()
    	{ 
    		back (item);
    
    	}
    }
    
  • 相关阅读:
    多线程(6)线程属性
    多线程(五) Thread和Object中线程相关方法
    面试汇总
    多线程(4)线程生命周期
    多线程(3) 多线程之线程的停止和中断
    springboot(6)redis缓存
    软件安装(总)
    redis分布式锁
    第一天
    Thinkphp5高级进阶教程
  • 原文地址:https://www.cnblogs.com/didiaodexi/p/4875203.html
Copyright © 2020-2023  润新知