Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
//属性值
public float moveSpeed=3;//移动速度
private Vector3 bullectEulerAngles;
private float timeVal;//攻击CD
private float defendTimeVal=3;//无敌时间3秒
private bool isDefended=true;//无敌开启
//引用
private SpriteRenderer sr;//精灵渲染
public Sprite[] tankSprite;//上 右 下 左
public GameObject bullectPrefab;//子弹预制件
public GameObject explosionPrefab;//爆炸预制件
public GameObject defendEffectPrefab;//无敌预制件
public AudioSource moveAudio;//播放音频组件
public AudioClip[] tankAudio;//接受音频
private void Awake()//自动调用
{
//获取组件
sr = GetComponent
}
// Use this for initialization
void Start () {
}
//更新
void Update () {
//是否处于无敌状态
if (isDefended)
{
//setactive是控制GameObject对象显示/关闭
defendEffectPrefab.SetActive(true);
defendTimeVal -= Time.deltaTime;
if (defendTimeVal<=0)
{
isDefended = false;
defendEffectPrefab.SetActive(false);
}
}
}
//固定更新
private void FixedUpdate()
{
if (PlayerManager.Instance.isDefeat)
{
return;
}
Move();
//攻击的CD
if (timeVal >= 0.4f)
{
Attack();
}
else
{
timeVal += Time.fixedDeltaTime;
}
}
//坦克的攻击方法
private void Attack()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//子弹产生的角度:当前坦克的角度+子弹应该旋转的角度。
Instantiate(bullectPrefab, transform.position,Quaternion.Euler(transform.eulerAngles+bullectEulerAngles));
timeVal = 0;
}
}
//坦克的移动方法
private void Move()
{
//Vertical 键盘按上或下键时触发
float v = Input.GetAxisRaw("Vertical");
//使小球按照y轴正方向以每帧*3的单位长度移动
transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);
if (v < 0)
{
//下
sr.sprite = tankSprite[2];
bullectEulerAngles = new Vector3(0, 0, -180);
}
else if (v > 0)
{
//上
sr.sprite = tankSprite[0];
bullectEulerAngles = new Vector3(0, 0, 0);
}
//如果上下动了
if (Mathf.Abs(v)>0.05f)
{
//播放tankAudio[1]
moveAudio.clip = tankAudio[1];
if (!moveAudio.isPlaying)//如果不在播放
{
moveAudio.Play();//就播放
}
}
if (v != 0)
{
return;
}
float h = Input.GetAxisRaw("Horizontal");
//使小球按照x轴正方向以每帧*3的单位长度移动
transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
if (h < 0)
{
sr.sprite = tankSprite[3];
bullectEulerAngles = new Vector3(0, 0, 90);
}
else if (h > 0)
{
sr.sprite = tankSprite[1];
bullectEulerAngles = new Vector3(0, 0, -90);
}
if (Mathf.Abs(h) > 0.05f)
{
moveAudio.clip = tankAudio[1];
if (!moveAudio.isPlaying)
{
moveAudio.Play();
}
}
else
{
moveAudio.clip = tankAudio[0];
if (!moveAudio.isPlaying)
{
moveAudio.Play();
}
}
}
//坦克的死亡方法
private void Die()
{
//如果无敌
if (isDefended)
{
return;
}
PlayerManager.Instance.isDead = true;
//产生爆炸特效
Instantiate(explosionPrefab, transform.position, transform.rotation);
//死亡
Destroy(gameObject);
}
}
Bullet.cs
using System.Collections;
using System.Collections.Generic;
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)
{
collision.SendMessage("Die");
Destroy(gameObject);
}
break;
case "Heart":
collision.SendMessage("Die");
Destroy(gameObject);
break;
case "Enemy":
if (isPlayerBullect)
{
collision.SendMessage("Die");
Destroy(gameObject);
}
break;
case "Wall":
Destroy(collision.gameObject);
Destroy(gameObject);
break;
case "Barrier":
if (isPlayerBullect)
{
collision.SendMessage("PlayAudio");
}
Destroy(gameObject);
break;
default:
break;
}
}
}
Barriar.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Barriar : MonoBehaviour {
//接受声音
public AudioClip hitAudio;
public void PlayAudio()
{
//设置子弹射墙的音效
AudioSource.PlayClipAtPoint(hitAudio, transform.position);
}
}
Heart.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Heart : MonoBehaviour {
//精灵渲染
private SpriteRenderer sr;
//爆炸 游戏对象
public GameObject explosionPrefab;
//死亡 音效
public AudioClip dieAudio;
//精灵对象
public Sprite BrokenSprite;
// Use this for initialization
void Start () {
sr = GetComponent
}
public void Die()
{
sr.sprite = BrokenSprite;
//预设实例化
Instantiate(explosionPrefab, transform.position, transform.rotation);
//查找isDefeat并赋值
PlayerManager.Instance.isDefeat = true;
AudioSource.PlayClipAtPoint(dieAudio, transform.position);
}
}
Born.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Born : MonoBehaviour {
//玩家 游戏对象
public GameObject playerPrefab;
//敌人 游戏对象列表
public GameObject[] enemyPrefabList;
//是否创造玩家
public bool createPlayer;
// Use this for initialization
void Start () {
//1秒后执行某个函数
Invoke("BornTank", 1f);
//1秒后再销毁gameObject
Destroy(gameObject, 1);
}
// Update is called once per frame
void Update () {
}
private void BornTank()
{
if (createPlayer)
{
//实例化玩家对象
Instantiate(playerPrefab, transform.position, Quaternion.identity);
}
else
{
int num = Random.Range(0, 2);
//实例化敌人对象
Instantiate(enemyPrefabList[num], transform.position, Quaternion.identity);
}
}
}
Explosion.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explosion : MonoBehaviour {
// Use this for initialization
void Start () {
Destroy(gameObject, 0.167f);
}
// Update is called once per frame
void Update () {
}
}
MapCreation.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapCreation : MonoBehaviour {
//用来装饰初始化地图所需物体的数组。
//0.老家 1.墙 2.障碍 3.出生效果 4.河流 5.草 6.空气墙
public GameObject[] item;
//已经有东西的位置列表
private List
private void Awake()
{
InitMap();
}
private void InitMap()
{
//实例化老家
CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity);
//用墙把老家围起来
CreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity);
CreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity);
for (int i = -1; i < 2; i++)
{
CreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity);
}
//实例化上围墙
for (int i = -11; i < 12; i++)
{
CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity);
}
//实例化下围墙
for (int i = -11; i < 12; i++)
{
CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity);
}
//实例化左围墙
for (int i = -8; i < 9; i++)
{
CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity);
}
//实例化右围墙
for (int i = -8; i < 9; i++)
{
CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity);
}
//初始化玩家
GameObject go = Instantiate(item[3], new Vector3(-2, -8, 0), Quaternion.identity);
go.GetComponent
//产生敌人
CreateItem(item[3], new Vector3(-10, 8, 0), Quaternion.identity);
CreateItem(item[3], new Vector3(0, 8, 0), Quaternion.identity);
CreateItem(item[3], new Vector3(10, 8, 0), Quaternion.identity);
//4秒后执行,5秒CD
InvokeRepeating("CreateEnemy", 4, 5);
//实例化地图
for (int i = 0; i < 60; i++)
{
CreateItem(item[1], CreateRandomPosition(), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
CreateItem(item[2], CreateRandomPosition(), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
CreateItem(item[4], CreateRandomPosition(), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
CreateItem(item[5], CreateRandomPosition(), Quaternion.identity);
}
}
private void CreateItem(GameObject createCameObject,Vector3 createPosition,Quaternion createRotation)
{
GameObject itemGo = Instantiate(createCameObject, createPosition, createRotation);
itemGo.transform.SetParent(gameObject.transform);
itemPositionList.Add(createPosition);
}
//产生随机位置的方法
private Vector3 CreateRandomPosition()
{
//不生成x=-10,10的两列,y=-8,8正两行的位置
while (true)
{
Vector3 createPosition = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8), 0);
if (!HasThePosition(createPosition))
{
return createPosition;
}
}
}
//用来判断位置列表中是否有这个位置
private bool HasThePosition(Vector3 createPos)
{
for (int i = 0; i < itemPositionList.Count; i++)
{
if (createPos==itemPositionList[i])
{
return true;
}
}
return false;
}
//产生敌人的方法
private void CreateEnemy()
{
int num = Random.Range(0, 3);
Vector3 EnemyPos = new Vector3();
if (num==0)
{
EnemyPos = new Vector3(-10, 8, 0);
}
else if (num==1)
{
EnemyPos = new Vector3(0, 8, 0);
}
else
{
EnemyPos = new Vector3(10, 8, 0);
}
CreateItem(item[3], EnemyPos, Quaternion.identity);
}
}
Option.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Option : MonoBehaviour {
private int choice = 1;
public Transform posOne;
public Transform posTwo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.W))
{
choice = 1;
transform.position = posOne.position;
}
else if (Input.GetKeyDown(KeyCode.S))
{
choice = 2;
transform.position = posTwo.position;
}
if (choice==1&&Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene(1);
}
}
}
PlayManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerManager : MonoBehaviour {
//生命值
public int lifeValue = 3;
//得分
public int playerScore = 0;
public bool isDead;
public bool isDefeat;
//引用
public GameObject born;
public Text playerScoreText;
public Text PlayerLifeValueText;
public GameObject isDefeatUI;
//单例
private static PlayerManager instance;
public static PlayerManager Instance
{
get
{
return instance;
}
set
{
instance = value;
}
}
private void Awake()
{
Instance = this;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (isDefeat)
{
isDefeatUI.SetActive(true);
Invoke("ReturnToTheMainMenu", 3);
return;
}
if (isDead)
{
Recover();
}
playerScoreText.text = playerScore.ToString();
PlayerLifeValueText.text = lifeValue.ToString();
}
private void Recover()
{
if (lifeValue<=0)
{
//游戏失败,返回主界面
isDefeat = true;
Invoke("ReturnToTheMainMenu", 3);
}
else
{
lifeValue--;
GameObject go = Instantiate(born, new Vector3(-2, -8, 0), Quaternion.identity);
go.GetComponent
isDead = false;
}
}
private void ReturnToTheMainMenu()
{
SceneManager.LoadScene(0);
}
}