用unity写一个3D版打砖块的游戏代码
时间: 2024-06-09 21:09:13 浏览: 12
以下是一个基本的Unity3D版打砖块游戏的代码示例。请注意,这只是一个基本的示例,您可以根据自己的需要和想象力进行修改和扩展。
```csharp
using UnityEngine;
using System.Collections;
public class Brick : MonoBehaviour {
public int hitsToBreak = 1;
public int points = 10;
public GameObject brickParticle;
private int timesHit;
void OnCollisionEnter(Collision other) {
timesHit++;
if (timesHit >= hitsToBreak) {
BreakBrick();
}
}
void BreakBrick() {
GameManager.Instance.Score += points;
Instantiate(brickParticle, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
public class Paddle : MonoBehaviour {
public float speed = 1f;
public float leftBound = -7.5f;
public float rightBound = 7.5f;
void Update() {
float horizontalInput = Input.GetAxis("Horizontal");
transform.position += Vector3.right * horizontalInput * speed * Time.deltaTime;
if (transform.position.x < leftBound) {
transform.position = new Vector3(leftBound, transform.position.y, transform.position.z);
}
if (transform.position.x > rightBound) {
transform.position = new Vector3(rightBound, transform.position.y, transform.position.z);
}
}
}
public class Ball : MonoBehaviour {
public float speed = 10f;
public float maxSpeed = 20f;
public float minSpeed = 5f;
public float speedIncrease = 1.1f;
public float sideHitExtraSpeed = 2f;
public float topHitExtraSpeed = 1.5f;
private Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
rb.velocity = Vector3.up * speed;
}
void OnCollisionEnter(Collision other) {
if (other.gameObject.CompareTag("Brick")) {
rb.velocity *= speedIncrease;
} else if (other.gameObject.CompareTag("Paddle")) {
float xHit = hitFactor(transform.position, other.transform.position, other.collider.bounds.size.x);
Vector3 direction = new Vector3(xHit, 1, 0).normalized;
rb.velocity = direction * (speed + sideHitExtraSpeed);
} else if (other.gameObject.CompareTag("Wall")) {
if (other.contacts[0].normal == Vector3.up) {
rb.velocity *= topHitExtraSpeed;
}
}
}
float hitFactor(Vector3 ballPos, Vector3 paddlePos, float paddleWidth) {
return (ballPos.x - paddlePos.x) / paddleWidth;
}
void Update() {
if (rb.velocity.magnitude > maxSpeed) {
rb.velocity = rb.velocity.normalized * maxSpeed;
} else if (rb.velocity.magnitude < minSpeed) {
rb.velocity = rb.velocity.normalized * minSpeed;
}
}
}
public class GameManager : MonoBehaviour {
public static GameManager Instance;
public int InitialLives = 3;
public int Score = 0;
private int lives;
private bool gameOver;
void Awake() {
Instance = this;
lives = InitialLives;
}
void Update() {
if (gameOver) {
return;
}
if (lives <= 0) {
GameOver();
return;
}
if (GameObject.FindGameObjectsWithTag("Brick").Length == 0) {
NextLevel();
}
}
void GameOver() {
gameOver = true;
Debug.Log("Game Over!");
// TODO: show game over UI
}
void NextLevel() {
Debug.Log("Next Level!");
// TODO: load next level
}
public void LoseLife() {
lives--;
Debug.Log("Lives: " + lives);
if (lives > 0) {
// TODO: show life lost UI
}
}
}
public class DeathZone : MonoBehaviour {
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag("Ball")) {
GameManager.Instance.LoseLife();
Destroy(other.gameObject);
}
}
}
```
以上代码包含了四个主要的组件:Brick,Paddle,Ball和GameManager。
Brick:这个组件控制砖块的行为,包括它需要被击中的次数,它会给玩家带来多少分数,以及在玩家命中时如何破坏它。
Paddle:这个组件控制玩家移动的挡板。 它响应水平输入,并确保挡板不会移动到边界之外。
Ball:这个组件控制球的行为,包括速度和碰撞响应。它将更快地移动,如果它击中挡板的侧面,那么它将斜向反弹。
GameManager:这个组件控制整个游戏的逻辑。它跟踪玩家的得分和生命,并在相关事件发生时触发GameOver或NextLevel。它也处理球掉落的情况,减少生命。
还有一个额外的DeathZone组件,它处理球掉落的情况,如果球掉落到了底部,那么玩家将失去一条生命。
请注意,这个代码示例只是一个基本示例,您可以根据自己的需求和想象力进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)