简介
第二个项目 Nymph Find Money 是对第一个项目的创新,并独立创作。
项目的构思 1、将小鸟换成一个美少女 2、从在天上飞变为地上跑 3、地上会出现障碍 4、由鼠标控制变为键盘控制,并限制跳跃的次数 5、增加金币,让美少女吃到并加分
第一步 场景创建
新建叫ground的背景,然后添加碰撞器 ,并将添加新图层,将背景设为默认图层。
第二步 美少女创建
新建Player的游戏物体,然后给 Player 添加碰撞器与刚体。
第三步 动画创建
第四步 代码编写
public class CoinPools : MonoBehaviour
{
public GameObject prefabs;
Queue<GameObject> pools;
public float interval = 3f;
public int Poolsize = 20;
public static CoinPools instance;
public int x;
public int y;
private void Awake()
{
instance = this;
}
void Start()
{
pools = new Queue<GameObject>();
for (int i=0; i < Poolsize; i++)
{
GameObject coin = Instantiate(prefabs);
coin.SetActive(false);
pools.Enqueue(coin);
}
InvokeRepeating("Spawn", interval, interval);
}
void Spawn()
{
x = Random.Range(10, 30);
GameObject coin = pools.Dequeue();
coin.SetActive(true);
coin.transform.position = new Vector3(x, 2);
}
public void Despan(GameObject go)
{
go.SetActive(false);
pools.Enqueue(go);
}
}
public class GameControl : MonoBehaviour
{
public GameObject TipText;
public GameObject GameOverText;
public GameObject Score;
public bool IsGameOver = false;
private int score;
public static GameControl instance;
private void Awake()
{
instance = this;
}
void Update()
{
if (IsGameOver == true&& Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene("Main");
}
}
public void OnGameOver()
{
GameOverText.SetActive(true);
TipText.SetActive(true);
}
public void AddScore()
{
score += 1;
Score.GetComponent<Text>().text = string.Format("{0}", score);
}
}
public class ObstaclePools : MonoBehaviour
{
public static GameObject Prefabs;
public static GameObject Prafab_1;
public static GameObject Prefab_02;
public float interval = 4f;
public Queue<GameObject> pools;
public static ObstaclePools instance;
public GameObject[] ObstacleListPrefab = new GameObject[3] { Prefabs, Prafab_1, Prefab_02 };
public int poolsize=20;
private void Awake()
{
instance = this;
}
void Start()
{
pools = new Queue<GameObject>();
if (GameControl.instance.IsGameOver != true)
{ for (int i = 0; i < poolsize; i++)
{
var id = Random.Range(0, 2);
GameObject ob = Instantiate(ObstacleListPrefab[id]);
ob.SetActive(false);
pools.Enqueue(ob);
}
InvokeRepeating("Spawn", interval, interval);
}
}
void Spawn()
{
GameObject ob = pools.Dequeue();
ob.SetActive(true);
ob.transform.position =new Vector3(20, -1, 0);
}
public void Despawn(GameObject go)
{
go.SetActive(false);
pools.Enqueue(go);
}
}
public class Scrolling : MonoBehaviour
{
Rigidbody2D rd2d;
float Startpos;
public float width;
public float ScrollSpeed = -3;
public bool destory = false;
void Start()
{
rd2d = GetComponent<Rigidbody2D>();
rd2d.velocity = new Vector2(ScrollSpeed, 0);
if (width == 0)
{
width = GetComponent<BoxCollider2D>().size.x;
}
Startpos = transform.position.x;
}
void Update()
{
if (GameControl.instance.IsGameOver == true)
{
rd2d.velocity = Vector2.zero;
return;
}
if (Startpos - transform.position.x >= width)
{
if (destory == true)
{
ObstaclePools.instance.Despawn(this.gameObject);
CoinPools.instance.Despan(this.gameObject);
}
else
{
Vector3 pos = transform.position;
pos.x = Startpos;
transform.position = pos;
}
}
}
}
public class Player : MonoBehaviour
{
bool IsDead = false;
Rigidbody2D rd2d;
KeyCode key=KeyCode.Space;
Animator anim;
public float timer = 1.0f;
private void Start()
{
anim = GetComponent<Animator>();
anim.SetTrigger("Run");
}
void Update()
{
timer -= Time.deltaTime;
anim = GetComponent<Animator>();
rd2d=GetComponent<Rigidbody2D>();
if (Input.GetKeyDown(key))
{
if (timer <= 0)
{
rd2d.velocity = Vector2.zero;
rd2d.AddForce(new Vector2(0, 300));
anim.SetTrigger("Jump");
timer = 2.0f;
}
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Obstacle")
{
GameControl.instance.IsGameOver = true;
GameControl.instance.OnGameOver();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameControl.instance.AddScore();
collision.gameObject.SetActive(false);
}
}
|