Implementing a simple game
In the first example application of this book, we will see how the canvas elements come together by building the graphical elements of a snake game (for a history of this game, see the Wikipedia entry at https://en.wikipedia.org/wiki/Snake_(video_game_genre). The main element of this game is a snake character that the user will control as it moves around the screen. We will build the snake from a row of rectangles and add animation elements to bring it to life. Let's start by drawing the initial screen.
Drawing a snake on screen
To start the work of displaying the game canvas, we will see create a simple snake that consists of a row of 10 green squares. Let's begin:
- Firstly, we will create a setup function that will build the game screen. We will call this function
setupGame
and create an empty list that we will populate. The return from this method is a container with no layout so that we can later use a manual layout for the visual...