teaching machines

Pong

December 19, 2011 by . Filed under cs491 mobile, fall 2011, postmortems.

For the final project, I had decided to implement the game of pong. Whoever gets a score of 10 first wins! I implemented this game with the idea that one would want to play against the computer, not another person. For this reason, I made a fairly simple method that made the Phone’s paddle follow the ball along the Y-Axis (this method was only called if it was the “Phone’s Turn”. For the player’s paddle, I created a onTouchEvent within the GameView class (which extends surface view). This method was fairly simple, because it doesn’t matter where on the screen you touch. The only thing that matters is the Y-axis of where your finger is. I had originally planned on making the paddle itself an onTouchListener, however I then realized that it did not matter where you were touching the surface of the phone.

Once either you or the phone’s score has reached 10, there will be a  message displayed letting you know if you have either won or lost. At this time, the items on the screen stop moving, indicating that the game has also stopped.

 

You can also choose your difficulty for this game.. Normal, Difficult, or Impossible. These difficulty levels decide the speed of the Phone’s paddle and of the ball.

 

One of the biggest things I was concerned about when designing this application was the orientation on the screen. When I had to place the ball and both paddles onto the screen, I had to give it x and y values to show where they needed to go. Normally, this isn’t a big deal. However, because there are so many Android devices with different screen sizes, I thought I should research if there was a line of code that gave you the dimensions of the screen. I used the following:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

From this code, I was able to get both the screen height and width, and based all placements off of these 2 measurements.

When I had first thought up this idea. I had only thought I would need to classes: GameActivity and GameView. However, I also implemented my own personal thread, a preferences class and a PongState class, where everything about the game is developed.

The GameActivity really doesn’t do much. The main purpose for it being there is to set up the menu and to set up the view of the game.

I even tried to implement my own physics for the game. Depending on where the ball is hit on the paddle, it goes different directions.