编程题
- 设计游戏场景
- 红色胶囊是玩家(摄像机对象在玩家对象下面,摄像机放置到玩家头顶)
- 蓝色和黄色的门的自动门
pos01、pos02是克隆位置
- 设计游戏策略
- 通过键盘水平垂直键(WSAD)控制玩家移动
- 按下B键,蓝色自动门消失,10秒后自动门再次显示出来
- 游戏开始3秒后在位置pos1周围随机克隆3个黑色立方体,5秒后在位置pos2周围随机克隆5个黑色立方体(用协程)
- 当玩家移动到第一批黑色立方体附近时,点击鼠标左键,从玩家头顶发色红色子弹,当子弹打到黑色立方体时,黑色立方体消失,如果没用打到,红色子弹5秒后自动消失
- 当玩家移动到第二批黑色立方体附近时,按下一次F键使一个黑色立方体消失
- 当玩家移动到黄色自动门时,按下Y键,黄色自动门向上移动8米,10秒后回落到原来的位置
- 当玩家重新回到出发点,碰到一面空气墙时,游戏出现5秒倒计时
- 计时完毕,弹出游戏结束对话框,点击“重新开始”按钮,游戏可以重新开始
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class move : MonoBehaviour
{
Rigidbody r;
public float speed;
public GameObject blue;
public GameObject yellow;
public GameObject Enemy;
public GameObject En;
public GameObject txt;
GameObject[] Enemy1 = new GameObject[5];
int a = 0;
void Start()
{
r = GetComponent<Rigidbody>();
StartCoroutine(BB());
}
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 pos = new Vector3(h, 0, v);
r.velocity = pos * speed;
StartCoroutine(AA());//协程调用
StartCoroutine(CC());
StartCoroutine(DD());
}
IEnumerator AA()//按下B键,蓝色自动门消失,10秒后自动门再次显示出来
{
if (Input.GetKeyDown("b"))
{
blue.SetActive(false);//失活
yield return new WaitForSeconds(10f);
blue.SetActive(false);
}
}
IEnumerator BB()//游戏开始3秒后在位置pos1周围随机克隆3个黑色立方体,5秒后在位置pos2周围随机克隆5个黑色立方体(用协程)
{
yield return new WaitForSeconds(3f);
for (int i = 0; i < 3; i++)
{
float x = Random.Range(-43f, -32f);
float z = Random.Range(-44f, -30f);
Instantiate(Enemy, new Vector3(x, 1f, z), Quaternion.identity);
}
yield return new WaitForSeconds(5f);
for (int i = 0; i < 5; i++)
{
float x = Random.Range(-7f, -22f);
float z = Random.Range(-16f, -3f);
Enemy1[i] = Instantiate(En, new Vector3(x, 1f, z), Quaternion.identity);
}
}
IEnumerator DD()//当玩家移动到第二批黑色立方体附近时,按下一次F键使一个黑色立方体消失
{
yield return new WaitForSeconds(0);
if (Input.GetKeyDown("f"))
{
if (a < 5)
{
Destroy(Enemy1[a]);
a++;
}
}
}
IEnumerator CC()//当玩家移动到黄色自动门时,按下Y键,黄色自动门向上移动8米,10秒后回落到原来的位置
{
if (Input.GetKeyDown("y"))
{
yellow.transform.Translate(Vector3.up * 8);
yield return new WaitForSeconds(10f);
yellow.transform.Translate(Vector3.down * 8);
}
}
void OnTriggerEnter(Collider other)//当玩家重新回到出发点,碰到一面空气墙时
{
if (other.CompareTag("kongqi"))
{
txt.SetActive(true);//使Text文本使用
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mo : MonoBehaviour {
public GameObject sq;
public Transform poss;
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1")) //当玩家移动到第一批黑色立方体附近时,点击鼠标左键,从玩家头顶发色红色子弹
{
Instantiate(sq, poss.position, Quaternion.identity);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZD : MonoBehaviour {
Rigidbody r;
public float a = 5;
void Start () {
r = GetComponent<Rigidbody>();
StartCoroutine("AA");
}
// Update is called once per frame
void Update () {
r.velocity = Vector3.forward * a;
}
void OnTriggerEnter(Collider other)//,当子弹打到黑色立方体时,黑色立方体消失,如果没用打到,红色子弹5秒后自动消失
{
if (other .CompareTag("EN"))
{
Destroy(other.gameObject);
}
}
IEnumerator AA()
{
yield return new WaitForSeconds(5f);
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class daojis : MonoBehaviour
{
public Text tap;
float t = 6f;
public GameObject text;
public GameObject Button;
void Update()//,游戏出现5秒倒计时
{
if (t>1)
{
tap.text = ((int)t).ToString();
}
else
{
text.SetActive(false);
Button.SetActive(true);
}
t -= Time.deltaTime;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class cxkais : MonoBehaviour {
public void ReStart()//计时完毕,弹出游戏结束对话框,点击“重新开始”按钮,游戏可以重新开始
{
SceneManager.LoadScene("game");
Time.timeScale = 1;//游戏开始 重新开始
}
}