Just another BlockBreaker, with an in-game pause menu and lives

Hey and thanks for clicking!

This is just my go at the BlockBreaker game. I have to admit, I’ve learned a lot from the video and I think we all agree that this course is amazing! But it also really inspired me to try doing some things on my own and outside the scope of the videos. So here are a few improvements that you can try to build yourself:

  • Started by adding the lives system, which carry over from level to level (so no new lives when you finish a level).

  • Added the Pause Meniu. I guess this is by far the largest and most extensive one. It’s not that hard to implement and there are miriads of such tutorials online. There are a few tricks to getting it to work in this context but it was a lot of fun to figure them out an I have to say I learned a lot from doing it. Not guaranteeing, though, that it’s the right way to do it and something of the sorts is probably implemented in later videos (I only now finished the BlockBreaker part of the course so no idea really what’s ahead)

  • Added Toggle buttons for the Music and Sounds in the Pause Meniu. Pretty simple in theory but requires a little trickery to get them to keep changes you’ve made in pevious Levels/Scenes.

Try Game Here
http://gamebucket.io/game/e9e5aa65-6c0b-441e-b0f5-36c71bdf7478

So thanks again for reading and/or trying the game and hope you have fun and learn a lot from the course!

Lorena

1 Like

The pause screen is really cool! I quite like it. The only problem I’ve found (which happened when I tested a pause button too) is that you can use a pause function to effectively cheat in BlockBreaker since the paddle jumps to the mouse position immediately.
Still, the pause menu is really cool!
The music and sound are also nice.

Keep up the great work!

WOW! Pause menu is cool! So is your paddle!

Hey! Thank you very much! It means a lot to hear kind words! You’re right though, the resume button did break the gameplay a little. I do believe I’ve solved the problem (somewhat) in this new version though, even if just by adding the possibility of playing with the arrow keys + space bar as well as mouse (which is a bit of a cop out, I know. But still, why not I guess). Heck, you could even use them at the same time :smile:

http://gamebucket.io/game/938303d5-93ba-4c1a-8702-521832a2a61d

Problem is, all was fine in Unity, but I now realize that gamebucket puts the game in the left of the browser and that can break it a bit since there’s no more room for the mouse to move that way at some point. But still, it was worth a shot I guess.

Thanks you very much once again!

Hi and thank you so very much! :smiley: I was actually a bit afraid that the paddle looked a bit weird and a tad out of place honestly. So your comment really makes me feel better! It’s just a sprite I created from scratch in GIMP (which is quite new to me) but I’m happy to share it or even just the creation process if it helps any!

Hi, would you mind sharing your code - I’m trying to add lives to my game but I can’t get it to work. Thanks, Vince

Hey, there! I’d be happy to share! So here goes:

The biggest chunk of modifications would be in the LevelManager script:

LevelManager.cs

public static int maxNrOfLives = 3;  
public Text livesText;
public Ball ballInstance;

private static int nrOfLives = maxNrOfLives;

void Start()
{
    if (livesText)      //Can be null on Start and Lose screens
    {
        livesText.text = "Lives: " + nrOfLives;
    }
}

public void LoadLevel(string name)
{
    //On restart of entire game (comming back from LoseScreen and hit Play again)
    if (name.CompareTo("Level 1") == 1)
    {
        nrOfLives = maxNrOfLives;
        Brick.breackableCount = 0;	//Make certain brickCount is 0 after losing game
    }

    SceneManager.LoadScene(name);
}

public void LoseLife()
{
    nrOfLives--;
    if (nrOfLives <= 0)
    {
        LoadLevel("Lose Screen");
    }else
    {
        ballInstance.reset();    //if we still have lives left, reset ball to paddle
        livesText.text = "Lives: " + nrOfLives;
    }
}

Notes on this:

  • Theoreticaly, you might be better off making a brand new GameObject with a new script called LivesManager or something of the sorts and move all of this new code there. However, for simplicity in this case (and hopefully making it easier to understand and adapt) I’ve decided to stick within the confines of what we were given in the course.

  • I’ve made the nrOfLives attribute static as I want the changes in the nr of lives left to be reflected over all the levels. (i.e. if you start with 3 lives and you lose a life in level 1, you still only have 2 lives left in the next level). If that’s not the behaviour you’re looking for, you can simply make it not static and omit the ‘if’ statement in the LoadLevel method

  • The livesText and ballInstance will have to be dragged and drop in the editor on every level. Since we don’t display the lives on start and lose screens, for instance, (but we do have a LevelManager there too) the text field and ball instance can be left as blank.
    (SideNote: This is one of the reasons why a new GameObject might be better, since you could add that strictly to actual level scenes and get the ballInstance and textField programmaticaly, with no need for drag and drop.)

  • The method LoseLife needs to be public, as it will be called from the LoseCollider script.

LoseCollider.cs

public class LoseCollider : MonoBehaviour {

	private LevelManager levelManager;

	void Start()
	{
		levelManager = GameObject.FindObjectOfType<LevelManager>();
	}

	void OnCollisionEnter2D(Collision2D collision)
	{
		levelManager.LoseLife();
	}
}
  • The LoseCollider now simply transmits the lose of a life to the LevelManager above, and lets it decide whether the game has ended or not.

Ball.cs

public void reset()
{
    gameStarted = false;
  //  Debug.Log("Ball reset");
}

As gameStarted is the boolean which tells us if the game has started, i.e if the ball should be attached to the paddle or not, we want to reset this value once we lose a life.


And that’s about it for the lives system. If you’re adding code to prefabs, don’t forget to hit the Apply button once everything is dragged and dropped.

Hope this helps and good luck with your game! If you have any more questions or need any more help with this code, do let me know.

Lorena

Thanks so much Lorena!

Vince

1 Like