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

2d Book Distortion

The game I am working on allows the player to read and create magic books. Because of this, I decided it would be useful to create different effects that look liked they were happening on the book's pages. To do this I needed a normal map. I figured a book would not be too hard to model so I made this model in blender. Book Model The model didn't have to be perfect, because I only need it to render a normal map viewing it from the top. Normal Map That is all the work I needed to do in blender.  The art for the actual book is just for testing and I found it on open game art  here . I fudged with the normal map and the book art a little bit in gimp until they lined up nicely. The rest of the work I did in unity. Everything that  I wanted to appear on the pages I put on a separate layer, that I will be referring to as the page layer. This way I could render that layer with a separate camera. I ended up rendering that to a render texture that was applied...