Skip to main content

Pong

Lately I have been overwhelmed with my current project so I thought it would be a good time to take a step back and do something simple. For anyone who doesn’t know, pong is basically a simulated table-tennis game. It was one of the first video games, so I thought it would be a good place to start this blog.
I decided to use Unity to make the game. The coding for it was all pretty simple. The ball has a variable for its speed in the x and y direction. Every frame it calls a function that increments its current position by these variables.
        There are six surfaces the ball can hit. These are the top wall, bottom wall, right wall, left wall, and one of the two paddles. If it hits the left or right wall that is a score and the game resets. If it hits the top wall, bottom wall, or one of the paddles it bounces off and goes in the opposite direction. I did this by reversing the perpendicular speed variable. For example, if it hits the top or bottom wall the y speed is reversed and if it hits a paddle the x speed is reversed.
The balls x speed is reversed after hitting the paddle
         To make the game more interesting, if the paddle is moving up it increases the balls y speed and if it is moving down it decreases it. I handled all of these collision rules with tags. Every surface has a different tag that gets returned when the ball collides with it. A switch statement then uses the returned tag to determine what to do.


I programmed one paddle to be controllable by the player and the other to be controlled by a simple AI. The x speed of the ball is always going to equal the absolute value of whatever you set it to since the only thing that changes is the direction. The y speed on the other hand will become slower and faster since it will change when it hits a moving paddle. The AI paddle will only follow the ball, it cannot predict if it is going to bounce off the top or bottom. If you increase the y speed enough and hit the ball towards a wall the AI paddle will move towards the wall and not be able to catch the ball when it bounces off and you will win the round. 

          
         I embedded the game below for you to try. You will need to download the Unity web player and give it permission to run. Don't worry its safe, quick and easy. When you get the game running move the paddle up and down with the W and S key. Press space to start a round.


Comments

Popular posts from this blog

Water Effect

This purpose of this post is to give some insight in to my 2d water effect. The effect  was made in unity and consist of one C# script and two shaders. Additionally this effect can be be broken down in to two parts. The reflection and the distortion. First we will look at the reflection Reflection A lot of the work of the reflection is done through the C# script. The script creates a new game object with the sprite renderer of the original object, mirrors it, and puts our water reflection shader on it. You can see it below. A couple things to note with this technique. First, everything done in this script could be done manually through the editor. It is only for convenience and does not have to be done at run time. You might want to add [ExecuteInEditMode] to this script if you plan on using it. Second, this is kind of a crude way of doing things. Animations, lighting, or anything else that effect your sprite will not get reflected. For animations you could use the s...

Chromatic Aberration

I made a chromatic aberration shader after not finding one I liked online. You can see the effect above on the sprite bottle to the right. My goal was to make a stylized chromatic aberration effect that I could apply to any sprite. The effect works by separating the rgb channels and displacing them in different directions. Below you can see the blue and red channels getting displaced in opposite directions on the x axis.  You can control the displacement easily with a script to change how extreme the effect is. I personally think the effect looks best when used subtlety but I could see some cool applications were it is controlled dynamically and combined with a screen shake or something to show a large impact. Here is the effect applied more subtlety on a cool rogue sprite Here is a link to the shader Here is an example C# script to control it

Random Map Generator

A lot of games use Perlin Noise to generate maps, so I decided I would give it a try. One of the most popular games to do this is Minecraft which uses a similar noise function to create its map.  Landscape generated in MineCraft The basic premise is that you use the noise to create a height map on a 2d grid. Every point on a grid is assigned a random value and that value is its height. If you where to just use a normal random number function to assign the values they would be all over the place. It wouldn't make any natural looking shapes. That is why Perlin Noise is used. It creates smooth gradients between random points. This make the height changes less jarring and look natural. Perlin noise vs white noise  Unity already has a Perlin noise function so I did not have to implement my own. All I did at first was make a 2d array and assign each index a height value. Every height above a cutoff is land and everything below is water, this is the result. It was...