Files
flame/examples/lib/stories/system/without_flamegame_example.dart
Lukas Klingsbo 8b132d7c0b Unify examples structure (#1118)
* Animations, CameraAndViewport, CollisionDetection and Components unified

* Added descriptions to effects

* Rename input games

* Unify input stories

* Add info to parallax section

* Added descriptions to the rendering examples

* Add descriptions to the sprites directory

* Fix utils and rendering section

* Add descriptions to the widgets section

* Delete directory that rebase brought back

* Unify game names

* Added some styleguide docs for examples

* Fix analyze issues

* All files should have _example as suffix

* Made the FollowComponentExample a bit easier to understand

* Change priority of ember
2021-11-19 14:28:04 +01:00

53 lines
1.5 KiB
Dart

import 'dart:ui';
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);
}
}