Rolando Andrade
Back to projects

Built in 2019

Pong

Pong Game

Pong preview

Pong HTML Canvas video-game.

What really happens when a ball bounces off a paddle? Most game tutorials never tell you. They start with a framework. Install Unity. Learn Godot. Pick up a physics engine. Those tools help you ship games faster, but they hide the basics.

I wanted to see the basics. In 2019, I built one of the simplest games there is with only a canvas element and vanilla JavaScript. No libraries. No build tools. Only drawing code and collision math.

That game was Pong. Two paddles, one ball, and more interesting problems than I expected.

One ball, two paddles

The game runs in the browser. A 500x300 pixel canvas shows the field. You move the left paddle with the arrow keys. A computer opponent moves the right paddle.

A ball starts in the center and bounces between the paddles. Miss the ball, and your opponent gets a point. The scores show as large numbers in the background. The ball gets a little faster after each hit, so the game gets harder over time.

The game loops at about 125 frames per second. Every frame clears the canvas, updates positions, checks collisions and draws everything again.

Drawing every frame again

The HTML canvas gives you a 2D drawing context. Every frame, the game fills the whole canvas with a dark background. That erases the last frame. Then it draws the paddles, the ball and the score on top.

I wrote my own shapes for rectangles and circles. Each shape knows its position, size and color. Its draw method makes all the canvas calls: it sets the fill style, draws the path and strokes the border.

This is immediate mode rendering. The DOM is the opposite, a retained mode system that keeps elements on the page. On a canvas, nothing stays between frames. Every element is drawn from zero on each tick. It sounds wasteful. For a simple game it works well, and it keeps the code easy to reason about.

Where the ball hits

The ball moves by adding its velocity to its position every frame. Two numbers make up the velocity, one for horizontal and one for vertical movement. When the ball hits the top or bottom wall, the vertical velocity flips. The ball bounces and keeps going.

Paddle hits are more interesting. When the ball touches a paddle, the horizontal velocity flips to send it back. I also change the vertical velocity based on where the ball hit. A hit near the edge sends the ball at a steeper angle. A hit in the center sends it straighter.

That angle comes from trigonometry. The distance between the ball and the paddle center goes into a cosine function. Now where you put the paddle matters, not only whether you block the ball.

Each paddle hit also raises the horizontal speed a little. Matches cannot last forever, and long rallies get tense.

An opponent that misses

The right paddle follows the ball. The rule is simple. If the ball is above the paddle, move up. If it is below, move down. Perfect tracking would be impossible to beat.

Random offsets make it miss. The computer compares positions with a small random buffer. Sometimes it starts moving too late. Sometimes it goes too far. The opponent feels more human, and you get a fair chance.

The difficulty grows on its own. A faster ball gives the computer less time to react, so its mistakes cost more.

A tick every 8 milliseconds

A JavaScript interval fires every 8 milliseconds. Each tick runs the full update. It clears the canvas, moves the computer paddle, checks collisions, updates the ball and draws everything.

Your paddle reacts to keyboard events outside that interval. A key press moves the paddle right away. Input stays responsive even if drawing slows down.

Collision checks run every frame before drawing. The ball checks its edges against both paddles and the walls. If it hits something, its velocity and position change before the frame is drawn. That way the ball never shows up inside a paddle.

Circles meet rectangles

To detect a collision, the game checks if a circle touches a rectangle. The ball keeps its center point and radius. Each paddle keeps its top left corner, width and height.

For a paddle hit, two things must be true. The ball’s edge crosses the paddle’s edge. The ball’s vertical range overlaps the paddle’s vertical range.

When the game detects a hit, it moves the ball to the paddle’s edge. A fast ball cannot get stuck inside the paddle.

Small game, real problems

Pong showed me that simple games hide real complexity. Bouncing a ball sounds easy until you meet the edge cases. What happens when the ball clips a corner? How do you stop tunneling, where a fast ball jumps through a paddle between two frames? Every physics simulation has these problems.

Without a framework, I had to learn the rendering cycle myself. Clear, update, draw. Every game engine runs on that cycle. Knowing it helps me see what engines do for me and when I need one.

The computer opponent showed how little intelligence a game needs. A few rules and some randomness look like a thinking player. People see intent in simple patterns.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build