mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
Previously, as part of Issue #2798, I fixed the Klondike Tutorial app's step4 executable to use the Flame 1.9.x `world` and `camera` built-in objects and described the new code in the Step 2 tutorial, where it first appears in the discussion. However, the Step 2 and Step 3 tutorial texts have options to execute code and those did not include the new code. Accordingly, I have cloned the relevant code into `app/lib/step2/klondike_game.dart` and `app/lib/step3/klondike_game.dart`. I have also checked that there are no `*.md` files to be updated. The `step2.md` tutorial file covers Issue #2799 AFAICS.
79 lines
2.2 KiB
Dart
79 lines
2.2 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:flame/components.dart';
|
|
import 'package:flame/flame.dart';
|
|
import 'package:flame/game.dart';
|
|
|
|
import 'components/card.dart';
|
|
import 'components/foundation.dart';
|
|
import 'components/pile.dart';
|
|
import 'components/stock.dart';
|
|
import 'components/waste.dart';
|
|
|
|
class KlondikeGame extends FlameGame {
|
|
static const double cardGap = 175.0;
|
|
static const double cardWidth = 1000.0;
|
|
static const double cardHeight = 1400.0;
|
|
static const double cardRadius = 100.0;
|
|
static final Vector2 cardSize = Vector2(cardWidth, cardHeight);
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
await Flame.images.load('klondike-sprites.png');
|
|
|
|
final stock = Stock()
|
|
..size = cardSize
|
|
..position = Vector2(cardGap, cardGap);
|
|
final waste = Waste()
|
|
..size = cardSize
|
|
..position = Vector2(cardWidth + 2 * cardGap, cardGap);
|
|
final foundations = List.generate(
|
|
4,
|
|
(i) => Foundation()
|
|
..size = cardSize
|
|
..position =
|
|
Vector2((i + 3) * (cardWidth + cardGap) + cardGap, cardGap),
|
|
);
|
|
final piles = List.generate(
|
|
7,
|
|
(i) => Pile()
|
|
..size = cardSize
|
|
..position = Vector2(
|
|
cardGap + i * (cardWidth + cardGap),
|
|
cardHeight + 2 * cardGap,
|
|
),
|
|
);
|
|
|
|
world.add(stock);
|
|
world.add(waste);
|
|
world.addAll(foundations);
|
|
world.addAll(piles);
|
|
|
|
camera.viewfinder.visibleGameSize =
|
|
Vector2(cardWidth * 7 + cardGap * 8, 4 * cardHeight + 3 * cardGap);
|
|
camera.viewfinder.position = Vector2(cardWidth * 3.5 + cardGap * 4, 0);
|
|
camera.viewfinder.anchor = Anchor.topCenter;
|
|
|
|
final random = Random();
|
|
for (var i = 0; i < 7; i++) {
|
|
for (var j = 0; j < 4; j++) {
|
|
final card = Card(random.nextInt(13) + 1, random.nextInt(4))
|
|
..position = Vector2(100 + i * 1150, 100 + j * 1500)
|
|
..addToParent(world);
|
|
// flip the card face-up with 90% probability
|
|
if (random.nextDouble() < 0.9) {
|
|
card.flip();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Sprite klondikeSprite(double x, double y, double width, double height) {
|
|
return Sprite(
|
|
Flame.images.fromCache('klondike-sprites.png'),
|
|
srcPosition: Vector2(x, y),
|
|
srcSize: Vector2(width, height),
|
|
);
|
|
}
|