Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter flutter logo flame

flame - a minimalist flutter game engine.
flame – a minimalist flutter game engine.

any help is appreciated! comment, suggestions, issues, pr’s! give us a star to help!

goals

the goal of this project is to provided a complete set of out-of-the-way solutions for the common problems every game developed in flutter will share.

currently it provides you with: a few utilities, images/sprites/sprite sheets, audio, a game loop and a component/object system.

you can use whatever ones you want, as they are all somewhat independent.

usage

just drop it in your pubspec.yaml:

dependencies:
  flame: ^0.9.5

and start using it!

bellow is an overview that should suffice to build a simple game, and work your way up from there.

the flame-example game has been updated to use the newer apis (0.8.2) on a new branch.

there is a very good quickstart tutorial for version 0.6.1 here. the api has changed a lot, so refer this documentation for updated information. soon i plan to release an updated tutorial.

structure

the only structure you are required to comply is a assets folder with two sub folders: audio and images.

an example:

  flame.audio.play('explosion.mp3');

  flame.images.load('player.png');
  flame.images.load('enemy.png');

the file structure would have to be:

.
└── assets
    ├── audio
    │   └── explosion.mp3
    └── images
        ├── enemy.png
        └── player.png

don’t forget to add these files to your pubspec.yaml file:

flutter:
  assets:
    - assets/audio/explosion.mp3
    - assets/images/player.png
    - assets/images/enemy.png

modules

the modular approach allows you to use any of these modules independently, or together, or as you wish.

audio

to play an audio, just use the flame.audio.play method:

    import 'package:flame/flame.dart';

    flame.audio.play('explosion.mp3');

you can pre-load your audios in the beginning and avoid delays with the loadall method:

    // in a async prepare function for your game
    await flame.audio.loadall(['explosion.mp3']);

images

if you are using the component module and doing something simple, you probably won’t need to use these classes; use spritecomponent or animationcomponent instead.

if you want to load an image and render it on the canvas, you can use the sprite class:

    import 'package:flame/sprite.dart';

    sprite sprite = new sprite('player.png');

    // in your render loop
    sprite.render(canvas, width, height);

note that the render method will do nothing while the image has not been loaded; you can check for completion using the loaded method.

component

this class represent a single object on the screen, being a floating rectangle or a rotating sprite.

the base abstract class has the common expected methods update and render to be implemented.

the intermediate inheritance positioncomponent adds x, y, width, height and angle to your components, as well as some useful methods like distance and anglebetween.

the most commonly used implementation, spritecomponent, can be created with a sprite:

    import 'package:flame/components/component.dart';

    sprite sprite = new sprite('player.png');

    const size = 128.0;
    var player = new spritecomponent.fromsprite(size, size, sprite); // width, height, sprite

    // screen coordinates
    player.x = ... // 0 by default
    player.y = ... // 0 by default
    player.angle = ... // 0 by default

    player.render(canvas); // it will render only if the image is loaded and the x, y, width and height parameters are not null

every component has a few other methods that you can optionally implement, that are used by the basegame class. if you are not using the base game, you can alternatively use these methods on your own game loop.

the resize method is called whenever the screen is resized, and in the beginning once when the component is added via the add method. you need to apply here any changes to the x, y, width and height of your component, or any other changes, due to the screen resizing. you can start these variables here, as the sprite won’t be rendered until everything is set.

the destroy method can be implemented to return true and warn the basegame that your object is marked for destruction, and it will be remove after the current update loop. it will then no longer be rendered or updated.

the ishud method can be implemented to return true (default false) to make the basegame ignore the camera for this element.

there are also other implementations:

  • the animationcomponent takes an animation object and renders a cyclic animated sprite (more details about animations here)
  • the parallaxcomponent can render a parallax background with several frames
  • the box2dcomponent, that has a physics engine built-in (using the box2d port for dart)

game loop

the game loop module is a simple abstraction over the game loop concept. basically most games are built upon two methods:

  • the render method takes the canvas ready for drawing the current state of the game.
  • the update method receives the delta time in milliseconds since last update and allows you to move the next state.

the class game can be subclassed and will provide both these methods for you to implement. in return it will provide you with a widget property that returns the game widget, that can be rendered in your app.

you can either render it directly in your runapp, or you can have a bigger structure, with routing, other screens and menus for your game.

to start, just add your game widget directly to your runapp, like so:

    main() {
        game game = new mygameimpl();
        runapp(game.widget);
    }

instead of implementing the low level game class, you should probably use the more full-featured basegame class.

the basegame implements a component based game for you; basically it has a list of components and repasses the update and render calls appropriately. you can still extend those methods to add custom behavior, and you will get a few other features for free, like the repassing of resize methods (every time the screen is resized the information will be passed to the resize methods of all your components) and also a basic camera feature (that will translate all your non-hud components in order to center in the camera you specified).

a very simple basegame implementation example can be seen below:

    class mycrate extends spritecomponent {

        // creates a component that renders the crate.png sprite, with size 16 x 16
        mycrate() : spritecomponent.fromsprite(16.0, 16.0, new sprite('crate.png'));

        @override
        void resize(size size) {
            // we don't need to set the x and y in the constructor, we can set then here
            this.x = (size.width - this.width)/ 2;
            this.y = (size.height - this.height) / 2;
        }
    }

    class mygame extends basegame {
        mygame() {
            add(new mycrate()); // this will call resize the first time as well
        }
    }

input

in order to handle user input, you can use the libraries provided by flutter for regular apps: gesture recognizers.

however, in order to bind them, use the flame.util.addgesturerecognizer method; in doing so, you’ll make sure they are properly unbound when the game widget is not being rendered, and so the rest of your screens will work appropriately.

for example, to add a tap listener (“on click”):

    flame.util.addgesturerecognizer(new tapgesturerecognizer()
        ..ontapdown = (tapdowndetails evt) => game.handleinput(evt.globalposition.dx, evt.globalposition.dy));

where game is a reference to your game object and handleinput is a method you create to handle the input inside your game.

if your game doesn’t have other screens, just call this after your runapp call, in the main method.

download the full project for this post from the following button

this source is fully free for all time

download as zip


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.