The snake is now functionally complete, so we turn our attention to building an interesting environment for the snake to live in. All of our game objects use the same positioning method we saw when designing the snake.
Filename: SNPlayfieldLayer.m
-(CGPoint) positionForRow:(NSInteger)rowNum andColumn:(NSInteger)colNum { float newX = (colNum * gridSize) - 2; float newY = (rowNum * gridSize) - 4; return ccp(newX, newY); }
This method takes the designated row and column values and multiplies them by the gridSize
. The additional modifiers (-2 and -4) are used to better align the walls so there is an equal size of partial walls on the outside edges of the screen. This is because the gridSize
value of 22
does not exactly fit the dimensions of the iPhone screen. With this slight adjustment, it looks visually centered after we add the outer walls.