Book Image

Building Games with Flutter

By : Paul Teale
Book Image

Building Games with Flutter

By: Paul Teale

Overview of this book

With its powerful tools and quick implementation capabilities, Flutter provides a new way to build scalable cross-platform apps. In this book, you'll learn how to build on your knowledge and use Flutter as the foundation for creating games. This game development book takes a hands-on approach to building a complete game from scratch. You'll see how to get started with the Flame library and build a simple animated example to test Flame. You'll then discover how to organize and load images and audio in your Flutter game. As you advance, you'll gain insights into the game loop and set it up for fast and efficient processing. The book also guides you in using Tiled to create maps, add sprites to the maps that the player can interact with, and see how to use tilemap collision to create paths for a player to walk on. Finally, you'll learn how to make enemies more intelligent with artificial intelligence (AI). By the end of the book, you'll have gained the confidence to build fun multiplatform games with Flutter.
Table of Contents (17 chapters)
1
Part 1: Game Basics
5
Part 2: Graphics and Sound
11
Part 3: Advanced Games Programming

Moving a sprite around the screen

Now that we have the George sprite animating, we will build on that and change the animation based on the direction of travel:

  1. Let's add some variables at the top of the George class to store the animations and direction information:
      late SpriteAnimation georgeDownAnimation, 
        georgeLeftAnimation, georgeUpAnimation, 
        georgeRightAnimation;
      double elapsedTime = 0.0;
      double georgeSpeed = 40.0;
      int currentDirection = down;
      static const int down = 0, left = 1, up = 2, 
        right = 3;
  2. Below the onLoad function, define a new function called changeDirection, which we will call every 3 seconds to change George's direction randomly:
      void changeDirection() {
        Random random = Random();
        int newDirection = random.nextInt(4);
        switch (newDirection...