Managing Game Outcomes

When the player loses the game and a boat hits the castle, you call reset(). This is a simple and quick function.

Follow these steps:

1. Add the code from Listing 9-19 below the other functions in GameView.

Listing 9-19. reset() Method

private void reset(){
        for(int i = 0; i < boat_count; i++){
                boat[i].setstate(boat[i].DEAD);
        }
        boat_count = 0;

}

All you do is destroy the boats. This, in effect, restarts the game, because the boats are randomly created once again. You don’t remove the cannons because there is no need to worry about them. The user can delete them if they wish. If you want to display a message to the user, you can create a sprite and draw it onscreen at this point. In the update() function, have it wait for 30 or so cycles, and then remove the message.

2. The createBullet() method is a bit more involved, as you can see in Listing 9-20, but it’s definitely manageable. Put this method directly below the reset() function.

Listing 9-20. createBullet() Method

private void createBullet(int x, int y, int direction, int r){
        if(r >= 0){
                int index = available_bullet[r];
                if(direction == bullets[index].RIGHT){
                        bullets[index].setMoveX(10);
                        bullets[index].setMoveY(0);
                        bullets[index].setX(x);
                        bullets[index].setY(y);
                        bullets[index].setstate(bullets[index].ALIVE);
                }
                if(direction == bullets[index].LEFT){
                        bullets[index].setMoveX(-10);
                        bullets[index].setMoveY(0);
                        bullets[index].setX(x);
                        bullets[index].setY(y);
                        bullets[index].setstate(bullets[index].ALIVE);
                }
                if(direction == bullets[index].UP){
                        bullets[index].setMoveY(-10);
                        bullets[index].setMoveX(0);
                        bullets[index].setX(x);
                        bullets[index].setY(y);
                        bullets[index].setstate(bullets[index].ALIVE);
                }
                if(direction == bullets[index].DOWN){
                        bullets[index].setMoveY(10);
                        bullets[index].setMoveX(0);
                        bullets[index].setX(x);
                        bullets[index].setY(y);
                        bullets[index].setstate(bullets[index].ALIVE);
                }
        }

}

The bullet sprites are symmetrical, so you don’t have to worry about their orientation, only the direction of their movement. Don’t forget the last line of each if block, which makes the bullets alive. Otherwise, they will never be drawn, and you’ll have trouble figuring out what went wrong.

You’ve finally finished the game project. The next section gives you some ideas for future plans.

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

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