Lets Make Dumb Pong! – 30/04/21

In todays programming session, we finally got rounf to creating a game and we done so by going back to basics! We started by making Pong.

Pong is a classic, simple game that features two paddles and a ball. The objective is to get the ball past the opponents paddle to score points. I started by making a new 2D project and using Unity’s built in sprites.

Once my layout was complete, it was time to start coding. I started off by coding the player paddle first, the player paddle had to feature movement by using a button press. This is my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed;
    private Vector3 movement;

    

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float moveVertical = Input.GetAxis("Vertical");

        movement = new Vector3(0, moveVertical, 0f);

        movement = movement * speed * Time.deltaTime;

        transform.position += movement;
    }
}

This essentially gets the vertical input axis which is the up and down arrows and converts the values into a float. I then use that float to create a new Vector3 for movement. Finally, I made the movement responsive to speed and real time.

Next, I started to code the ball movement. Once again, this is pretty straight forward. The ball is going to be moving and for that to happen, it needs a physics component. I attached a rigidbody to the ball and then wrote this code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BallManager : MonoBehaviour
{

    public Text playerScore;
    public Text cpuScore;

    private int playerGoal;
    private int cpuGoal;
    private float rand;

    private Rigidbody2D rb;
    private Vector3 spawn;

    // Start is called before the first frame update
    void Start()
    {
        rand = Random.Range(-6f, 6f);
        rb = GetComponent<Rigidbody2D>();

        rb.velocity = new Vector2(rand, rand);

        spawn = new Vector3(0, 0, 0);
    }

    // Update is called once per frame
    void Update()
    {
        playerScore.text = playerGoal.ToString();
        cpuScore.text = cpuGoal.ToString();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "PlayerGoal")
        {
            playerGoal += 1;
            Start();
            transform.position = spawn;
            Debug.Log("Player+1! || Ball Reset!");
        }

        if (collision.gameObject.tag == "CPUGoal")
        {
            cpuGoal += 1;
            Start();
            transform.position = spawn;
            Debug.Log("CPU+1! || Ball Reset!");
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Player" || collision.gameObject.tag == "CPU")
        {
            rb.velocity = rb.velocity * 0.2f;
        }
    }
}

Basically, this code detects when the ball has hit a goal boundary and resets the position of the ball back to the center. It will then re-run the start function to give the ball a random physics input.

Finally, the CPU pong paddle. This one is not yet complete but it is functional for now, I’m sure there are ways to make it better. Once again, as it is going to be moving, we need some sort of physics input so I used a Rigidbody again. This is the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CPUController : MonoBehaviour
{
    private Vector3 movement;
    private Rigidbody2D rb;

    public Transform ball;
    public float speed;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();   
    }

    // Update is called once per frame
    void Update()
    {
        
        movement = new Vector3(0, ball.localPosition.y, 0);

        rb.velocity = new Vector3(0, movement.y, 0);
    }
}

The code itself is small and simple. What I’ve done here is made a reference to the balls transform position. I have then referenced this in the Vector3 for movement on the Y axis.

That is pretty much it, the CPU Paddle is still a bit dumb so there is work to be done on that but for now, it’ll do.

When it comes to making games, I would love to learn to make an adventure type game that matches the likes of Super Mario Galaxy. I want to learn how to make the environments and the movement features in the game, including how the gravity works.

When it comes to programming, I don’t find much of it difficult really, its just trying to remember the components and how to code them correctly. Making sure all the references are in the right place.

When it comes to Year 2 and making a game, I want to make a Super Mario Galaxy like game featuring my character Zero, but I would also love to make a karting game. I’ve always been a fan of Mario Kart so to be able to make my own version or variant of this would be amazing.

itch.io

Leave a comment