Create an easy variant of Pong in just a few minutes!
View the original Pong tutorial in GDScript here
Are you a beginner looking to learn Godot? Then look no further because in this guide I will walk you through step by step on how to make your first video game !
Disclaimer: Your game will not look like the picture in the thumbnail. This tutorial aims to be as simple as possible. This means that you may be introduced to some coding pracices that are not sustainable for long term. Please be aware that this guide aims only to cover the pure basics.
Let's begin by downloading Godot, if you haven't already. Make sure to download the .Net version
In this tutorial, I will be using Godot 4, but it should work in Godot 3 with a few modifications.
Create a new project. Name it "pong" and create a new folder. Select "compatibility" for the renderer, and hit "create and edit."
Add a new 2d Scene
Hit ctrl + s to save the scene and name it whatever
Next, we'll add something to our currently empty scene. right click the root node and hit "add child node"
Search for "CharacterBody2D" and create it.
Note: In Godot 3, it's called "KinematicBody2D".
Double click it and rename it to "player 1"
Next, add a ColorRect to the player. The hierarchy matters in Godot, so make sure you have player 1 selected when doing this.
Now, select the ColorRect and adjust it to resemble a long paddle. The height of the camera is given by a thin blue frame, so don't make it longer than that.
Now select player 1 again and add a CollisionShape2D as a child. This will add an area that defines the shape of the character body.
Now we need to assign the collision shape an actual shape. Look to the right and find the Shape property, and change it to a RectangeShape2D
Now take the paddles of the collision shape and adjust it so that it covers the entire rectangle.
Nice! Now we have a good setup for a pong paddle. Next click on player 1 and hit the "group select" button so we can move it freely without disrupting the childeren nodes.
To complete our setup, hit crtl + d to duplicate it and drag the new one over to the other side.
Hit ctrl + s to save the scene
Here comes the fun part- Code! We need to add functionality to the paddles.
click on player 1, and hit the attatch script button.
Under Language, select C#, and change the path to res://player_1.cs
Replace all the code with the following:
using Godot;
using System;
public partial class player_1 : CharacterBody2D //KinematicBody2D on Godot 3
{
public override void _PhysicsProcess(double delta)
{
if (Input.IsKeyPressed(Key.W))
{
Vector2 currentPosition = Position;
currentPosition.Y -= 5;
Position = currentPosition;
}
if (Input.IsKeyPressed(Key.S))
{
Vector2 currentPosition = Position;
currentPosition.Y += 5;
Position = currentPosition;
}
}
}
This code may look confusing, but all it does is make the paddle up or down when W or S is pressed
Let's make sure it all works! Hit the play button in the top right. When it asks you to select a main scene, hit Select Current
You'll notice that the left paddle should move when you hit W or S.
Remember how we attatched a script to player 1? Now we need to do the same for player 2. Just select player 2 and attatch a script, select C# language, and use this path: res://player_2.cs . Add this code in:
using Godot;
using System;
public partial class player_2 : CharacterBody2D //KinematicBody2D on Godot 3
{
public override void _PhysicsProcess(double delta)
{
if (Input.IsKeyPressed(Key.Up))
{
Vector2 currentPosition = Position;
currentPosition.Y -= 5;
Position = currentPosition;
}
if (Input.IsKeyPressed(Key.Down))
{
Vector2 currentPosition = Position;
currentPosition.Y += 5;
Position = currentPosition;
}
}
}
This is actually the exact same code as before, except W and S were swapped with the up and down arrow keys.
Try running the code again. This time, you should see that the left paddle and the right paddle can both move when you press their respective buttons.
Now it's time to add in the ball. Switch the tab back to 2d in the top. Click the root node, "Node2D", and add a new CharacterBody2D child. Rename it "ball".
Drag it in between the two paddles and add a ColorRect to it. Make it look like a small square. Then add a CollisionShape2D and set its shape to the RectangeShape2D, and adjust its margins to fit the color, just like we did with the paddles.
Attatch a script to ball just like we did with both paddles. Name the file res://ball.cs Replace the code with this:
using Godot;
using System;
public partial class ball : CharacterBody2D //KinematicBody2D for Godot 3
{
//use the following line on Godot 3
//Vector2 Velocity;
KinematicCollision2D collisionData;
public override void _Ready()
{
base._Ready();
Velocity = new Vector2(300, 0);
}
public override void _PhysicsProcess(double delta)
{
collisionData = MoveAndCollide(Velocity * new Vector2((float)delta, (float)delta));
if (collisionData != null)
{
//randomize the y value a little
RandomNumberGenerator r = new RandomNumberGenerator();
Vector2 tempVelocity = Velocity;
tempVelocity.Y += r.RandfRange(-20, 20);
Velocity = tempVelocity;
//bounce
Velocity = Velocity.Bounce(collisionData.GetNormal());
}
}
}
This is some lengthier code, but all it does is make the ball move and bounce when it hits stuff.
There you have it! This should now be a fully working pong clone. You may notice that the ball moves back and forth in a very dull manor.
You can fix this by adjusting
Velocity = new Vector2(300, 0);
to be more like
Velocity = new Vector2(300, 500);
and you'll see some more dynamic vertical movement.
That will, however require you to add in barriers below and above your scene! You can accomplish this by adding in a new StaticBody2D and giving it a CollisionShape2D, just like with the paddles, but adjust it to cover the out-of-bounds area. Or you can duplicate one of the paddles, detatch the script, and just turn it sideways so it behaves like a barrior. The choice is up to you!
Now that you've built a pong clone, you've taken one step forward in your journey of learning. Consider checking out some of my other guides!