Coding the apple
Let's start with the Apple
class by adding the required import
statements and the member variables. Add the code and study it, and then we will discuss it:
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import java.util.Random; class Apple { // The location of the apple on the grid // Not in pixels private Point mLocation = new Point(); // The range of values we can choose from // to spawn an apple private Point mSpawnRange; private int mSize; // An image to represent the apple private Bitmap mBitmapApple; }
The Apple
class has a Point
object that we will use to store the horizontal and vertical location of the apple. Note that this will be a position on our virtual grid, and not a specific pixel position.
There is a second Point
variable called mSpawnRange
as well that will eventually...