Prev Up Next
Go backward to Preparation
Go up to Top
Go forward to Setting the Ball's Initial Position

Variable Declarations

As you may have guessed, Pong has a good bit in common with the bouncing ball applet you have seen in class. Your code will mimic the bouncing ball applet in many ways. At the same time, there are some important differences. You will get into trouble if you ignore these difference. For example:

With all these warnings out of the way, you can get a good start by copying the variables used to keep track of the ball's position and its velocity from the bouncing ball program into your applet. Just as in the bouncing ball applet, you will need variables to keep track of the x and y coordinates of the ball's current position and the rate at which each of these are changing. We might as well stick with the variable names used in the bouncing ball applet for these values: x, y, xspeed, and yspeed.

These variables will be shared by several methods in your applet so their declarations should appear before all method declarations at the start of the applet itself.

The numbers used to describe the ball's speed in the x and y directions are likely to have fractional parts. As a result, you can not use the type "int" that has been appropriate for variables that stood for numbers in all your earlier labs. Instead, use the type "double". Unfortunately, the name "double" doesn't suggest that it corresponds to real numbers in quite the way that "int" suggests integers. This awful name refers to the fact that a variable of type "double" requires twice as much memory. It is a remnant of a day (not so long ago) when computers had very small amounts of memory and programmers had to be careful not to waste what was available.

So, despite the lousy name, declare all four variables as type "double".


Prev Up Next