Preparing the Game Environment

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.

Modifying SpriteObject.java

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:

  1. Add the following two methods to SpriteObject.java:
    public double getMoveY(){
            return y_move;
    }
    public double getMoveX(){
            return x_move;
    }
  2. You can make another change to SpriteObject.java to make the programming more convenient for you. Instead of worrying about the adj_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 the update() 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.

Modifying GameView.java

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:

  1. Because this game doesn’t use the noises from the last game, remove these variable declarations from GameView.java:
    private SoundPool soundPool;
    private int sound_id;
    private int ID_robot_noise;
    private int ID_alien_noise;
    private int ID_human_noise;
  2. Look through your code and remove any references to these elements, because they will produce errors.
  3. 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;
  4. 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;

    images 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.

  5. 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.

    Listing 6-1. GameView Constructor

    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);
    }
  6. 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.
  7. 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.
  8. 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.

    Listing 6-2. surfaceCreated() Function Override

    @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);

    }
  9. 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.
  10. 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 your onDraw() function as shown in Listing 6-3.

Listing 6-3. onDraw() Function Override

@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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.29.234