游戏介绍
unity开发,通过控制坦克的移动与攻击,保护基地,消灭敌方坦克。
开发过程
背景的导入与素材的处理
选择素材Graphics中的backgrand作为背景。
如果素材没有被分割需要我们手动分割,点击需要分割的素材在Sprite Mode中选择Multiple后点击右下的Sprite Editor选择Apply进入分割页面点击左上角的Slice选择Slice系统将自动分割,点击Apply。
预制体与2D动画的形成
预制体的形成 在Project面板下建立Prafads文件夹将Hierarchy面板下的贴图,拖回Prafabs文件夹下,形成预制体。 2D动画的形成 按住ctrl将多个图片拖入Hierarchy面板下,创建Animation文件夹,命名并保存到Animation文件夹。
控制玩家的移动及其方向
public void Move()
{
float i = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * i * MoveSpeed * Time.fixedDeltaTime, Space.World);
if (i < 0)
{
sr.sprite = tanksprite[3];
bulletsprite = new Vector3(0, 0, 90);
}
else if (i > 0)
{
sr.sprite = tanksprite[1];
bulletsprite = new Vector3(0, 0, -90);
}
if (i != 0)
{
return;
}
float j = Input.GetAxis("Vertical");
transform.Translate(Vector3.up * j * MoveSpeed * Time.fixedDeltaTime, Space.World);
if (j < 0)
{
sr.sprite = tanksprite[2];
bulletsprite = new Vector3(0, 0, 180);
}
else if (j > 0)
{
sr.sprite = tanksprite[0];
bulletsprite = new Vector3(0, 0, 0);
}
}
玩家主代码
public class Player : MonoBehaviour
{
public float MoveSpeed = 2;
private Vector3 bulletsprite;
private float time;
private float defenttime=5;
private bool isdefended=true;
public AudioClip attackAudio;
public AudioClip explorAudio;
public SpriteRenderer sr;
public Sprite[] tanksprite;
public GameObject bulletPro;
public GameObject explortank;
public GameObject Shield0;
private void Awake()
{
sr = GetComponent<SpriteRenderer>();
}
添加碰撞器,解决抖动
给player加刚体和碰撞器,给其他障碍物加碰撞器 在刚体组件下点击Constraints 把Z轴锁死,就可以解决移动当中的旋转 把代码放在FiexedUpdate生命周期函数中,因为固定帧数,可以解决抖动
坦克的攻击CD,移动优先级问题,层级渲染
private void FixedUpdate()
{
if(Playerstate.Instance.isDefent)
{
return;
}
Move();
if (time >= 0.4f)
{
Attack();
}
else
{
time += Time.fixedDeltaTime;
}
}
Oorder in Layer 越大渲染层级越高。
坦克的攻击方法,无敌的保护时间
private void Attack()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Instantiate(bulletPro, transform.position, Quaternion.Euler(transform.eulerAngles+bulletsprite));
AudioSource.PlayClipAtPoint(attackAudio, transform.position);
time = 0;
}
}
void Update()
{
if(isdefended)
{
Shield0.SetActive(true);
defenttime -= Time.deltaTime;
if(defenttime<=0)
{
isdefended = false;
Shield0.SetActive(false);
}
}
}
坦克的死亡与爆炸
private void Die()
{
if(isdefended)
{
return;
}
Playerstate.Instance.dieplayer = true;
Instantiate(explortank, transform.position,transform.rotation);
AudioSource.PlayClipAtPoint(explorAudio, transform.position);
Destroy(gameObject);
}
子弹的脚本
public class Bullet : MonoBehaviour
{
private float moveSpeed = 10;
public bool bulletplayer;
void Start()
{
}
void Update()
{
transform.Translate(transform.up*moveSpeed*Time.deltaTime,Space.World);
}
public void OnTriggerEnter2D(Collider2D collision)
{
switch(collision.tag)
{
case "Player1":
{
if(!bulletplayer)
{ collision.SendMessage("Die"); Destroy(gameObject); }
}
break;
case "Heart":
{
collision.SendMessage("Die");
Destroy(gameObject);
}
break;
case "Wall":
{
Destroy(collision.gameObject);
Destroy(gameObject);
}
break;
case "Barriar":
{
if (bulletplayer)
{
collision.SendMessage("PlayerAudio");
}
Destroy(gameObject);
}
break;
case "Eneray":
{
if(bulletplayer)
{
collision.SendMessage("Die");
Destroy(gameObject);
}
}
break;
default:break;
}
}
}
敌人脚本的编写
public class Enemys : MonoBehaviour
{
public float MoveSpeed = 2;
private Vector3 bulletsprite;
private float time;
private float timeChange=4;
private float i;
private float j=-1;
public SpriteRenderer sr;
public Sprite[] tanksprite;
public GameObject bulletPro;
public GameObject explortank;
private void Awake()
{
sr = GetComponent<SpriteRenderer>();
}
void Start()
{
}
void Update()
{
if (time >= 3)
{
Attack();
}
else
{
time += Time.deltaTime;
}
}
private void FixedUpdate()
{
Move();
}
private void Attack()
{
Instantiate(bulletPro, transform.position, Quaternion.Euler(transform.eulerAngles + bulletsprite));
time = 0;
}
public void Move()
{
if (timeChange >= 4)
{
int num = Random.Range(0, 8);
if (num >= 4)
{
i = 0;
j = -1;
}
else if (num == 0)
{
j = 1;
i = 0;
}
else if (num > 0 && num <= 2)
{
i = 1;
j = 0;
}
else if (num > 2 && num < 4)
{
i = -1;
j = 0;
}
timeChange = 0;
}
else
timeChange += Time.fixedDeltaTime;
transform.Translate(Vector3.right * i * MoveSpeed * Time.fixedDeltaTime, Space.World);
if (i < 0)
{
sr.sprite = tanksprite[3];
bulletsprite = new Vector3(0, 0, 90);
}
else if (i > 0)
{
sr.sprite = tanksprite[1];
bulletsprite = new Vector3(0, 0, -90);
}
if (i != 0)
{
return;
}
transform.Translate(Vector3.up * j * MoveSpeed * Time.fixedDeltaTime, Space.World);
if (j < 0)
{
sr.sprite = tanksprite[2];
bulletsprite = new Vector3(0, 0, 180);
}
else if (j > 0)
{
sr.sprite = tanksprite[0];
bulletsprite = new Vector3(0, 0, 0);
}
}
private void Die()
{
Playerstate.Instance.score++;
Instantiate(explortank, transform.position, transform.rotation);
Destroy(gameObject);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag=="Eneray")
{
timeChange = 4;
}
}
}
坦克的出生特效
public class Born : MonoBehaviour
{
public GameObject BornPlayer;
public GameObject[] EnemyBorn;
public bool creatPlayer;
public AudioClip bornAudio;
void Start()
{
Invoke("BornTank", 1.2f);
AudioSource.PlayClipAtPoint(bornAudio, transform.position);
Destroy(gameObject, 1.2f);
}
void Update()
{
}
private void BornTank()
{
if (creatPlayer)
{
Instantiate(BornPlayer, transform.position, Quaternion.identity);
}
else
{
int num = Random.Range(0, 2);
Instantiate(EnemyBorn[num], transform.position, Quaternion.identity);
}
}
}
基地(心脏)的爆炸与死亡
public class Heart : MonoBehaviour
{
public AudioClip dieAudio;
public SpriteRenderer sr;
public Sprite Borken;
public GameObject Explorer;
void Start()
{
sr = GetComponent<SpriteRenderer>();
}
public void Die()
{
sr.sprite = Borken;
Instantiate(Explorer,transform.position,transform.rotation);
Playerstate.Instance.isDefent = true;
AudioSource.PlayClipAtPoint(dieAudio,transform.position);
}
}
地图的建立
public class MapCreat : MonoBehaviour
{
private List<Vector3> itemlist = new List<Vector3>();
public GameObject[] item;
private void Awake()
{
GreateItem(item[0],new Vector3(0,-8,0),Quaternion.identity);
GreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity);
GreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity);
for(int i=-1;i<2;i++)
{
GreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity);
}
for(int i=-11;i<=10;i++)
{
GreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity);
}
for (int i = -11; i <=10; i++)
{
GreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity);
}
for (int i = -9; i <= 8; i++)
{
GreateItem(item[6], new Vector3(-11,i , 0), Quaternion.identity);
}
for (int i = -9; i <= 8; i++)
{
GreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity);
}
for(int i=0;i<60;i++)
{
GreateItem(item[1], CreateRandom(), Quaternion.identity);
}
for (int i = 0; i < 19; i++)
{
GreateItem(item[2], CreateRandom(), Quaternion.identity);
}
for (int i = 0; i < 18; i++)
{
GreateItem(item[4], CreateRandom(), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
GreateItem(item[5], CreateRandom(), Quaternion.identity);
}
GameObject go = Instantiate(item[3], new Vector3(-2, -8, 0), Quaternion.identity);
go.GetComponent<Born>().creatPlayer = true;
GreateItem(item[3], new Vector3(-10, 8, 0), Quaternion.identity);
GreateItem(item[3], new Vector3(10, 8, 0), Quaternion.identity);
GreateItem(item[3], new Vector3(0, 8, 0), Quaternion.identity);
InvokeRepeating("CreateEnemy",4,5);
}
private void GreateItem(GameObject CreategameObject,Vector3 Creatvector,Quaternion Craetquaternion)
{
GameObject itemgo = Instantiate(CreategameObject,Creatvector,Craetquaternion);
itemgo.transform.SetParent(gameObject.transform);
itemlist.Add(Creatvector);
}
private Vector3 CreateRandom()
{
while (true)
{
Vector3 Creatvector = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8), 0);
if (!HasPosition(Creatvector))
{
return Creatvector;
}
}
}
private bool HasPosition(Vector3 Creatpos)
{
for(int i=0;i<itemlist.Count;i++)
{
if(Creatpos==itemlist[i])
{
return true;
}
}
return false;
}
private void CreateEnemy()
{
int h = Random.Range(0, 3);
Vector3 EnemyPos = new Vector3();
if(h==0)
{
EnemyPos= new Vector3(-10, 8, 0);
}
else if(h==1)
{
EnemyPos = new Vector3(10, 8, 0);
}
else if(h==2)
{
EnemyPos = new Vector3(0, 8, 0);
}
GreateItem(item[3], EnemyPos , Quaternion.identity);
}
}
玩家状态管理的创建
定义生命值,分数,可以实时反映在UI界面,死亡状态,失败状态,调用相应方法。
public class Playerstate : MonoBehaviour
{
public int life = 3;
public int score = 0;
public bool dieplayer;
public GameObject born;
public GameObject isDefentUI;
public bool isDefent;
public Text playerLifeText;
public Text playerScoreText;
private static Playerstate instance;
public static Playerstate Instance { get => instance; set => instance = value; }
private void Awake()
{
Instance = this;
}
void Start()
{
}
void Update()
{
if(isDefent)
{
isDefentUI.SetActive(true);
Invoke("ReturnMenu", 3);
}
if(dieplayer)
{
Reverce();
}
playerLifeText.text = life.ToString();
playerScoreText.text = score.ToString();
}
private void Reverce()
{
if (life <= 0)
{
isDefent = true;
Invoke("ReturnMenu", 3);
}
else
{
life--;
GameObject go = Instantiate(born, new Vector3(-2, -8, 0), Quaternion.identity);
go.GetComponent<Born>().creatPlayer = true;
dieplayer = false;
}
}
private void ReturnMenu()
{
SceneManager.LoadScene(1);
}
}
UI的制作
public class Image : MonoBehaviour
{
public Transform poseone;
public Transform posetwo;
private float h = 1;
void Start()
{
}
void Update()
{
if(Input.GetKeyDown(KeyCode.W))
{
h = 1;
transform.position = poseone.position;
}
else if(Input.GetKeyDown(KeyCode.S))
{
h = 2;
transform.position = posetwo.position;
}
if (Input.GetKeyDown(KeyCode.Space) && h ==1)
{
SceneManager.LoadScene(0);
}
}
}
添加音效
可以加入Audio Souce组件,通过拖拽添加。
|