Fri
Apr 3rd

Let’s Make a Game 2: Food

This article is part 2 of our Let’s Make a Game series.  Remember you’ll need to view this on the site rather than your feed reader in case the code examples don’t appear.

Today I’m going to show you how to add a game board and food:

This tutorial is a little bit shorter than the previous one because I’m attending GitLondon!  Message me on Twitter (@alex_young or @quiteuseful) if you’re going.

Snakes Need Food

The core mechanic in Snake is collecting food.  Each piece of food grows the snake, making it more difficult to navigate without crashing.

The code I featured yesterday demonstrated how to extend the length of the snake.  It turns out that growing a snake is very similar to handling movement.  That means most of the work towards handling food is done.

A Game Board

The game board is simply a bag of arrays containing the positions of game items like food.  Eventually I’ll show you how to add maps, but for now let’s just add food:

Food is another use of the GameItems that make up the snake.  The board is also going to handle collision detection by comparing the position of one item with another.

Notice that sneaky remove_food_at method.  This will remove items from the game board.  Removing food will work like this:

  • The item will be deleted from the food items array
  • The item will then be hidden from view

Shoes allows you to hide shapes using the hide method.  That means GameItems now need a remove method, like this:

Putting it Together

I’ll add a random sprinkling of food to the board to give you something to play with.  Next up: snakes need rules!

Comments (View)
Related Posts Widget for Blogs by LinkWithin