Unity3D打地鼠

比较简陋,上图






两个代码

1 MainScript

using UnityEngine;
using System.Collections;

public class MainScript : MonoBehaviour {

	public GameObject[]points;
	public UILabel scoreLable;
	public GameObject uiPoint1;
	public GameObject uiPoint2;	
	public GameObject[]dishu;
	public UIPanel startPanel;
	public UIPanel gamePanel;
	public UIPanel pausePanel;
	public UIPanel scorePanel;
	public UIPanel helpPanel;
	
	public GameObject blue;
	public GameObject red;
	public GameObject zi;
	
	public float gameTime;
	
	public int score;
	
	UIPanel nowPanel;
	
	
	// Use this for initialization
	void Start () 
	{
		nowPanel=startPanel;
		PauseGame();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetKey(KeyCode.Escape))
		{
			Application.Quit();	
		}
		
		gameTime=Time.time;
		if(Input.GetMouseButtonDown(0))
		{
			Ray myRay = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
			
			RaycastHit hit;
			if(Physics.Raycast(myRay,out hit))
			{
				if (hit.transform.gameObject.tag == "blue" || hit.transform.gameObject.tag == "red" || hit.transform.gameObject.tag == "zi") 
				{
					hit.transform.gameObject.renderer.material.color = Color.black;

					//蓝色50分
					if (hit.transform.gameObject.tag == "blue") {
						SendMessage ("AddScore", 50);	
					}
                    //红色100分
					if (hit.transform.gameObject.tag == "red") {
						SendMessage ("AddScore", 100);	
					}
                    //紫色200分
					if (hit.transform.gameObject.tag == "zi") {
						SendMessage ("AddScore", 200);	
					}
					scoreLable.text=score+"";
				}
			}
		}
		

	}
	
	void OnClick(GameObject btn)
	{
		if(btn.name=="StartButton")
		{
			TweenPanel(gamePanel);
			
			StartGame();
			gameTime=Time.time;
		}
		
		if(btn.name=="ScoreButton")
		{
			TweenPanel(scorePanel);	
		}
		
		if(btn.name=="OptionButton")
		{
				
		}
		
		if(btn.name=="HelpButton")
		{
			TweenPanel(helpPanel);	
		}
		
		if(btn.name=="ScoreReturnButton")
		{
			TweenPanel(startPanel);	
		}
		
		if(btn.name=="AgainButton")
		{
			StartGame();
			TweenPanel(gamePanel);	
		}
		
		if(btn.name=="PauseReturnStartButton")
		{
			TweenPanel(startPanel);	
		}
		
		if(btn.name=="PauseReturnGameButton")
		{
			StartGame();
            gameTime = Time.time;
			TweenPanel(gamePanel);
			
		}
		
		if(btn.name=="HelpReturnButton")
		{
			TweenPanel(startPanel);		
		}
		
		if(btn.name=="GameScoreButton")
		{
			PauseGame();
			TweenPanel(scorePanel);	
		}
		
		if(btn.name=="GamePauseButton")
		{
			PauseGame();
			TweenPanel(pausePanel);	
		}
		
	}
	//移动界面的方法
	void TweenPanel(UIPanel panel)
	{
		nowPanel.transform.position=uiPoint2.transform.position;
		nowPanel=panel;
		panel.transform.position=uiPoint1.transform.position;
	}
	
	
	
	
	//加分数,使用sendMessage调用
	void AddScore(int a)
	{
		score+=a;
	}
	
	void StartGame()
	{
		for(int i=0;i<points.Length;i++)
		{
			points[i].SetActive(true);	
		}
	}
	
	void PauseGame()
	{
		for(int i=0;i<points.Length;i++)
		{
			points[i].SetActive(false);	
		}
	}
	
}

2 GameScript

using UnityEngine;
using System.Collections;

public class GameScript : MonoBehaviour
{

	
	public GameObject blue;
	public GameObject red;
	public GameObject zi;
	
	GameObject temp;
	
	float gameTime;
	public static float diShuLife;
	
	bool flag=true;
	// Use this for initialization
	void Start ()
	{
		
		CreatDiShu();
	}
	
	// Update is called once per frame
	void Update ()
	{
		gameTime=Time.time;
		CheckDiShuBeBlack();
	}
	
	void CreatDiShu ()
	{
        //出现的概率百分之五十
		int i = Random.Range (1, 3);
			
		if (i == 1) 
		{
			int j = Random.Range (1, 11);
			if (j == 1) 
			{
				temp = Instantiate (zi, gameObject.transform.position, Quaternion.identity)as GameObject;
				temp.transform.parent=gameObject.transform;
				diShuLife=Time.time;
				flag=true;
			}
			if (j == 2 || j == 3 || j == 4 || j == 5 || j == 6) 
			{
				temp = Instantiate (blue, gameObject.transform.position, Quaternion.identity)as GameObject;
				temp.transform.parent=gameObject.transform;
				diShuLife=Time.time;
				flag=true;
			}
			if (j == 7 || j == 8 || j == 9 || j == 10) 
			{
				temp = Instantiate (red, gameObject.transform.position, Quaternion.identity)as GameObject;
				temp.transform.parent=gameObject.transform;
				diShuLife=Time.time;
				flag=true;
			}
		}
		else
		{
			StartCoroutine(Wait());
		}
	}
	
	
	
	void CheckDiShuBeBlack ()
	{
		if(temp!=null)
		{
			if(temp.gameObject.renderer.material.color==Color.black&&flag==true)
			{
				StartCoroutine(DestoryDiShu());
				flag=false;
			}
			
			if(temp.gameObject.renderer.material.color!=Color.black&&gameTime-diShuLife>=1)
				
			{
				
				StartCoroutine(Wait1());
			}
		}
		
	}
	
	IEnumerator DestoryDiShu ()
	{
		
		yield return new WaitForSeconds(0.2f);
 		Destroy(temp);
		yield return new WaitForSeconds(0.5f);
		CreatDiShu();
	}
	
	IEnumerator Wait()
	{
		yield return new WaitForSeconds(0.8f);
		CreatDiShu();
	}
	
	IEnumerator Wait1()
	{
		Destroy(temp);
		yield return new WaitForSeconds(0.5f);
		CreatDiShu();
	}
	
}


点这里下载

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地狱为王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值