(结对作业)基于Unity 3D游戏引擎开发的“打砖块”游戏

来源:互联网 发布:免费的分身软件 编辑:程序博客网 时间:2024/06/10 03:23

游戏简介:游戏在传统的打砖块游戏的基础上增加的3D的效果。游戏的操作非常简单,简单的左右方向键控制,以及鼠标对视角的控制。

编写人员: 朱俊璋,王士溥

开发工具:Unity3D  4.3

开发语言:C#


计划

任务分工:后台的游戏功能的具体实现和测试,后台代码的编写和调试      朱俊璋

              游戏前端界面的设计和游戏逻辑功能的创想       王士溥

开发时间:2周

功能简介:3D打砖块小游戏,实现基本功能,在游戏中可以通过鼠标调节camera视角以及位置——左键+移动鼠标-水平移动;右键+移动鼠标-摄像头倾斜角度;滚轮-缩放镜头

运行游戏:

原始视角


通过鼠标调节视角


游戏界面的设计:




源代码:

BreakoutGame类:该类是游戏的主功能类,游戏的初始化和游戏界面的渲染都在此类中。该脚本绑定到游戏Main Camera组件中,使游戏在一开始时就调用此脚本。
using UnityEngine;using System.Collections;public enum BreakoutGameState { playing, win, lost };//定义游戏状态.public class BreakoutGame : MonoBehaviour{    <span style="white-space:pre"></span>public static BreakoutGame SP;<span style="white-space:pre"></span>public Transform ballPrefab;//珠球.<span style="white-space:pre"></span>private int totalBlocks;//总的方块数.private int blocksHit;//已经击打的方块数.    <span style="white-space:pre"></span>private BreakoutGameState gameState;//游戏状态.public float ZoomSpeed = 10;public float MovingSpeed = 0.5f;//移动速度.public float RotateSpeed = 1;//旋转速度.public float distance = 5;   <span style="white-space:pre"></span> void Awake()    <span style="white-space:pre"></span>{        <span style="white-space:pre"></span>SP = this;       <span style="white-space:pre"></span>blocksHit = 0;        <span style="white-space:pre"></span>gameState = BreakoutGameState.playing;       <span style="white-space:pre"></span>totalBlocks = GameObject.FindGameObjectsWithTag("Pickup").Length;//得到所有的方块数.       <span style="white-space:pre"></span>Time.timeScale = 1.0f;//设置传递时间为1 .//SpawnBall();    <span style="white-space:pre"></span>}void Update(){Quaternion rotation = Quaternion.identity;Vector3 position;float delta_x, delta_y, delta_z;float delta_rotation_x, delta_rotation_y;//按下鼠标左键.if(Input.GetMouseButton(0)){delta_x = Input.GetAxis("Mouse X") * MovingSpeed;//获取x轴方向的鼠标运动增量,乘以相应的移动速度.delta_y = Input.GetAxis("Mouse Y") * MovingSpeed;//获取y轴方向的鼠标运动增量,乘以相应的移动速度.//rotation = Quaternion.Euler(0,transform.rotation.eulerAngles.y,0);//设置旋转的角度,存储在Quaternion中.rotation.eulerAngles = new Vector3(0,transform.rotation.eulerAngles.y,0);transform.position = rotation * new Vector3(-delta_x,0,-delta_y)+transform.position;//Debug.Log(rotation);}//按下鼠标右键.if(Input.GetMouseButton(1)){delta_rotation_x = Input.GetAxis("Mouse X") * RotateSpeed;delta_rotation_y = Input.GetAxis("Mouse Y") * RotateSpeed;position = transform.rotation*new Vector3(0,0,distance)+transform.position;transform.Rotate(0,delta_rotation_x,0,Space.World);transform.Rotate(delta_rotation_y,0,0);transform.position = transform.rotation * new Vector3(0,0,-distance)+position;}//滑动鼠标滚动条.if(Input.GetAxis("Mouse ScrollWheel")!=0){delta_z = -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;transform.Translate(0,0,-delta_z);distance += delta_z;}} /// <summary>/// 初始化珠球/// </summary>    <span style="white-space:pre"></span>void SpawnBall()    <span style="white-space:pre"></span>{        <span style="white-space:pre"></span>Instantiate(ballPrefab, new Vector3(1.81f, 1.0f , 9.75f), Quaternion.identity);//实例化珠球.    <span style="white-space:pre"></span>}/// <summary>/// 界面的渲染/// </summary>    <span style="white-space:pre"></span>void OnGUI(){<span style="white-space:pre"></span>GUILayout.Label("作者:朱俊璋、王士溥");        <span style="white-space:pre"></span>GUILayout.Space(10);//添加空格.        <span style="white-space:pre"></span>GUILayout.Label(" 己击打: " + blocksHit + "/" + totalBlocks);//游戏开始.if (GUI.Button(new Rect(0,150,80,30),"开始游戏")) {SpawnBall();}       <span style="white-space:pre"></span>if (gameState == BreakoutGameState.lost)        <span style="white-space:pre"></span>{            <span style="white-space:pre"></span>GUILayout.Label("你输了!");            <span style="white-space:pre"></span>if (GUILayout.Button("重新加载"))            <span style="white-space:pre"></span>{                <span style="white-space:pre"></span>Application.LoadLevel(Application.loadedLevel);//重新加载关卡.            <span style="white-space:pre"></span>}        <span style="white-space:pre"></span>}else if (gameState == BreakoutGameState.win)        <span style="white-space:pre"></span>{            <span style="white-space:pre"></span>GUILayout.Label("你赢了!");            <span style="white-space:pre"></span>if (GUILayout.Button("重新加载"))            <span style="white-space:pre"></span>{                <span style="white-space:pre"></span>Application.LoadLevel(Application.loadedLevel);            <span style="white-space:pre"></span>}        <span style="white-space:pre"></span>}    <span style="white-space:pre"></span>}/// <summary>/// 击打砖块/// </summary>    <span style="white-space:pre"></span>public void HitBlock()    <span style="white-space:pre"></span>{       <span style="white-space:pre"></span> <span style="white-space:pre"></span>blocksHit++;        <span style="white-space:pre"></span>if (blocksHit%10 == 0) //每击打十个砖块生成新的珠球.       <span style="white-space:pre"></span> <span style="white-space:pre"></span>{            <span style="white-space:pre"></span>SpawnBall();       <span style="white-space:pre"></span> }       <span style="white-space:pre"></span> if (blocksHit >= totalBlocks)//游戏胜利.        <span style="white-space:pre"></span>{            <span style="white-space:pre"></span>WinGame();        <span style="white-space:pre"></span>}    <span style="white-space:pre"></span>}/// <summary>/// 珠球掉落/// </summary>public void LostBall(){int ballsLeft = GameObject.FindGameObjectsWithTag("Player").Length;//获得剩余珠球数.if(ballsLeft<=1){SetGameOver();//游戏结束.}}/// <summary>/// 游戏胜利/// </summary>    <span style="white-space:pre"></span>public void WinGame()    <span style="white-space:pre"></span>{        <span style="white-space:pre"></span>Time.timeScale = 0.0f; //设置游戏暂停.        <span style="white-space:pre"></span>gameState = BreakoutGameState.win;    <span style="white-space:pre"></span>}/// <summary>/// 游戏失败 /// </summary>    <span style="white-space:pre"></span>public void SetGameOver()    <span style="white-space:pre"></span>{        <span style="white-space:pre"></span>Time.timeScale = 0.0f; //设置游戏暂停.        <span style="white-space:pre"></span>gameState = BreakoutGameState.lost;    <span style="white-space:pre"></span>}}
Ball类:该类绑定在BallPre.prefab游戏组件中,用于控制小球的速度。
using UnityEngine;using System.Collections;public class Ball : MonoBehaviour {    public float maxVelocity = 20;//珠球最大速度.    public float minVelocity = 15;//珠球最小速度.void Awake () {        rigidbody.velocity = new Vector3(0, 0, -18);//小球初始速度.}void Update () {        //控制小球的速度在15—20之间.        float totalVelocity = Vector3.Magnitude(rigidbody.velocity);//得到珠球的总的速度.        if(totalVelocity > maxVelocity){            float tooHard = totalVelocity / maxVelocity;            rigidbody.velocity /= tooHard;        }        else if (totalVelocity < minVelocity)        {            float tooSlowRate = totalVelocity / minVelocity;            rigidbody.velocity /= tooSlowRate;        }//print(rigidbody.velocity);        //若珠球的z坐标小于-3,游戏结束.        if(transform.position.z <= -3){                        BreakoutGame.SP.LostBall();            Destroy(gameObject);//消除游戏组件.        }//print (rigidbody.velocity);}}
Paddle类:该脚本绑定到游戏组件paddle中,用于调节控制板条的移动。
using UnityEngine;using System.Collections;public class Paddle : MonoBehaviour {    public float moveSpeed = 20;void Update () {//获取水平方向,得到移动距离.float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;        <span style="white-space:pre"></span>transform.position += new Vector3(moveInput, 0, 0);//控制板条移动范围.        <span style="white-space:pre"></span>float max = 14.0f;        <span style="white-space:pre"></span>if (transform.position.x <= -max || transform.position.x >= max)        <span style="white-space:pre"></span>{          <span style="white-space:pre"></span>  <span style="white-space:pre"></span>float xPos = Mathf.Clamp(transform.position.x, -max, max); //板条移动的x坐标范围在-max和max之间.        <span style="white-space:pre"></span>    <span style="white-space:pre"></span>transform.position = new Vector3(xPos, transform.position.y, transform.position.z);        <span style="white-space:pre"></span>}}/// <summary>/// 增加小球碰撞后的水平速度,否则小球左右反弹的效果不理想。当珠球退出碰撞时调用该方法/// </summary>/// <param name="collisionInfo">Collision info.碰撞事件</param>void OnCollisionExit(Collision collisionInfo ) {Rigidbody rigid = collisionInfo.rigidbody;//得到我们碰撞的刚体.float xDistance = rigid.position.x - transform.position.x;//碰撞的珠球与板条的水平距离,落到板条中间时,水平速度保持不变.rigid.velocity = new Vector3(rigid.velocity.x + xDistance, rigid.velocity.y, rigid.velocity.z);//刚体碰撞后的速度.}}
Block类:该脚本绑定到砖块Block中。
using UnityEngine;using System.Collections;public class Block : MonoBehaviour {/// <summary>/// 触发器/// </summary>void OnCollisionEnter () {        BreakoutGame.SP.HitBlock();        Destroy(gameObject);//删除组件}}


性能分析:


CPU Usage


GPU Usage


Rendering

Memory

Physics


心得体会:
这次的作业与上一次有很大的区别,需要两个人的分工和合作,这次的内容更加贴近我们兴趣和爱好,贴近实际应用,而且使用了当下流行的3d游戏引擎Unity 3D。
0 0
原创粉丝点击