PONG(2D)

游戏介绍:

制作一个2D对战游戏(pc),游戏由2个玩家控制各自的板子击打小球如果未能接到对方打过来的球则对方得一分。

运行截图:


GameObject:

Main Camera:摄像头
GameManager:包含rightWall,leftWall,upWall,downWall作为围墙保证小球不会飞出地图
BG:用于bgm的播放
Player1、Player2:玩家通过w,s(up、down)来控制的板子来完成和小球的互动
ball:游戏小球
Canvas:承载ui控制显示分数
EventSystem:实现reset键

Scripts:

Ball:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {
    private Rigidbody2D rigidbody2D;

    void Start() {
        rigidbody2D = GetComponent<Rigidbody2D>();
        GoBall();
    }

    //控制小球的运动速度保证不会低于9
    void Update() {
        Vector2 velocity = rigidbody2D.velocity;
        if (velocity.x < 9 && velocity.x > -9 && velocity.x != 0) {
            if (velocity.x > 0) {
                velocity.x = 10;
            }
            else {
                velocity.x = -10;
            }
        }
    }

    //小球碰撞到板子和墙壁的情况
    void OnCollisionEnter2D(Collision2D collision) {
        if (collision.collider.tag == "Player") {
            Vector2 velocity = rigidbody2D.velocity;
            velocity.y = velocity.y / 2 + collision.rigidbody.velocity.y / 2;
            rigidbody2D.velocity = velocity;
        }
        if (collision.gameObject.name == "rightWall" || collision.gameObject.name == "leftWall") {
            GameManager.Instance.ChangeScore(collision.gameObject.name);
        }
    }

    //使得小球动起来
    void GoBall() {
        int number = Random.Range(0, 2);//随机向一方出球
        if (number == 1) 
            rigidbody2D.AddForce(new Vector2(100, 0));
        else 
            rigidbody2D.AddForce(new Vector2(-100, 0));
    }

    //小球归位重置方法
    public void Reset() {
        transform.position = Vector3.zero;
        GoBall();
    }
}


Wall:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wall : MonoBehaviour {

    private AudioSource audio;

    void Start() {
        audio = GetComponent<AudioSource>();
    }
    //与小球碰撞时候发出声音
    private void OnCollisionEnter2D() {
        audio.Play();
    }
}


PlayerContorller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    public KeyCode upKey;
    public KeyCode downKey;
    public float speed = 10;
    private Rigidbody2D rigidbody2D;
    private AudioSource audio;
 
	void Start () {
        audio = GetComponent<AudioSource>();
        rigidbody2D = GetComponent<Rigidbody2D>();
	}
	
    //控制板子移动仅限y轴
	void Update () {
        if (Input.GetKey(upKey)) {
            rigidbody2D.velocity = new Vector2(0, speed);
        }else if (Input.GetKey(downKey)) {
            rigidbody2D.velocity = new Vector2(0, -speed);
        }
        else {
            rigidbody2D.velocity = new Vector2(0, 0);
        }
	}


    //与小球碰撞时候发出声音
    void OnCollisionEnter2D() {
        audio.pitch = Random.Range(0.7f, 1f);
        audio.Play();
    }
}

GameManager:
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour {

    private static GameManager _instance;
    public static GameManager Instance {
        get {
            return _instance;
        }
    }

    private BoxCollider2D rightWall;
    private BoxCollider2D leftWall;
    private BoxCollider2D upWall;
    private BoxCollider2D downWall;
    private int score1;
    private int score2;

    public Transform player1;
    public Transform player2;
    public Text score1Text;
    public Text score2Text;

    void Awake() {
        _instance = this;  
    }

    //初始化游戏场景
    void Start () {
        ResetWall();
        resetPlayer();
	}
	
    //初始化围墙
    void ResetWall() {
        rightWall = transform.Find("rightWall").GetComponent<BoxCollider2D>();
        leftWall = transform.Find("leftWall").GetComponent<BoxCollider2D>();
        upWall = transform.Find("upWall").GetComponent<BoxCollider2D>();
        downWall = transform.Find("downWall").GetComponent<BoxCollider2D>();

        Vector3 tempPosition = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
        upWall.transform.position = new Vector3(0, tempPosition.y + 0.5f, 0);
        upWall.size = new Vector2(tempPosition.x * 2, 1);

        downWall.transform.position = new Vector3(0, -tempPosition.y -0.5f, 0);
        downWall.size = new Vector2(tempPosition.x * 2, 1);

        rightWall.transform.position = new Vector3(tempPosition.x+0.5f,0,0);
        rightWall.size = new Vector2(1,tempPosition.y * 2);

        leftWall.transform.position = new Vector3(-tempPosition.x - 0.5f, 0, 0);
        leftWall.size = new Vector2(1, tempPosition.y * 2);
    }

    //初始化player的位置
    void resetPlayer() {
        Vector3 player1Position= Camera.main.ScreenToWorldPoint(new Vector3(100, Screen.height / 2, 0));
        player1Position.z = 0;
        player1.position = player1Position;
        Vector3 player2Position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width- 100, Screen.height / 2, 0));
        player2Position.z = 0;
        player2.position = player2Position;

    }

    //计分的实现
    public void ChangeScore(string wallName) {
        if (wallName == "leftWall") {
            score2++;
        }
        else if (wallName == "rightWall") {
            score1++;
        }
        score1Text.text = score1.ToString();
        score2Text.text = score2.ToString();
    }

    //重置比分和小球
    public void Reset() {
        score1 = 0;
        score2 = 0;
        score1Text.text = score1.ToString();
        score2Text.text = score2.ToString();
        GameObject.Find("Ball").SendMessage("Reset");
        resetPlayer();
    }
}
    







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值