Files
flame/examples/lib/stories/system/without_flamegame_example.dart
Lukas Klingsbo 5c47d7f6d7 chore: analyze issues introduced from new dart version (#1196)
* Added Component.childrenFactory

* fix some of the lint warnings

* more lint warnings

* remove changelog entry

* more analyzer warnings

* one more warning

* one more warning

* remove more unused imports

* fix more warnings

* another warning

* one more warning

* a lot more warnings

* some more warnings

* fix warnings in flame_svg

* fix warnings in flame_bloc

* Remove OrderedSet override feature

* Remove testRandom change

* Remove unnecessary type checks

* Re-remove deprecated argument in random_test

Co-authored-by: Pasha Stetsenko <stpasha@google.com>
2021-12-09 15:40:43 +01:00

51 lines
1.5 KiB
Dart

import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame/palette.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
class NoFlameGameExample with Loadable, Game, KeyboardEvents {
static const String description = '''
This example showcases how to create a game without the FlameGame.
It also briefly showcases how to act on keyboard events.
Usage: Use A S D F to steer the rectangle.
''';
static final Paint white = BasicPalette.white.paint();
static const int speed = 200;
Rect rect = const Rect.fromLTWH(0, 100, 100, 100);
final Vector2 velocity = Vector2(0, 0);
@override
void update(double dt) {
final displacement = velocity * (speed * dt);
rect = rect.translate(displacement.x, displacement.y);
}
@override
void render(Canvas canvas) {
canvas.drawRect(rect, white);
}
@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final isKeyDown = event is RawKeyDownEvent;
if (event.logicalKey == LogicalKeyboardKey.keyA) {
velocity.x = isKeyDown ? -1 : 0;
} else if (event.logicalKey == LogicalKeyboardKey.keyD) {
velocity.x = isKeyDown ? 1 : 0;
} else if (event.logicalKey == LogicalKeyboardKey.keyW) {
velocity.y = isKeyDown ? -1 : 0;
} else if (event.logicalKey == LogicalKeyboardKey.keyS) {
velocity.y = isKeyDown ? 1 : 0;
}
return super.onKeyEvent(event, keysPressed);
}
}