Skip to content

Commit 445e177

Browse files
authored
Improve fluidity of arrow key paddle movement (#2839)
1 parent 2378afc commit 445e177

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

pkgs/samples/lib/brick_breaker.dart

+27-27
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,20 @@ class BrickBreaker extends FlameGame
9999

100100
void startGame() {
101101
world.removeAll(world.children.query<Ball>());
102-
world.removeAll(world.children.query<Bat>());
102+
world.removeAll(world.children.query<Paddle>());
103103
world.removeAll(world.children.query<Brick>());
104104

105105
world.add(Ball(
106106
difficultyModifier: difficultyModifier,
107107
radius: ballRadius,
108108
position: size / 2,
109109
velocity:
110-
Vector2((rand.nextDouble() - 0.5) * width, height * 0.2).normalized()
110+
Vector2((rand.nextDouble() - 0.5) * width, height * 0.3).normalized()
111111
..scale(height / 4),
112112
));
113113

114-
world.add(Bat(
115-
size: Vector2(batWidth, batHeight),
114+
world.add(Paddle(
115+
size: Vector2(paddleWidth, paddleHeight),
116116
cornerRadius: const Radius.circular(ballRadius / 2),
117117
position: Vector2(width / 2, height * 0.95),
118118
));
@@ -141,12 +141,6 @@ class BrickBreaker extends FlameGame
141141
RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
142142
super.onKeyEvent(event, keysPressed);
143143
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);
150144
case LogicalKeyboardKey.space:
151145
case LogicalKeyboardKey.enter:
152146
startGame();
@@ -201,7 +195,7 @@ class Ball extends CircleComponent
201195
},
202196
));
203197
}
204-
} else if (other is Bat) {
198+
} else if (other is Paddle) {
205199
velocity.y = -velocity.y;
206200
velocity.x = velocity.x +
207201
(position.x - other.position.x) / other.size.x * game.width * 0.3;
@@ -220,9 +214,9 @@ class Ball extends CircleComponent
220214
}
221215
}
222216

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({
226220
required this.cornerRadius,
227221
required super.position,
228222
required super.size,
@@ -234,6 +228,22 @@ class Bat extends PositionComponent
234228
..color = const Color(0xff1e6091)
235229
..style = PaintingStyle.fill;
236230

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+
237247
@override
238248
void render(Canvas canvas) {
239249
super.render(canvas);
@@ -253,16 +263,6 @@ class Bat extends PositionComponent
253263
position.x = (position.x + event.localDelta.x)
254264
.clamp(width / 2, game.width - width / 2);
255265
}
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-
}
266266
}
267267

268268
class Brick extends RectangleComponent
@@ -316,9 +316,9 @@ const brickColors = [
316316
const gameWidth = 820.0;
317317
const gameHeight = 1600.0;
318318
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;
322322
const brickGutter = gameWidth * 0.015;
323323
final brickWidth =
324324
(gameWidth - (brickGutter * (brickColors.length + 1))) / brickColors.length;

pkgs/sketch_pad/lib/samples.g.dart

+27-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)