How to Create a 2D Snake Game in Flutter
Learn how to build a 2D Snake game in Flutter for both Android and iOS platforms. You will learn how to use Flutter as a simple game engine by creating the classic 2D Snake Game. You will get to know the basics of 2D game graphics and to control objects on the screen. By Samarth Agarwal.
Who is this for?
This course is for beginner developers who wish to learn implementation of graphics, motion and game logic in Flutter without using any third-party libraries, purely Flutter.
Covered concepts
- Use Flutter as a 2D game engine
- Move objects
- Control movement
- Build game UI
- Add game elements
Part 1: How to Create a 2D Snake Game in Flutter
Get introduced to Flutter as a 2D game engine. Learn about the capabilities of Flutter that allow it to be used for 2D game development.
Learn about the basics of 2D rendering in Flutter and the widgets that are often used to place objects on the screen as a specific position.
Learn about the code provided as a part of the starter project and also learn about the Piece class, ControlPanel and ControlButton classes, Direction enum and a few other utility methods that will be used during the development.
Learn to create a random position on the screen for the Snake to spawn according to the device’s screen size and then spawn a Piece on the screen at the generated position.
Learn to write the code to change the Piece positions such that it appears as if the Snake is moving.
Learn to write the code to render the Snake as a sequence of Pieces on the screen.
Learn to implement a timer to rebuild the UI continuosly thereby creating a perception of movement of the Snake on the screen.
Learn to write the code to change the direction of movement of the Snake using the provided ControlPanel widget.
Learn to write the code to render the food on the screen, again using the Piece widget.
Learn to write the code to allow the Snake to eat the food and then re-render the food at a new random position on the screen.
Learn to limit the Snake’s movement within the screen area to prevent the Snake from moving off of the screen.
Learn to display the game over dialog if the Snake collides with the game boundaries.
Learn to add the feature to restart the game when the game is over.
Learn to write the code to display the score on the top right corner of the screen.