DEV Community

Cover image for PONG - The Hello World of Game Development - The 2nd month
PythonIsNotASnake
PythonIsNotASnake

Posted on

PONG - The Hello World of Game Development - The 2nd month

In the second month of my game coding journey I had not very much time to invest in the learning process. Nevertheless I implement the first game of the 20 games challenge. Surprise! It was PONG. One of the very first games from the year 1972.
The concept is very simple but contain most of the essential functions you need in game development. There are two players. Each of them can only move up or down. The goal to achieve is to let the ball bounce on your bar and hope that your opponent don't do the same.
The whole project I present in the following lines can be checkout on github. For this project I have used Godot in the version 4.2.1.

Preparation

First of all I started to create some assets. For the whole game I needed two bars as players, one ball, the game field, the goals and walls. To make it a little eye catcher I decided to use same colors. After I had the assets the process of game development started.

Building scenes

The structure of the project is apart in different scenes. First of all I have a player_one and a player_two scene. Both of them are identical. The only different is the sprite. In general there are solutions to not duplicate the scene in the way I had done it. But to keep it simple I made it this way. Next there is the ball as a separate scene. Further in a separate directory are the last scenes goal and level.

Goal

As goal I used an Area2D as scene. In this scene I put a Sprite2D and a CollisionShape2D. To keep it so simple as possible the Area2D has a signal. This is the body_entered signal to track if the ball enter the collision shape of the goal. If the ball enter this area the game will be reset so the players must remember the result by their self.

Player

The player scenes are from type CharacterBody2D and are nearly as simple as the goal is. Each player consists of a Sprite2D and a CollisionShape2D. In the script are functions to enable the movement of the players. For each player are different keys defined to move up and down. Because I use two separate scenes for the players I need a player script which exports essential variables like the speed so no player have an advantage in movement.

Ball

The ball is a CharacterBody2D too. It consists like the other scenes a Sprite2D and a CollisionShape2D. To make the ball bounce if it collide with any object in the game world the move_and_collide() function will be needed. This function returns collision info. If the info is true the bounce() function of the velocity will be triggered. So the ball can bounce between the two players and don't stuck in the walls of the court.

Level

The level is a Node2D which consists all previous scenes and combine them. To show a background as court for the game a CanvasLayer with a TextureRect is used. This is only for the aesthetic and feeling and have no effect to the game logic nor the physics. Next a TileMap allows to place some squares as the wall. The wall is needed so the ball can bounce on it and don't leave the screen and disappears in the Nirvana. Additional the two players, the ball and the two goals are brought into position. The game can start and two players can enjoy the nostalgic charm of the 70s.

Conclusion

I hope I could help you a bit with my first GODOT game. Probably you stuck at some point and found a solution. If you have code PONG by yourself share your code with the community. My code can be discovered on github.
Open to discuss different approaches to solve the challenge to code PONG.
Have fun and keep coding.

Top comments (0)