160 – 3D Pong

For the rest of the year, I’m going to highlight some impressive final projects in Computer Programming and BC Calculus.
The computer programming students have had the majority of the term to work on their projects and one of the requirements was to write a blog post about their experience.
Here’s a crosspost of a implementation of 3D Pong in processing. Neat idea, and although there are some bugs, really fun:

The New and Improved Pong (Now in the Third Dimension)

Making the game, I personally thought was pretty fun. Given, there were a couple of rough patches, but what code doesn’t have any rough patches? In the end, the game turned out almost exactly like I wanted it to. Just pong (because who doesn’t like pong) in the third dimension with a couple of other game variables added in. There were a couple of things I would’ve liked to add in like optional two-player mode, but in the end, I hope whoever plays it gets a little bit of enjoyment out of it.

Old Pong                                            My Pong

This is the original version of pong (to the left) versus my first person version(to the right) next to each other so you can really see the difference of the two up close.

Playing the Code

If you don’t have processing downloaded on your computer yet, here is a link to the game here on openprocesing.com. Though the game does work a little bit better by just running it in processing. (also, when you’re in processing, you can change the size of the game and it’ll change corresponding to the size). So, you can just copy the code from the link.

Controls

The controls are quite simple. You can simply move the player paddle by using the W, A, S, and D keys. Then you can change the AI paddle speed/difficulty by using the Up and Down arrow keys while you are playing. I just used some booleans that would become true if the key was pressed and false if it was released. I did this same code for the other keys too.

void keyPressed()
//code recognizing that "W" is being pushed
{
 //setting the keys
 if (key == 'W' || key =='w'){
 r = true;
 }
}
void keyReleased()
//code recognizing that "W" is not being pushed
{
 //again setting the keys
 if (key == 'W' || key == 'w'){
 r = false;
 }
}

Problems with the code

There were a couple (probably more than a couple) of problems that I ran into during this project like I had previously mentioned. One of the biggest problems was when the ball would randomly hit any of the walls it would get stuck on that wall but could still move forward and backward. So the X ad Y coordinates of the ball were locked in place. I ended up fixing it by putting method in whenever the ball hit one of the walls:

void changingspeed()
{
 boolean ballspeedneg = false;
 boolean ballxspeedneg = false;
 boolean ballyspeedneg = false;
//these lines are making the next x, y, or z speed negative if the previous one was also negative
//so that the ball doesn't change direction out of nowhere when it hits a wall
 if (ballspeed < 0){
 ballspeedneg = true;
 }
 if (ballxspeed < 0){
 ballxspeedneg = true;
 }
 if (ballyspeed < 0){
 ballyspeedneg = true;
 }
 if (ballspeed > 0){
 ballspeedneg = false;
 }
 if (ballxspeed > 0){
 ballxspeedneg = false;
 }
 if (ballyspeed > 0){
 ballyspeedneg = false;
 }
//these three functions are pretty much finding the x, y, and z speed of the ball depending on the level
 ballxspeed = getballxspeed(level);
 ballyspeed = getballyspeed(level);
 ballspeed = getballzspeed(level);
 if (ballspeedneg == true){
 ballspeed *= -1;
 }
 if (ballxspeedneg == true){
 ballxspeed *= -1;
 }
 if (ballyspeedneg == true){
 ballyspeed *= -1;
 }
}

What these lines of code do is first make a boolean true if the ball’s speed was negative. Then, it would use some functions to figure out what the ball’s speed should be because it changes corresponding to the level. Then if the boolean said that the previous speed was negative it multiplies the new speed by -1 (because it turned out the ball’s speed was constantly being changed from negative to positive so it would stay in the same place).

Level System of the Game

To change the level in the game, all I did was first make a variable (I used “bounces”) that would increase by one whenever it hit the player’s paddle like so:

if (ballz >= 270)
 {
 //ball only bounces off paddle
 backward = true;
 forward = false;
 if (ballx >= paddlex - 75 && ballx <= paddlex + 75 && bally <= paddley + 40 && bally >= paddley - 40){
//counts the number of times the paddle has succesfully deflected the paddle
 bounces += 1;
 ballspeed *= -1;
 changingspeed();
}

Then there was a method I made called “levelincrease” that would change the level according to how many times the ball has been succesfully deflected off of the player’s paddle

void levelincrease()
//this is increasing the level as the number of succesful bounces increase
{
  if (bounces > 1){
    level = 2;
  }
  if (bounces > 2){
    level = 3;
  }
  if (bounces > 3){
    level = 4;
  }
  if (bounces > 4){
    level = 5;
  }
  if (bounces > 5){
    level = 6;
  }
  if (bounces > 6){
    level = 7;
  }
  if (bounces > 7){
    level = 8;
  }
  if (bounces > 8){
    level = 9;
  }
  if (bounces > 9){
    level = 10;
  }
  else{
    level = level;
  }
}

This part of the code is in fact very easy to change, so if you wanted to, you could substitute the numbers shown and make levels longer or shorter depending on what you wanted. I also made the level you are currently on be displayed on the top part of the screen while you are playing the game.

Project picture Andrew Shaw

 

Hardest part of the Project

Easily the hardest part of the project was utilizing the two commands;

pushMatrix();
//and 
popMatrix();

These two lines of code close off the translations for each shape you are adding in to the game. So, for example if you want to translate a cube 3 units to the left, without using the two commands, you would just end up moving the whole virtual world 3 units to the left. It also didn’t help that I did not understand this until half way through the project (mostly why it was so difficult for me). So basically to negate this movement of the whole world when you only intend for one shape to move, you just have to put “pushMatrix();” before you translate and fill a shape, and then “popMatrix();” once you draw the shape afterwards.

Switching to 2 Player Mode

Switching it to two player mode was not hard at all. All I did was put an “if” statement into the 2nd paddle’s code that says if “twoplayer” is true than, the second paddle uses the arrow keys and if  “nottwoplayer” is true than the paddle automatically follows to the balls location without leaving its “z” coordinate.

 if (start == true && level != 0 && nottwoplayer == true)
 {
 }

 if (start == true && level != 0 && twoplayer == true)
 {
 }

Although there isn’t a seperate mode or gametype for each of the game styles, while you are in game, pressing the “p” button will change to two player and pressing “o” will change to single player.

Reasons for this Project

Pong is usually seen as an “easy” or “simple” project, because it completely is. But, I like the idea of the game and how much you can change it. So for this project, I knew I wanted to do something in 3D, but I didn’t want to do something that could have some very large problems because I would probably not know how to fix them considering I am still relatively new to the whole “coding in more than 2 dimensions” area. So I did pong because people are familiar with the idea, it would be understandable to manipulate on larger scales, there are unlimited things you can do with this game, and its fun! Then, I chose to use processing because I knew the language very well, and its the only program that can run 3D things that we learned about this year. I also just like the format of it. But overall, I thought this was a really fun project to do, and I hope that others can have as much fun with it as I did.

By: Andrew Shaw

3 years ago

Leave a Reply

Your email address will not be published. Required fields are marked *