Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .vs/Tank/v16/.suo
Binary file not shown.
12 changes: 6 additions & 6 deletions Assets/Scripts/Barriar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using System.Collections.Generic;
using UnityEngine;

public class Barriar : MonoBehaviour {

public AudioClip hitAudio;
public class Barriar : MonoBehaviour {

public AudioClip hitAudio;

public void PlayAudio()
{
AudioSource.PlayClipAtPoint(hitAudio, transform.position);
//播放音乐
public void PlayAudio()
{
AudioSource.PlayClipAtPoint(hitAudio, transform.position);
}
}
46 changes: 25 additions & 21 deletions Assets/Scripts/Born.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

public class Born : MonoBehaviour {

//存放需要的坦克
public GameObject[] playerPrefab;

//存放需要的敌人
public GameObject[] enemyPrefabList;

//生成的模式
public int createPlayer;

// Use this for initialization
void Start () {
//生成一个敌人,后摧毁自己
void Start () {
Invoke("BornTank", 1f);
Destroy(gameObject, 1);
}
Expand All @@ -20,23 +21,26 @@ void Start () {
void Update () {

}
//诞生坦克
private void BornTank()
{
//诞生玩家1
if (createPlayer == 1)
{
Instantiate(playerPrefab[0], transform.position, Quaternion.identity);

private void BornTank()
{
if (createPlayer == 1)
{
Instantiate(playerPrefab[0], transform.position, Quaternion.identity);

}
else if(createPlayer == 2)
{
Instantiate(playerPrefab[1], transform.position, Quaternion.identity);
}
else
{
int num = Random.Range(0, 2);
Instantiate(enemyPrefabList[num], transform.position, Quaternion.identity);
}

}
//诞生玩家2
else if(createPlayer == 2)
{
Instantiate(playerPrefab[1], transform.position, Quaternion.identity);
}
//诞生敌人
else
{
int num = Random.Range(0, 2);
Instantiate(enemyPrefabList[num], transform.position, Quaternion.identity);
}

}
}
15 changes: 10 additions & 5 deletions Assets/Scripts/Bullect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@
using UnityEngine;

public class Bullect : MonoBehaviour {


//子弹移动速度
public float moveSpeed = 10;

//判别是谁的子弹
public bool isPlayerBullect;


// Use this for initialization

void Start () {

}

// Update is called once per frame

//子弹每秒移动
void Update () {
transform.Translate(transform.up * moveSpeed * Time.deltaTime, Space.World);
}


//判断碰到了谁
private void OnTriggerEnter2D(Collider2D collision)
{
//碰撞的标签
switch (collision.tag)
{
//给碰撞的物体死亡信息
case "Tank":
if (!isPlayerBullect)
{
Expand All @@ -43,6 +47,7 @@ private void OnTriggerEnter2D(Collider2D collision)
}

break;
//墙壁则摧毁
case "Wall":
Destroy(collision.gameObject);
Destroy(gameObject);
Expand Down
122 changes: 81 additions & 41 deletions Assets/Scripts/Enemy.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,72 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/****************************************
* 敌方坦克模块,由系统自动控制
******************************************/

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Enemy : MonoBehaviour {

//属性值
public float moveSpeed = 3;
private Vector3 bullectEulerAngles;
private float v=-1;
/*坦克属性值*/
public float moveSpeed = 3; //移动速度

private Vector3 bullectEulerAngles; //子弹飞行角度

private float v=-1;
// v控制坦克的垂直方向
// v=1 表示向上
// v=-1 表示向下

private float h;
// h控制坦克的水平方向
// h=1 表示向右
// h=-1 表示向左

//引用
private SpriteRenderer sr;
public Sprite[] tankSprite;//上 右 下 左
public GameObject bullectPrefab;
public GameObject explosionPrefab;

//计时器
private float timeVal;
private float timeValChangeDirection;
/*系统调用*/
private SpriteRenderer sr; //控制图片方向
public Sprite[] tankSprite; //上 右 下 左 方向的图片
public GameObject bullectPrefab; //出生特效
public GameObject explosionPrefab; //爆炸特效

/*计时器*/
private float timeValAttack; //攻击时间间隔
private float timeValChangeDirection; //转向时间间隔

private void Awake()
{
sr = GetComponent<SpriteRenderer>();
}

// Use this for initialization
void Start()
{

//nothing to do
}

// Update is called once per frame
void Update()
{


//攻击的时间间隔
if (timeVal >= 3)
if (timeValAttack >= 3)
{
//间隔时间到了就攻击
Attack();
}
else
{
timeVal += Time.deltaTime;
//间隔时间没到不能攻击,时间累积
timeValAttack += Time.deltaTime;
}

}

private void FixedUpdate()
{
Move();
Move(); //坦克移动方法
}

//坦克生成方法
private void Awake()
{
//获取一个坦克对象
sr = GetComponent<SpriteRenderer>();
}

//坦克的攻击方法
Expand All @@ -60,33 +75,42 @@ private void Attack()

//子弹产生的角度:当前坦克的角度+子弹应该旋转的角度。
Instantiate(bullectPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bullectEulerAngles));
timeVal = 0;
timeValAttack = 0;

}


//坦克的移动方法
private void Move()
{

//如果距离上一次改变朝向的时间间隔达到了4
if (timeValChangeDirection>=4)
{
//生成一个(0,8)的随机数,根据范围来确定方向
int num = Random.Range(0, 8);

//向下
if (num>5)
{
v = -1;
h = 0;
}

//向上
else if (num==0)
{
v = 1;
h = 0;
}

//向左
else if (num>0&&num<=2)
{
h = -1;
v = 0;
}

//向右
else if (num > 2 && num <= 4)
{
h = 1;
Expand All @@ -97,62 +121,78 @@ private void Move()
}
else
{
//时间累积
timeValChangeDirection += Time.fixedDeltaTime;
}




//改变垂直坐标
transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);

if (v < 0)
if (v < 0) //向下
{
//坦克图片改为向下
sr.sprite = tankSprite[2];

//子弹角度改为向下
bullectEulerAngles = new Vector3(0, 0, -180);
}

else if (v > 0)
else if (v > 0) //向上
{
//坦克图片改为向上
sr.sprite = tankSprite[0];

//子弹角度改为向上
bullectEulerAngles = new Vector3(0, 0, 0);
}

if (v != 0)
if (v != 0) //不动
{
return;
}


//改变水平坐标
transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
if (h < 0)

if (h < 0) //向左
{
//坦克图片改为向左
sr.sprite = tankSprite[3];

//子弹角度改为向左
bullectEulerAngles = new Vector3(0, 0, 90);
}

else if (h > 0)
else if (h > 0) //向右
{
//坦克图片改为向右
sr.sprite = tankSprite[1];

//子弹角度改为向右
bullectEulerAngles = new Vector3(0, 0, -90);
}
}

//坦克的死亡方法
//坦克死亡方法
private void Die()
{
PlayerManager.Instance.playerScore++;
//产生爆炸特效
Instantiate(explosionPrefab, transform.position, transform.rotation);
//死亡
//销毁
Destroy(gameObject);
//玩家得分加一
PlayerManager.Instance.playerScore++;
}

//坦克碰撞方法
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag=="Enemy")
{
//碰撞之后必须改变方向,将转向时间间隔调到4即可
timeValChangeDirection = 4;
}
}

}
}
Loading