Summer Break – Block Breaker Game in Unity, Part 1

Home / Technology / Game Development / Summer Break – Block Breaker Game in Unity, Part 1
It's week 3 of Summer of Code 2019. This week, I started developing a summer-themed classic arcade block breaker game in Unity. It features 2D colliders, triggers, rigidbody components, and more! Read more at blissfullemon.com

This week I had the opportunity to create one of those classic arcade block breaker games. It was so much fun, and I learned so many new useful bits about Unity and game design in general. Unsurprisingly, this project was much bigger than the ones from previous weeks. So, before I get too far ahead of myself, I need to address the elephant in the room.

My Revised Summer of Code Plan

I did not complete my goals this week. My original plan was complete the entire block breaker game this week. Unfortunately, as I mentioned last week, these projects are getting more and more complex, and thus take a considerable amount of time. I made the decision to push one of my Color My Life projects back a week so I could spend time on this, but that still wasn’t enough to give it all the time and attention it needs.

I can see the inevitable spiral of incomplete projects and unfinished goals if I try to stick to my original Summer of Code schedule. That’s why I’ve decided to revise things a bit.

It is no longer my goal to complete the entire Udemy course in the next 5 weeks. Instead, I am going to plan on spending 2-3 weeks on each of the remaining projects. That means I won’t get to TileVania, the 2D tilemap platformer game. That’s a little bit of a disappointment, but it’s nothing to get too upset about. Sometimes plans change, and that’s okay. Besides, Fall of Code has a nice ring to it, right?

Week 4: July 29 – August 4

  • Finish ‘Block Breaker’ section of Udemy course (🕒6h 14m)
  • Weekly blog post

Week 5: August 5 – 11

  • Begin ‘Laser Defender’ section of Udemy course (🕒5h 58m)
  • Weekly blog post

Week 6: August 12 – 18

  • Complete ‘Laser Defender’ section of Udemy course (🕒5h 58m)
  • Begin ‘Glitch Garden’ section of Udemy course (🕒10h 18m)
  • Weekly blog post

Week 7: August 19 – 25

  • Continue ‘Glitch Garden’ section of Udemy course (🕒10h 18m)
  • Weekly blog post

Week 8: August 26 – September 1

  • Complete ‘Glitch Garden’ section of Udemy course (🕒10h 18m)
  • Weekly blog post

Block Breaker Games

In case you aren’t familiar with the classic block-breaker style arcade game, the concept is simple. Each level has a number of breakable blocks somewhere towards the middle or top of the screen. The player uses a moveable paddle to hit a ball into the air. The ball bounces around, breaking or damaging blocks it comes in contact with, before falling back down towards the bottom of the screen. Then the player must move the paddle left or right to hit the ball and make it bounce back up. If they miss and the ball falls down off the screen, it’s game over. However, if they succeed, and all the blocks are successfully broken, the player wins and can move onto the next level.

There are also several different variations of this style of game. Some use multiple paddles while others use multiple balls. Even the block shape varies from squares, rectangles, and even circles as seen in this bubble block breaker game.

What I Learned

Despite the concept of the game being very simple, the actual mechanics behind it are a lot more complex than what I have been doing the past few weeks. I am only about halfway through the game, but in this time I have learned a ton of new things.

  • Exporting packages from other Unity projects and subsequently importing them in a new project
  • Unity world units and how that relates to the size of sprites and other objects that you place inside the world
  • Resizing sprites (in pixels per unit) so that they correspond to a specific number of world units
  • How to set up and modify colliders, including the Box Collider 2D, Circle Collider 2D, and Polygon Collider 2D
  • Making the ball bounce by adding a Physics Material 2D to the ball, then adjusting it to make it more or less bouncy
  • The difference between a collision and a trigger
    • Collisions are like car crashes, where objects bump into each other and something happens as a result
    • Triggers are like walking through an invisible barrier – you can pass through, but someone is going to be alerted that you are there (which may cause something to happen in return)
  • How to use the Scene Manager namespace to load a level using its string name, which should be done sparingly in case the name or desired order ever changes
  • How to move the paddle on the screen with the mouse by finding the position of the mouse as it moves, converting that to world units, and updating the position of the paddle based on where the mouse is
  • What a Vector 2 is and how to use it to specify a set of x and y coordinates
  • Using the Mathf.Clamp() function to limit the min and max range
  • Creating and using prefabs in Unity to be able to add multiple instances of the same game object and allow modifications of all the objects at one time

Block Breaker Game Design

The first thing I did was export the scenes from the Number Wizard UI, then import them into my block breaker game. After a bit of tweaking, along with modifying the fonts and backgrounds to go with the new theme, the main menu came together quite nicely.

I started developing a summer-themed classic arcade block breaker game in Unity. It features 2D colliders, triggers, rigidbody components, and more! Read more at blissfullemon.com
Main Menu of Summer Break block breaker game

Afterwards, I began to focus my attention on the actual game, starting with level 1, as shown below. I used a portion of the main menu background to create a more neutral background with the same theme so the blocks, paddle, and ball still stand out without getting lost among the rocks and trees.

I started developing a summer-themed classic arcade block breaker game in Unity. It features 2D colliders, triggers, rigidbody components, and more! Read more at blissfullemon.com
Level 1 of Summer Break block breaker game

The Ball

The ball used in this block breaker game is designed to look like a blue and yellow beach ball. It has a Dynamic Rigidbody 2D component attached, which allows it to be affected by gravity. This is what makes it fall back down from the sky after it is launched into the air. It also has a Circle Collider 2D component attached with an additional physics material applied. The collider causes a collision event when the ball hits the blocks, paddle, or walls/ceiling. The physics material makes the ball to bounce when the collision occurs.

When the level starts, the ball is stuck to the paddle, regardless of where the player moves the paddle to on the screen. Once the player clicks the left mouse button, the ball is launched into the air.

Ball.cs

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

public class Ball : MonoBehaviour
{
    // Configuration parameters
    [SerializeField] Paddle paddle1;
    [SerializeField] float xPush = 2f;
    [SerializeField] float yPush = 15f;

    // State
    Vector2 paddleToBallVector;
    bool hasStarted = false;

    // Start is called before the first frame update
    void Start()
    {
        paddleToBallVector = transform.position - paddle1.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (!hasStarted) 
        {
            LockBallToPaddle();
        }

        LaunchOnMouseClick();
    }

    private void LockBallToPaddle()
    {
        Vector2 paddlePosition = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
        transform.position = paddlePosition + paddleToBallVector;
    }

    public void LaunchOnMouseClick()
    {
        if (Input.GetMouseButtonDown(0)) // 0 = primary button (left)
        {
            hasStarted = true;
            GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush); 
        }
    }
}

The Paddle

The block breaker paddle contains a Polygon Collider 2D component, which, like the ball’s collider component, allows it to trigger a collision event when it comes into contact with the ball. It is not affected by gravity, so it does not have a rigidbody component. It is, however, restricted in movement to only the x axis within the confines of the screen.

Paddle.cs

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

public class Paddle : MonoBehaviour
{
    [SerializeField] float screenWidthInUnits;
    [SerializeField] float minX = 1f;
    [SerializeField] float maxX = 15f;

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

    // Update is called once per frame
    void Update()
    {
        float mousePositionInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
        Vector2 paddlePosition = new Vector2(transform.position.x, transform.position.y);
        paddlePosition.x = Mathf.Clamp(mousePositionInUnits, minX, maxX);
        transform.position = paddlePosition;
    }
}

The Blocks

Level 1 contains 217 green and pink blocks. It is a block breaker game, after all. I created a prefab using a single white block, then created 2 additional prefab variants. One was changed to the color green while the other was changed to the color pink. Aside from the color, however, all of the blocks are exactly the same.

Like the paddle, they do not have a rigidbody component because they are not affected by gravity. I could potentially add one and adjust the settings to allow the blocks to fall slowly to the bottom, increasing the difficulty of the game. But for now they are static. They contain a Box Collider 2D that creates a collision event when the ball hits them. For now, this event causes the block that is hit to be immediately destroyed.

Block.cs

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

public class Block : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Destroy(gameObject);
    }
}

Invisible Game Objects (Outside of Playspace)

Although you cannot see them, the game also features 4 game objects located outside of the playspace. Along the top, left, and right side are walls which contain Box Collider 2D components. This allows the ball the trigger a collision event with the wall, which then keeps the ball in play and causes it to bounce away. There is also an invisible game object along the bottom of Along the bottom of the screen that contains a Box Collider 2D component. However, this one is set up as a trigger. It allows the ball to pass through entirely but triggers a script to execute that ends the level and loads the Game Over screen.

LoseCollider.cs

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

public class LoseCollider : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        SceneManager.LoadScene("Game Over");
    }
}

Next Week

Next week, I will be finishing up the Summer Break block breaker game. I have not looked ahead to see what all that entails, but I do know there will be additional levels, a bit of refactoring, and a bit of tweaking to the game mechanics. For example, right now the ball gets stuck bouncing back and forth forever because the gravity is turned down so low, so that will need to be adjusted.

I am also hoping to figure out how to properly build the game for the web and get it up somewhere on my website so you can play around with it and test it out. If I can’t figure it out next week, I will most likely include it in my portfolio once the game is finished, polished, and ready to present. 😊

If you have your own block breaker game that you have created, I would love to check it out (even if it’s just a video of you playing). Leave your link in the comments below. Also, if you enjoy reading content like this, be sure to subscribe below to get alerts in your inbox when I post something new. ❤


Join 1,467 other subscribers
I started developing a summer-themed classic arcade block breaker game in Unity. It features 2D colliders, triggers, rigidbody components, and more! Read more at blissfullemon.com

Let's chat!

%d bloggers like this: