Step 12

Adding the getPlayer function to CanvasPanel


Awesome! Now that our player can move, we need to get it to eat food and get bigger. However, before we can do that, we need to add some code to our “CanvasPanel.”

Open up the “CanvasPanel” file and add the code as shown. This will allow the “CanvasPanel” to give information about the “Player” to other classes that need it.

Completing the Food collision function


Next, open up the “Food” file where we can now complete the collision function we made before.

In here, we get the bounds of the player and if the bounds of the food intersects with it the player grows!

Testing the code


Now that we’ve finished all that code, let’s test it out! If done correctly, the Player should be able to eat any food that appear in the game, and grow after eating!

If not, go back and double check your code for any errors you may have made.