@@ -99,20 +99,20 @@ class BrickBreaker extends FlameGame
99
99
100
100
void startGame () {
101
101
world.removeAll (world.children.query <Ball >());
102
- world.removeAll (world.children.query <Bat >());
102
+ world.removeAll (world.children.query <Paddle >());
103
103
world.removeAll (world.children.query <Brick >());
104
104
105
105
world.add (Ball (
106
106
difficultyModifier: difficultyModifier,
107
107
radius: ballRadius,
108
108
position: size / 2 ,
109
109
velocity:
110
- Vector2 ((rand.nextDouble () - 0.5 ) * width, height * 0.2 ).normalized ()
110
+ Vector2 ((rand.nextDouble () - 0.5 ) * width, height * 0.3 ).normalized ()
111
111
..scale (height / 4 ),
112
112
));
113
113
114
- world.add (Bat (
115
- size: Vector2 (batWidth, batHeight ),
114
+ world.add (Paddle (
115
+ size: Vector2 (paddleWidth, paddleHeight ),
116
116
cornerRadius: const Radius .circular (ballRadius / 2 ),
117
117
position: Vector2 (width / 2 , height * 0.95 ),
118
118
));
@@ -141,12 +141,6 @@ class BrickBreaker extends FlameGame
141
141
RawKeyEvent event, Set <LogicalKeyboardKey > keysPressed) {
142
142
super .onKeyEvent (event, keysPressed);
143
143
switch (event.logicalKey) {
144
- case LogicalKeyboardKey .arrowLeft:
145
- case LogicalKeyboardKey .keyA:
146
- world.children.query <Bat >().first.moveBy (- batStep);
147
- case LogicalKeyboardKey .keyD:
148
- case LogicalKeyboardKey .arrowRight:
149
- world.children.query <Bat >().first.moveBy (batStep);
150
144
case LogicalKeyboardKey .space:
151
145
case LogicalKeyboardKey .enter:
152
146
startGame ();
@@ -201,7 +195,7 @@ class Ball extends CircleComponent
201
195
},
202
196
));
203
197
}
204
- } else if (other is Bat ) {
198
+ } else if (other is Paddle ) {
205
199
velocity.y = - velocity.y;
206
200
velocity.x = velocity.x +
207
201
(position.x - other.position.x) / other.size.x * game.width * 0.3 ;
@@ -220,9 +214,9 @@ class Ball extends CircleComponent
220
214
}
221
215
}
222
216
223
- class Bat extends PositionComponent
224
- with DragCallbacks , HasGameReference <BrickBreaker > {
225
- Bat ({
217
+ class Paddle extends PositionComponent
218
+ with DragCallbacks , HasGameReference <BrickBreaker >, KeyboardHandler {
219
+ Paddle ({
226
220
required this .cornerRadius,
227
221
required super .position,
228
222
required super .size,
@@ -234,6 +228,22 @@ class Bat extends PositionComponent
234
228
..color = const Color (0xff1e6091 )
235
229
..style = PaintingStyle .fill;
236
230
231
+ @override
232
+ void update (double dt) {
233
+ super .update (dt);
234
+
235
+ final keysPressed = HardwareKeyboard .instance.logicalKeysPressed;
236
+ if (keysPressed.contains (LogicalKeyboardKey .arrowLeft) ||
237
+ keysPressed.contains (LogicalKeyboardKey .keyA)) {
238
+ position.x =
239
+ (position.x - (dt * 500 )).clamp (width / 2 , game.width - width / 2 );
240
+ } else if (keysPressed.contains (LogicalKeyboardKey .arrowRight) ||
241
+ keysPressed.contains (LogicalKeyboardKey .keyD)) {
242
+ position.x =
243
+ (position.x + (dt * 500 )).clamp (width / 2 , game.width - width / 2 );
244
+ }
245
+ }
246
+
237
247
@override
238
248
void render (Canvas canvas) {
239
249
super .render (canvas);
@@ -253,16 +263,6 @@ class Bat extends PositionComponent
253
263
position.x = (position.x + event.localDelta.x)
254
264
.clamp (width / 2 , game.width - width / 2 );
255
265
}
256
-
257
- void moveBy (double dx) {
258
- add (MoveToEffect (
259
- Vector2 (
260
- (position.x + dx).clamp (width / 2 , game.width - width / 2 ),
261
- position.y,
262
- ),
263
- EffectController (duration: 0.1 ),
264
- ));
265
- }
266
266
}
267
267
268
268
class Brick extends RectangleComponent
@@ -316,9 +316,9 @@ const brickColors = [
316
316
const gameWidth = 820.0 ;
317
317
const gameHeight = 1600.0 ;
318
318
const ballRadius = gameWidth * 0.02 ;
319
- const batWidth = gameWidth * 0.2 ;
320
- const batHeight = ballRadius * 2 ;
321
- const batStep = gameWidth * 0.05 ;
319
+ const paddleWidth = gameWidth * 0.2 ;
320
+ const paddleHeight = ballRadius * 2 ;
321
+ const paddleStep = gameWidth * 0.05 ;
322
322
const brickGutter = gameWidth * 0.015 ;
323
323
final brickWidth =
324
324
(gameWidth - (brickGutter * (brickColors.length + 1 ))) / brickColors.length;
0 commit comments