Before you can work on the game loop for, you must initiate all of these new sprites—paddle, ball, and blocks—each of which has different attributes and properties. You also have to prepare the environment—the game surface—in which the sprites are used. Let’s start by changing the source files you opened in the previous section to prepare the way for your new game.
SpriteObject.java
needs an extra function to return the MoveX
and MoveY
values, which are the variables that store the horizontal and vertical velocity of the sprites. In this way, you can easily reverse them to cause the ball to switch directions. In other game types, you may want to check the speed of a sprite to make sure it isn’t going too fast.
Follow these steps:
- Add the following two methods to
SpriteObject.java
:public double getMoveY(){
return y_move;
}
public double getMoveX(){
return x_move;
}- You can make another change to
SpriteObject.java
to make the programming more convenient for you. Instead of worrying about theadj_mov
variable that keeps the game at a constant rate, let’s opted to let the game run as fast as it can. This avoids the hassle of dealing with very small movement values, and it adds unpredictability to an otherwise normal game. To make this change, go to theupdate()
function, and change the code to read as follows:public void update(int adj_mov) {
x += x_move;
y += y_move;
}
With those small corrections, you’ll have a much easier time working on the game loop. You see the pieces come together in the coming pages.
Your game can finally take shape once you work out your processes and updating in GameView.java
. Remember that this is where you store the code that changes the performance and functionality of the game. Here are the steps:
- Because this game doesn’t use the noises from the last game, remove these variable declarations from
GameView.java
:- Look through your code and remove any references to these elements, because they will produce errors.
- You also need to change the two sprite objects that your previous game used. The larger your games become, the more likely it is that you’ll be using an array of sprites. This game is no different, and later you work on ways to populate your array of blocks with an XML document. You can remove the sprites from the last chapter because you have no use for bombs in this game! Declare your new sprites in
GameView.java
:private SpriteObject paddle;
private SpriteObject[] block;
private SpriteObject ball;- Add the following variables, which you use to access the screen size when testing whether the ball touches the edges:
private int game_width;
private int game_height;Note If all this deleting and retyping is bothersome, you can download a blank Android project through this book’s website (
http://code.google.com/p/android-tablet-games/
). From there, you can create the game from scratch.- The constructor method of
GameView
must be completely redone to make your new app work. Listing 6-1 shows the new constructor method, followed by a brief explanation. Make sure your code is identical to that shown in Listing 6-1.public GameView(Context con) {
super(con);
context = con;
getHolder().addCallback(this);
paddle = new SpriteObject(BitmapFactory.decodeResource(getResources(), R.drawable.paddle), 600, 600);
block = new SpriteObject[3];
block[0] = new SpriteObject(BitmapFactory.decodeResource(getResources(),
R.drawable.block), 300, 200);
block[1] = new SpriteObject(BitmapFactory.decodeResource(getResources(),
R.drawable.block), 600, 200);
block[2] = new SpriteObject(BitmapFactory.decodeResource(getResources(),
R.drawable.block), 900, 200);
ball = new SpriteObject(BitmapFactory.decodeResource(getResources(), R.drawable.ball),
600, 300);
mGameLogic = new GameLogic(getHolder(), this);
createInputObjectPool();
setFocusable(true);
}- If you look back at the last project, this should look very familiar. The
soundPool
object is removed from the code, and you plug in new coordinates for the sprites when they’re originally rendered. Sometimes this can be tricky, so I like to create a blank image in GIMP that is the size of the screen (1280 × 1040). You can then gather the coordinates that look appropriate for your game.- The previous game involved three bombs, and here you basically replace them with three blocks. Obviously you want more blocks in the future, but this way you can reuse all of your
for
loops to cycle through the bricks. Because you’re familiar with sprite objects now, notice that the only things you have to change are the location of the sprites and the image resource to use.- You need to get the ball moving. The next function that must be changed is
surfaceCreated()
, which you can simplify with only a few changes to the ball function. You also add two lines to assign the height and width of the canvas or screen to variables for use in your update function. Add the code shown in Listing 6-2 to the project.@Override
public void surfaceCreated(SurfaceHolder holder) {
mGameLogic.setGameState(mGameLogic.RUNNING);
mGameLogic.start();
ball.setMoveY(-10);
ball.setMoveX(10);
Canvas c = holder.lockCanvas();
game_width = canvas.getWidth();
game_height = canvas.getHeight();
holder.unlockCanvasAndPost(c);
}- This starts the ball moving toward upper right, which should give the player plenty of time to track its movement and be ready to respond. If the starting speed you set here seems either too fast or too slow,
surfaceCreated()
is where you come back to change it, as you grab the speed from the ball object later on.- You also need to change the
onDraw()
function, but again it isn’t a very complicated change. The loop to draw all the bricks is identical to the one you used to update bombs previously. Override youronDraw()
function as shown in Listing 6-3.
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
ball.draw(canvas);
paddle.draw(canvas);
for(int i = 0; i < 3; i++){
block[i].draw(canvas);
}
}
You’ve dealt with the basics. Now you move on to adding some bells and whistles to your previous work on collisions and events.
18.217.29.234