mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
Moving tutorials to the Flame main repository (#720)
* Moving tutorials to the Flame main repository * Update README.md Co-authored-by: Jochum van der Ploeg <jochum@vdploeg.net> * Upgraded scripts to support multi projects * Removed comment out code * Adding WIP disclaimer Co-authored-by: Jochum van der Ploeg <jochum@vdploeg.net>
This commit is contained in:
40
tutorials/1_basic_square/code/lib/main.dart
Normal file
40
tutorials/1_basic_square/code/lib/main.dart
Normal file
@ -0,0 +1,40 @@
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flame/game.dart';
|
||||
|
||||
void main() {
|
||||
final myGame = MyGame();
|
||||
runApp(
|
||||
GameWidget(
|
||||
game: myGame,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class MyGame extends Game {
|
||||
static const int squareSpeed = 400;
|
||||
static final squarePaint = BasicPalette.white.paint;
|
||||
late Rect squarePos;
|
||||
int squareDirection = 1;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
squarePos = Rect.fromLTWH(0, 0, 100, 100);
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
squarePos = squarePos.translate(squareSpeed * squareDirection * dt, 0);
|
||||
|
||||
if (squareDirection == 1 && squarePos.right > size.x) {
|
||||
squareDirection = -1;
|
||||
} else if (squareDirection == -1 && squarePos.left < 0) {
|
||||
squareDirection = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
canvas.drawRect(squarePos, squarePaint);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user