Pong

Games


Difficulty

Objective

Students will be able to know how object detection works by using conditional “IF…THEN” statements.


Pong

(synopsis) Learn about hit detection and have fun coding this remake of the classic Pong game, the grandmother of all video games!

The Code

Here we will go over each function and explain what it is doing so you can understand and change the code as you want.

Functions

Initialize

In our initialize function, we want to set up all of our sprites which would be our player we control, the opponent player and the ball. We will limit the players to only go up and down since it wouldn’t be pong if it was going across the screen, and we want to make sure the ball and the players don’t go anywhere we can’t see them. Here we will also set the score and display it so we can keep track of who is winning.

Countdown

We don’t want the game to start immediately. This function will set a short delay on the game start.

Ball Control

Setting Ball Speed

Here we will set the ball velocity to a random speed using Math functions. The first two blocks set the Variable of X and Y to the but it is the last blue block that actually sets the speed of the ball.

Bounce Ball

When the ball hits one of the players, we want it to bounce back instead of continuing to keep going in the same direction. In this case, we take the “SetBallSpeed” function and then reverse the X value which is the horizontal direction. Let’s add a sound aswell to let us know when it reaches each side

TestBall

In this function, we will check if the ball hits a player or if it goes past. if it hits the player, it will simply bounce, but, if it goes past, we want to end the game if it goes past a certain score (in this case 1).

Reset Ball

Lastly, for the ball, we want to reset it back to the center instead of a random position after a point is scored. We use the screen width and screen height divided by 2 so it is directly in the middle.

Player 2

We want our competition to actually try to win so it is more of a challenge, so, this function will update the second player’s position for us instead of needing another person. We actually use the ball’s coordinates so the second player more reliably will match or get close to where the ball will go.

Background

This will change the background to set a dashed line in the middle of the screen. technically, this function isn’t as needed as the others and can be changed for your own desire.

StartRound

Another small function. This will be used to start the game and puts the “ResetBall” and “RunCountdown” functions together for later use.

Putting the code together

If you are new to programming, all we wrote so far are just Functions. Functions are like shortcuts. Instead of rewriting the same lines every time something happens, we can write a function that can be called later. As such we want to call certain functions at certain times.

On Start

When the game starts, we want to draw the background, initialize all of our sprites, and start the round. This will only be called the first time the brainpad boots up.

On Game Update

While the game runs, these blocks will always be running. This is going to be the Main” function that actually controlls the game. As stated before, there’s not alot here since all the functions have that code in them and the game update just calls them.

Click on the button below to go to the project in MakeCode that we just went through.

Play Solo Pong!

Back to Lessons & Projects


Related Projects

Chicken Run

Help the chicken find her eggs. Inspired by frogger game

Helicopter Ride

Use your helicopter to fly your team back to the base

Particle Effects

Showing off the built in particle effect

Newsletter

Get Social
License
Creative Commons License
Except where otherwise noted, BrainPad provided material by GHI Electronic is licensed under a Creative Commons Attribution 4.0 International License.
Latest Press
December 19, 2019
October 11, 2019
September 26, 2019
August 23, 2019
August 7, 2019
August 5, 2019