flame 1.7.1 flame: ^1.7.1 copied to clipboard
A minimalist Flutter game engine, provides a nice set of somewhat independent modules you can choose from.
import 'dart:math' as math;
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/game.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
GameWidget(
game: MyGame(),
),
);
}
/// This example simply adds a rotating white square on the screen.
/// If you press on a square, it will be removed.
/// If you press anywhere else, another square will be added.
class MyGame extends FlameGame with TapCallbacks {
@override
Future<void> onLoad() async {
add(Square(size / 2));
}
@override
void onTapDown(TapDownEvent event) {
super.onTapDown(event);
if (!event.handled) {
final touchPoint = event.canvasPosition;
add(Square(touchPoint));
}
}
}
class Square extends RectangleComponent with TapCallbacks {
static const speed = 3;
static const squareSize = 128.0;
static const indicatorSize = 6.0;
static Paint red = BasicPalette.red.paint();
static Paint blue = BasicPalette.blue.paint();
Square(Vector2 position)
: super(
position: position,
size: Vector2.all(squareSize),
anchor: Anchor.center,
);
@override
void update(double dt) {
super.update(dt);
angle += speed * dt;
angle %= 2 * math.pi;
}
@override
Future<void> onLoad() async {
super.onLoad();
add(
RectangleComponent(
size: Vector2.all(indicatorSize),
paint: blue,
),
);
add(
RectangleComponent(
position: size / 2,
size: Vector2.all(indicatorSize),
anchor: Anchor.center,
paint: red,
),
);
}
@override
void onTapDown(TapDownEvent event) {
removeFromParent();
event.handled = true;
}
}