in this series | |
---|---|
Breakout | |
Bouncing balls | |
Hurt me plenty | |
Breakout
All right, this is my first tutorial series, so be nice :D
In this tutorial I will try to develop al simple clone of Breakout using Duality. Being a quite simple game concept, it shouldn’t be too difficult to implement, at least at a basic level.
Despite this being a basic tutorial, I will assume the following:
- you have already seen Duality and Dualitor (the Duality Editor)
- you can navigate your way around them
- you know how to add a GameObject or a Component
- you know the basics of C# and of coding
For this tutorial, no extra resources are needed other than what is already present inside Duality.
But first things first, a quick and extremely simplified overview of Breakout: the goal of the game is to break a wall of bricks using a ball, while steering a paddle around to stop the ball from leaving the game area. The ball moves using a constant motion and can bounce on the bricks, the walls of the game area, and the paddle. Whenever a brick is hit by the ball, it gets damaged and when its “health” is depleted, it is removed from the game; when all the bricks have been removed, the level is clear and the game moves to the next one. If the ball goes out of the game area, the players loses one life and a new ball is put into play. When all lives are gone, the game is over.
So let’s start and see where it takes us; fire up Dualitor and create a new project called “Breakout”: you will see something like this (results may vary, depending on your version)
The first thing we need for our game, is the ball.
Add a
Finally, add a
So click
The next thing to do, is to fix the
Switch the view to the
![]() |
![]() |
Finally, going back to the
At this point, we still only have our falling ball, which is quite amusing to see for the first couple times, but gets boring quite quickly, so let’s add a shiny paddle.
Add both a
- set
Rect to[-50, -10, 100, 20] - set
SharedMaterial toDefault\Material\SolidWhite (grab it from theProject View ) - set
ColorTint to a nice orange color (RGB255, 150, 0 )
.. and voilà! we have our
Again, add a
We are almost done.. now go back to the
One more time, press
One thing that I forgot to mention, is that in Breakout each time the paddle hits the ball, this would make the ball go a little faster, making more difficult to catch it the next time it would come down. To do this, we would need to have a Restitution higher than 1 (i.e. the RigidBody will get back more energy than what it had during collision) but since this is not possible to do with the editor, we will have to get our hands dirty with some coding and, in this particular case, we will make a Component in charge of managing the collisions of the Ball with the other actors in the game.
Open the Source Code by clicking on the second button of the toolbar; once Visual Studio (or your IDE of choice) is started, rename class “
What we want to do is to verify if the Ball hits our Paddle and, in this case, give it a little extra push to make it go faster than what it was before. We will accomplish this by using
Your code should look like this:
using Duality; | |
using Duality.Components.Physics; | |
namespace Breakout | |
{ | |
public class Ball : Component, ICmpCollisionListener | |
{ | |
void ICmpCollisionListener.OnCollisionBegin(Component sender, CollisionEventArgs args) | |
{ | |
} | |
void ICmpCollisionListener.OnCollisionEnd(Component sender, CollisionEventArgs args) | |
{ | |
} | |
void ICmpCollisionListener.OnCollisionSolve(Component sender, CollisionEventArgs args) | |
{ | |
if (args.CollideWith.Name == "Paddle") | |
{ | |
this.GameObj.GetComponent<RigidBody>().ApplyLocalImpulse(-Vector2.UnitY); | |
} | |
} | |
} | |
} |
Compile and, if everything went well, you will now be able to add the
Again compile, and go back to the editor: you should already see the new property in the
This is the final code, for reference:
using Duality; | |
using Duality.Components.Physics; | |
namespace Breakout | |
{ | |
public class Ball : Component, ICmpCollisionListener | |
{ | |
public float PaddleImpulse { get; set; } | |
void ICmpCollisionListener.OnCollisionBegin(Component sender, CollisionEventArgs args) | |
{ | |
} | |
void ICmpCollisionListener.OnCollisionEnd(Component sender, CollisionEventArgs args) | |
{ | |
} | |
void ICmpCollisionListener.OnCollisionSolve(Component sender, CollisionEventArgs args) | |
{ | |
if (args.CollideWith.Name == "Paddle") | |
{ | |
this.GameObj.GetComponent<RigidBody>().ApplyLocalImpulse(-Vector2.UnitY * PaddleImpulse); | |
} | |
} | |
} | |
} |
And that’s all for the first part of this tutorial. In a few days I will hopefully have the rest ready :D