Skip to content

Commit 4337448

Browse files
committed
Update brick_breaker part 2
1 parent 4026205 commit 4337448

File tree

1 file changed

+101
-90
lines changed

1 file changed

+101
-90
lines changed

brick_breaker/codelab_rebuild.yaml

Lines changed: 101 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -128,31 +128,31 @@ steps:
128128
// found in the LICENSE file.
129129
130130
import 'dart:async';
131-
131+
132132
import 'package:flame/components.dart';
133133
import 'package:flame/game.dart';
134-
134+
135135
import 'components/components.dart';
136136
import 'config.dart';
137-
137+
138138
class BrickBreaker extends FlameGame {
139139
BrickBreaker()
140-
: super(
141-
camera: CameraComponent.withFixedResolution(
142-
width: gameWidth,
143-
height: gameHeight,
144-
),
145-
);
146-
140+
: super(
141+
camera: CameraComponent.withFixedResolution(
142+
width: gameWidth,
143+
height: gameHeight,
144+
),
145+
);
146+
147147
double get width => size.x;
148148
double get height => size.y;
149-
149+
150150
@override
151151
FutureOr<void> onLoad() async {
152152
super.onLoad();
153-
153+
154154
camera.viewfinder.anchor = Anchor.topLeft;
155-
155+
156156
world.add(PlayArea());
157157
}
158158
}
@@ -183,18 +183,15 @@ steps:
183183
// found in the LICENSE file.
184184
185185
import 'dart:async';
186-
186+
187187
import 'package:flame/components.dart';
188188
import 'package:flutter/material.dart';
189-
189+
190190
import '../brick_breaker.dart';
191-
191+
192192
class PlayArea extends RectangleComponent with HasGameReference<BrickBreaker> {
193-
PlayArea()
194-
: super(
195-
paint: Paint()..color = const Color(0xfff2e8cf),
196-
);
197-
193+
PlayArea() : super(paint: Paint()..color = const Color(0xfff2e8cf));
194+
198195
@override
199196
FutureOr<void> onLoad() async {
200197
super.onLoad();
@@ -226,24 +223,30 @@ steps:
226223
import 'package:flame/components.dart';
227224
import 'package:flame/game.dart';
228225
@@ -19,6 +20,7 @@ class BrickBreaker extends FlameGame {
229-
),
230-
);
226+
),
227+
);
231228
232229
+ final rand = math.Random();
233230
double get width => size.x;
234231
double get height => size.y;
235232
236-
@@ -29,5 +31,14 @@ class BrickBreaker extends FlameGame {
233+
@@ -29,5 +31,20 @@ class BrickBreaker extends FlameGame {
237234
camera.viewfinder.anchor = Anchor.topLeft;
238235
239236
world.add(PlayArea());
240237
+
241-
+ world.add(Ball(
238+
+ world.add(
239+
+ Ball(
242240
+ radius: ballRadius,
243241
+ position: size / 2,
244-
+ velocity: Vector2((rand.nextDouble() - 0.5) * width, height * 0.2)
245-
+ .normalized()
246-
+ ..scale(height / 4)));
242+
+ velocity:
243+
+ Vector2(
244+
+ (rand.nextDouble() - 0.5) * width,
245+
+ height * 0.2,
246+
+ ).normalized()
247+
+ ..scale(height / 4),
248+
+ ),
249+
+ );
247250
+
248251
+ debugMode = true;
249252
}
@@ -278,21 +281,23 @@ steps:
278281
279282
import 'package:flame/components.dart';
280283
import 'package:flutter/material.dart';
281-
284+
282285
class Ball extends CircleComponent {
283286
Ball({
284287
required this.velocity,
285288
required super.position,
286289
required double radius,
287290
}) : super(
288-
radius: radius,
289-
anchor: Anchor.center,
290-
paint: Paint()
291+
radius: radius,
292+
anchor: Anchor.center,
293+
paint:
294+
Paint()
291295
..color = const Color(0xff1e6091)
292-
..style = PaintingStyle.fill);
293-
296+
..style = PaintingStyle.fill,
297+
);
298+
294299
final Vector2 velocity;
295-
300+
296301
@override
297302
void update(double dt) {
298303
super.update(dt);
@@ -322,8 +327,8 @@ steps:
322327
-class BrickBreaker extends FlameGame {
323328
+class BrickBreaker extends FlameGame with HasCollisionDetection {
324329
BrickBreaker()
325-
: super(
326-
camera: CameraComponent.withFixedResolution(
330+
: super(
331+
camera: CameraComponent.withFixedResolution(
327332
- name: Patch lib/src/components/ball.dart
328333
path: brick_breaker/lib/src/components/ball.dart
329334
patch-u: |
@@ -346,24 +351,24 @@ steps:
346351
Ball({
347352
required this.velocity,
348353
required super.position,
349-
@@ -15,7 +20,8 @@ class Ball extends CircleComponent {
350-
anchor: Anchor.center,
351-
paint: Paint()
352-
..color = const Color(0xff1e6091)
353-
- ..style = PaintingStyle.fill);
354-
+ ..style = PaintingStyle.fill,
355-
+ children: [CircleHitbox()]);
354+
@@ -17,6 +22,7 @@ class Ball extends CircleComponent {
355+
Paint()
356+
..color = const Color(0xff1e6091)
357+
..style = PaintingStyle.fill,
358+
+ children: [CircleHitbox()],
359+
);
356360
357361
final Vector2 velocity;
358-
359-
@@ -24,4 +30,23 @@ class Ball extends CircleComponent {
362+
@@ -26,4 +32,25 @@ class Ball extends CircleComponent {
360363
super.update(dt);
361364
position += velocity * dt;
362365
}
363366
+
364367
+ @override
365368
+ void onCollisionStart(
366-
+ Set<Vector2> intersectionPoints, PositionComponent other) {
369+
+ Set<Vector2> intersectionPoints,
370+
+ PositionComponent other,
371+
+ ) {
367372
+ super.onCollisionStart(intersectionPoints, other);
368373
+ if (other is PlayArea) {
369374
+ if (intersectionPoints.first.y <= 0) {
@@ -385,22 +390,26 @@ steps:
385390
patch-u: |
386391
--- b/brick_breaker/step_06/lib/src/components/play_area.dart
387392
+++ a/brick_breaker/step_06/lib/src/components/play_area.dart
388-
@@ -4,6 +4,7 @@
393+
@@ -4,13 +4,18 @@
389394
390395
import 'dart:async';
391396
392397
+import 'package:flame/collisions.dart';
393398
import 'package:flame/components.dart';
394399
import 'package:flutter/material.dart';
395400
396-
@@ -13,6 +14,7 @@ class PlayArea extends RectangleComponent with HasGameReference<BrickBreaker> {
397-
PlayArea()
398-
: super(
399-
paint: Paint()..color = const Color(0xfff2e8cf),
400-
+ children: [RectangleHitbox()],
401-
);
401+
import '../brick_breaker.dart';
402+
403+
class PlayArea extends RectangleComponent with HasGameReference<BrickBreaker> {
404+
- PlayArea() : super(paint: Paint()..color = const Color(0xfff2e8cf));
405+
+ PlayArea()
406+
+ : super(
407+
+ paint: Paint()..color = const Color(0xfff2e8cf),
408+
+ children: [RectangleHitbox()],
409+
+ );
402410
403411
@override
412+
FutureOr<void> onLoad() async {
404413
- name: Copy step_06
405414
copydir:
406415
from: brick_breaker
@@ -433,23 +442,28 @@ steps:
433442
+class BrickBreaker extends FlameGame
434443
+ with HasCollisionDetection, KeyboardEvents {
435444
BrickBreaker()
436-
: super(
437-
camera: CameraComponent.withFixedResolution(
438-
@@ -39,6 +43,24 @@ class BrickBreaker extends FlameGame with HasCollisionDetection {
439-
.normalized()
440-
..scale(height / 4)));
445+
: super(
446+
camera: CameraComponent.withFixedResolution(
447+
@@ -45,6 +49,29 @@ class BrickBreaker extends FlameGame with HasCollisionDetection {
448+
),
449+
);
441450
442-
+ world.add(Bat(
451+
+ world.add(
452+
+ Bat(
443453
+ size: Vector2(batWidth, batHeight),
444454
+ cornerRadius: const Radius.circular(ballRadius / 2),
445-
+ position: Vector2(width / 2, height * 0.95)));
455+
+ position: Vector2(width / 2, height * 0.95),
456+
+ ),
457+
+ );
446458
+
447459
debugMode = true;
448460
}
449461
+
450462
+ @override
451463
+ KeyEventResult onKeyEvent(
452-
+ KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
464+
+ KeyEvent event,
465+
+ Set<LogicalKeyboardKey> keysPressed,
466+
+ ) {
453467
+ super.onKeyEvent(event, keysPressed);
454468
+ switch (event.logicalKey) {
455469
+ case LogicalKeyboardKey.arrowLeft:
@@ -489,18 +503,17 @@ steps:
489503
import 'play_area.dart';
490504
491505
class Ball extends CircleComponent
492-
@@ -43,8 +45,14 @@ class Ball extends CircleComponent
506+
@@ -47,8 +49,13 @@ class Ball extends CircleComponent
493507
} else if (intersectionPoints.first.x >= game.width) {
494508
velocity.x = -velocity.x;
495509
} else if (intersectionPoints.first.y >= game.height) {
496510
- removeFromParent();
497-
+ add(RemoveEffect(
498-
+ delay: 0.35,
499-
+ ));
511+
+ add(RemoveEffect(delay: 0.35));
500512
}
501513
+ } else if (other is Bat) {
502514
+ velocity.y = -velocity.y;
503-
+ velocity.x = velocity.x +
515+
+ velocity.x =
516+
+ velocity.x +
504517
+ (position.x - other.position.x) / other.size.x * game.width * 0.3;
505518
} else {
506519
debugPrint('collision with $other');
@@ -528,48 +541,46 @@ steps:
528541
import 'package:flame/effects.dart';
529542
import 'package:flame/events.dart';
530543
import 'package:flutter/material.dart';
531-
544+
532545
import '../brick_breaker.dart';
533-
546+
534547
class Bat extends PositionComponent
535548
with DragCallbacks, HasGameReference<BrickBreaker> {
536549
Bat({
537550
required this.cornerRadius,
538551
required super.position,
539552
required super.size,
540-
}) : super(
541-
anchor: Anchor.center,
542-
children: [RectangleHitbox()],
543-
);
544-
553+
}) : super(anchor: Anchor.center, children: [RectangleHitbox()]);
554+
545555
final Radius cornerRadius;
546-
547-
final _paint = Paint()
548-
..color = const Color(0xff1e6091)
549-
..style = PaintingStyle.fill;
550-
556+
557+
final _paint =
558+
Paint()
559+
..color = const Color(0xff1e6091)
560+
..style = PaintingStyle.fill;
561+
551562
@override
552563
void render(Canvas canvas) {
553564
super.render(canvas);
554565
canvas.drawRRect(
555-
RRect.fromRectAndRadius(
556-
Offset.zero & size.toSize(),
557-
cornerRadius,
558-
),
559-
_paint);
566+
RRect.fromRectAndRadius(Offset.zero & size.toSize(), cornerRadius),
567+
_paint,
568+
);
560569
}
561-
570+
562571
@override
563572
void onDragUpdate(DragUpdateEvent event) {
564573
super.onDragUpdate(event);
565574
position.x = (position.x + event.localDelta.x).clamp(0, game.width);
566575
}
567-
576+
568577
void moveBy(double dx) {
569-
add(MoveToEffect(
570-
Vector2((position.x + dx).clamp(0, game.width), position.y),
571-
EffectController(duration: 0.1),
572-
));
578+
add(
579+
MoveToEffect(
580+
Vector2((position.x + dx).clamp(0, game.width), position.y),
581+
EffectController(duration: 0.1),
582+
),
583+
);
573584
}
574585
}
575586
- name: Copy step_07

0 commit comments

Comments
 (0)