- Learning Java by Building Android Games
- John Horton
- 277字
- 2021-07-23 19:02:29
Coding the boom method
Add the new highlighted code inside the boom method and then we will go through what just happened.
// This code says "BOOM!" void boom(){ gameView.setImageBitmap(blankBitmap); // Wipe the screen with a red color canvas.drawColor(Color.argb(255, 255, 0, 0)); // Draw some huge white text paint.setColor(Color.argb(255, 255, 255, 255)); paint.setTextSize(blockSize * 10); canvas.drawText("BOOM!", blockSize * 4, blockSize * 14, paint); // Draw some text to prompt restarting paint.setTextSize(blockSize * 2); canvas.drawText("Take a shot to start again", blockSize * 8, blockSize * 18, paint); // Start a new game newGame(); }
Most of the previous code is the same type of code as the draw
method. In the previous code, we set blankBitmap
to be viewed.
gameView.setImageBitmap(blankBitmap);
Fill the screen with a single color (in this case Red).
// Wipe the screen with a red color canvas.drawColor(Color.argb(255, 255, 0, 0));
Set the color to use for future drawing.
// Draw some huge white text paint.setColor(Color.argb(255, 255, 255, 255));
Set the text size to be quite large (blockSize * 10
)
paint.setTextSize(blockSize * 10);
Draw a huge "BOOM" in roughly the centre of the screen.
canvas.drawText("BOOM!", blockSize * 4, blockSize * 14, paint);
Make the text size smaller again for a subtler message underneath "BOOM!".
// Draw some text to prompt restarting paint.setTextSize(blockSize * 2);
Draw some text to prompt the player to take their first shot of the next game.
canvas.drawText("Take a shot to start again", blockSize * 8, blockSize * 18, paint);
Restart the game ready to receive the first shot.
// Start a new game newGame();
There is one final task. We need to draw the location of the player's shot on the grid.