//说明:原文上传时间:2014-11-20 地址:http://blog.sina.com.cn/s/blog_6e302d8b0102v4du.html
开发语言:C#
开发平台:Unity3D4.5.5f1
开发人员:
王士溥:负责3D部分代码编写以及软件分析
朱俊彰:负责基本代码编写以及测试
计划:
开发时间:2周
功能简介:3D打砖块小游戏,实现基本功能,在游戏中可以通过鼠标调节camera视角以及位置——左键+移动鼠标-水平移动;右键+移动鼠标-摄像头倾斜角度;滚轮-缩放镜头。
游戏画面:
游戏基本代码:
//Ball.cs
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);
}
}
//Block.cs
using UnityEngine;
using System.Collections;
public class Block : MonoBehaviour {
/// <summary>
/// 触发器
/// </summary>
void OnCollisionEnter () {
BreakoutGame.SP.HitBlock();
Destroy(gameObject);//删除组件
}
}
//BreakoutGame.cs
using UnityEngine;
using System.Collections;
public enum BreakoutGameState { playing, win, lost };//定义游戏状态.
public class BreakoutGame : MonoBehaviour
{
public static BreakoutGame SP;
public Transform ballPrefab;//珠球.
private int totalBlocks;//总的方块数.
private int blocksHit;//已经击打的方块数.
private BreakoutGameState gameState;//游戏状态.
public float ZoomSpeed = 10;
public float MovingSpeed = 0.5f;//移动速度.
public float RotateSpeed = 1;//旋转速度.
public float distance = 5;
void Awake()
{
SP = this;
blocksHit = 0;
gameState = BreakoutGameState.playing;
totalBlocks = GameObject.FindGameObjectsWithTag("Pickup").Length;//得到所有的方块数.
Time.timeScale = 1.0f;//设置传递时间为1 .
//SpawnBall();
}
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>
void SpawnBall()
{
Instantiate(ballPrefab, new Vector3(1.81f, 1.0f , 9.75f), Quaternion.identity);//实例化珠球.
}
/// <summary>
/// 界面的渲染
/// </summary>
void OnGUI(){
GUILayout.Label("作者:朱俊璋、王士溥");
GUILayout.Space(10);//添加空格.
GUILayout.Label(" 己击打: " + blocksHit + "/" + totalBlocks);
//游戏开始/
if (GUI.Button(new Rect(0,150,80,30),"开始游戏")) {
SpawnBall();
}
if (gameState == BreakoutGameState.lost)
{
GUILayout.Label("你输了!");
if (GUILayout.Button("重新加载"))
{
Application.LoadLevel(Application.loadedLevel);//重新加载关卡.
}
}
else if (gameState == BreakoutGameState.win)
{
GUILayout.Label("你赢了!");
if (GUILayout.Button("重新加载"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
/// <summary>
/// 击打砖块
/// </summary>
public void HitBlock()
{
blocksHit++;
if (blocksHit%10 == 0) //每击打十个砖块生成新的珠球.
{
SpawnBall();
}
if (blocksHit >= totalBlocks)//游戏胜利.
{
WinGame();
}
}
/// <summary>
/// 珠球掉落
/// </summary>
public void LostBall()
{
int ballsLeft = GameObject.FindGameObjectsWithTag("Player").Length;//获得剩余珠球数.
if(ballsLeft<=1){
SetGameOver();//游戏结束.
}
}
/// <summary>
/// 游戏胜利
/// </summary>
public void WinGame()
{
Time.timeScale = 0.0f; //设置游戏暂停.
gameState = BreakoutGameState.win;
}
/// <summary>
/// 游戏失败
/// </summary>
public void SetGameOver()
{
Time.timeScale = 0.0f; //设置游戏暂停.
gameState = BreakoutGameState.lost;
}
}
//Paddle.cs
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public float moveSpeed = 20;
void Update () {
//获取水平方向,得到移动距离.
float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
transform.position += new Vector3(moveInput, 0, 0);
//控制板条移动范围.
float max = 14.0f;
if (transform.position.x <= -max || transform.position.x >= max)
{
float xPos = Mathf.Clamp(transform.position.x, -max, max); //板条移动的x坐标范围在-max和max之间.
transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
}
}
/// <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);//刚体碰撞后的速度.
}
}
开发展示:
性能分析:
感想:
这次作业与第一次作业有很大区别,这次实现的内容跟接近实际并且运用了当下流行的跨平台开发工具Unity3D。在开发过程中遇到了难题,如游戏中视角的移动来展示3D的效果。但最后我们两个人合力完成了这个小型项目,我也意识到了合作的重要性。后期可以继续拓展游戏的可玩性如增加关卡内容;增加游戏美观度。