Book Image

Learning Java by Building Android Games

By : John Horton
Book Image

Learning Java by Building Android Games

By: John Horton

Overview of this book

Table of Contents (17 chapters)
Learning Java by Building Android Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The coordinate system


In the previous chapter, we drew all our game objects directly to points on the screen, and we used real screen coordinates to detect collisions, bounces, and so on. This time, we will be doing things slightly differently. This is partly out of necessity, but as we will see, collision detection and keeping track of our game objects will also get simpler. This might be surprising when we think about the potential of our snake to be many blocks long.

Keeping track of the snake segments

To keep track of all the snake segments, we will first define a block size to define a portion of a grid for the entire game area. Every game object will reside at an (x,y) coordinate, based not on the pixel resolution of the screen but on a position within our virtual grid. In the game, we define a grid that is 40 blocks wide, like this:

//Determine the size of each block/place on the game board
 blockSize = screenWidth/40;

So we know that:

numBlocksWide = 40;

The height of the game screen in...