Flutter Game: Build a Pong Game with Flutter Flame
Tags: flutter, gamedev, codenewbie, programming
Published: 2023-03-17 19:08:01

Building apps with Flutter is really cool. You know what’s cooler?
Building games with flutter. This article will show you how to build a flutter game from start to finish with a flutter flame tutorial.

Let’s create a flutter game! We can build the classic pong game in Flutter using the 🔥[Flame game engine](https://flame-engine.org/).

Flame is a 2D game engine built for Flutter. It’s built on top of the framework and simplifies game development. Flame provides us with everything we’ll need to build a flutter game.

<iframe title="vimeo-player" src="https://player.vimeo.com/video/744509891?h=fee931dbde" width="640" height="360" frameborder="0"    allowfullscreen></iframe>

Some of the concepts we’ll learn are: flutter game development

- Collision detection in Flame
- Building a simple AI opponent
- Using Flame audio flutter

**Note**: Knowledge of the basics of Flutter and Flame are required for this tutorial. Check out the [Flutter Flame docs](https://docs.flame-engine.org/1.3.0/index.html#) if you’re new to the engine.

Grab a coffee; let’s get started! Flutter game on! 🎮

## Flutter Game: Getting Started with game development in Flutter
Let’s create a new flutter project and enter the folder with the following commands:

`flutter create pong_game
cd pong_game`

Next, add the required Flame dependencies:

`flutter pub add flame`

For the game, our file structure will look like this:

`-lib/
··|---main.dart
··|---pong_game.dart
··|---player_paddle.dart
··|---ball.dart
··|---ai_paddle.dart
··|---scoretext.dart`
[Save this code](https://takrutvik.pieces.cloud/?p=fc3248810b)

We’ll update our main.dart with the following code:

`void main() {
  final game = PongGame();
  runApp(GameWidget(game: game));
}`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiIzMTVhNzQ5NS0wYzBmLTRiYmItYWEyNS1hMjNmMDNmMTE2MzciLCJhc3NldCI6Ijk4MjczZmZjLWRlN2ItNGNhYy1hN2E5LTNkMGM0MTRjNGFmMSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJtYWluLmRhcnQiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRp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YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovLzhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS5waWVjZXMuY2xvdWQifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Now, in the pong_game.dart file, we’ll add the the following:

`class PongGame extends FlameGame
 with HasCollisionDetection, HasKeyboardHandlerComponents {
 PongGame();
 
 @override
 Future<void> onLoad() async {}
 
 @override
 @mustCallSuper
 KeyEventResult onKeyEvent(
   RawKeyEvent event,
 Set<LogicalKeyboardKey> keysPressed,
 ) {
 super.onKeyEvent(event, keysPressed);
 
 return KeyEventResult.handled;
 }
}`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiJiY2JlMTA0My00MzUxLTQ1ODktOGI5Zi1lNDNhMGU1YzA5MDgiLCJhc3NldCI6Ijg3YjAyNTllLTg2YzAtNDkyZC1hZWVhLTM0YmQ0ZTdmZmE4ZiIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJwb25nX2dhbWUuZGFydCIsImRlc2NyaXB0aW9uIjoi8J+SoSBTbWFydCBEZXNjcmlwdGlvbjogTWFpbiBlbnRyeSBwb2ludCBmb3IgdGhlIGFwcGxpY2F0aW9uLiIsInVzZXIiOnsic2NoZW1hIjp7Im1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ZmI0ZDVjNzkxLnBpZWNlcy5jbG91ZCJ9LCJ2YW5pdHkiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vdGFrcnV0dmlrLnBpZWNlcy5jbG91ZCJ9fSwic3RhdHVzIjp7ImNsb3VkIjoiUlVOTklORyJ9LCJwcm9qZWN0IjoicnVudGltZS11c2VyLWNsb3VkLWdyb3VwLTAiLCJ1cGRhdGVkIjp7InZhbHVlIjoiMjAyMi0wNy0xNFQwNjowOTo0NC42MzUzODFaIiwicmVhZGFibGUiOiI4IG1vbnRocyBhZ28ifSwidmVyc2lvbiI6IjMuMS4wIn19fQ==&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Here, we have the PongGame declared. Notice that it has two mixings: HasCollisionDetection and HasKeyboardHandlerComponents. This will let Flame know that our game is going to use these two things and allow us to work with collision detection and take keyboard inputs at the component level.

We’re also overriding the onKeyEvent here and returning KeyEventResult.handled. This is because if you’re on macOS, then you’ll notice key press sounds as you’re receiving keyboard inputs in the game. Returning KeyEventResult.handled will disable those sounds.

Build & run:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jdpe8fpdgrlkhc0comj.png)

## Flutter Flame Collision Detection
Before moving on to building our game, let’s take a look at how collision detection works in Flame. This will be important for us as we’ll need to set up HitBoxes for our game bodies, know when these bodies collide with each other and react accordingly.

### HitBoxes
In many game systems, collision detection works by having a HitBox around the game object. HitBoxes react to collisions and can send callbacks with the collision information.

Flame supports adding different HitBoxes to our components like PolygonHitBox, RectangleHitBox, CircleHitBox or ScreenHitBox, which is usually used for declaring the world boundaries/screen edges that components may collide with.

**Note**: We can use multiple HitBoxes on a component to provide more accurate collision detection for it. For example, a game character can have separate HitBoxes around its arms, its legs, and so on.

### Enable Collision Detection
For this, we first need to add the HasCollisionDetection mixing to our Flame game.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvvitno667e2eoiasf5p.png)

For the components, we want to get notified when they collide with other bodies that are capable of collision. For this, we’ll add the CollisionCallbacks mixing to those components.

`class MyComponent extends PositionComponent with CollisionCallbacks {


 @override
 void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
 // TODO: implement onCollision
 super.onCollision(intersectionPoints, other);
  }  

 @override
 void onCollisionStart(Set<Vector2> intersectionPoints, PositionComponent other) {
 // TODO: implement onCollisionStart
 super.onCollisionStart(intersectionPoints, other);
  }

 @override
 void onCollisionEnd(PositionComponent other) {
 // TODO: implement onCollisionEnd
 super.onCollisionEnd(other);
  }

}`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiI5NWFiNTE5YS05ZDBjLTRjZjAtYTQ4Ny02NDE4YjExM2JlM2IiLCJhc3NldCI6IjVkMmIyYzE5LTg5NDgtNGMxMy1hYmMzLWJmY2E0NmJmZjExNCIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJEYXJ0IFNuaXBwZXQiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IFRoaXMgY2xhc3MgaW1wbGVtZW50cyB0aGUgQ29sbGlzaW9uQ2FsbGJhY2tzIGludGVyZmFjZS4iLCJ1c2VyIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIXzEifSwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BRmRadWNydWhXV09BSXFpODBDQzBRSEVicTFoYkx3SUVnN0IwS1RkdTN5T0RRPXM5Ni1jIiwiZW1haWwiOiJ0YWtydXR2aWtAZ21haWwuY29tIiwiY3JlYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6MTguMjMwWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIy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)

Adding this mixing allows us to be notified when a body collides with other bodies through callbacks such as onCollision, onCollisionStart and onCollisionEnd. These callbacks also provide the intersection points and the reference to the other body the component is colliding with.

Note: `https://docs.flame-engine.org/1.0.0/collision_detection.html` only lets us know when two bodies collide. What happens upon collision is up to us!

Now, let’s move on to the different components of our game.

## Game Components
Our Pong game mainly consists of the following components:
- Game Boundaries
- Player paddle
- Ball
- AI opponent paddle
- Scoring system

## Game Boundaries
Our ball is going to collide with the boundaries of our game/screen. We need to know when this happens so that we can either bounce it off of the top or bottom of the screen or update the players’ score if it’s colliding with the left or right of the screen.

For this, we’ll declare game boundaries by adding the ScreenHitBox component.

Replace the onload method within PongGame with the following:

`@override
 Future<void> onLoad() async {
   addAll([
     ScreenHitbox()
   ]);
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiJmYzcyMDk2Ny1lMjM0LTQ2ZDgtYWU3ZC05MjMwMTJhYmNmMjYiLCJhc3NldCI6IjE5OTM5ZWRjLTFmNTAtNGVkMC1iMGZkLTU2YTRiMGNjM2JjYyIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJnYW1lX2JvdW5kYXJpZXMiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IFRoZSBTY3JlZW5IaXRib3ggaXMgdGhlIGZpcnN0IGhpdGJveCB0aGF0IGlzIHRoZSBzY3JlZW4uIiwidXNlciI6eyJzY2hlbWEiOnsibWlncmF0aW9uIjowLCJzZW1hbnRpYyI6Ik1BSk9SXzBfTUlOT1JfMF9QQVRDSF8xIn0sInBpY3R1cmUiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS0vQUZkWnVjcnVoV1dPQUlxaTgwQ0MwUUhFYnExaGJMd0lFZzdCMEtUZHUzeU9EUT1zOTYtYyIsImVtYWlsIjoidGFrcnV0dmlrQGd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ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjQ0LjYzNTM4MVoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJ2ZXJzaW9uIjoiMy4xLjAifX19&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

ScreenHitBox will represent the edges of our game screen. If any other components collide with the edges, we’ll be notified of the collision.

## Player Paddle
Now, we’ll add the player paddle to the flutter game tutorial.

Create a new file called player_paddle.dart and add the following to it:

`// TODO: add key event enum
 
class PlayerPaddle extends PositionComponent
 with HasGameRef<FlameGame>, CollisionCallbacks {
 late final RectangleHitbox paddleHitBox;
 late final RectangleComponent paddle;
 
 // TODO: add variable key event and speed variables
 
 @override
 Future<void>? onLoad() {
 // TODO: implement onLoad
 
 final worldRect = gameRef.size.toRect();
 
   size = Vector2(10, 100);
   position.x = worldRect.width * 0.9 - 10;
   position.y = worldRect.height / 2 - size.y / 2;
   paddle = RectangleComponent(
     size: size,
     paint: Paint()..color = Colors.blue,
   );
 
   paddleHitBox = RectangleHitbox(
     size: size,
   );
 
   addAll([
     paddle,
     paddleHitBox,
   ]);
 
 // TODO: add keyboard listener component
 
 return super.onLoad();
 }
 
 //TODO: add update code for moving paddle
}`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiJkNzJjMzBiOS0yYzhiLTQ5NDAtYTVjMy1hMzY5ZTA5YTEyN2YiLCJhc3NldCI6ImNhMWY1YzU1LWNhMzQtNDkzNy1hMDVhLTk4YjYyODJmYzAwYSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJwbGF5ZXJfcGFkZGxlIiwiZGVzY3JpcHRpb24iOiLwn5KhIFNtYXJ0IERlc2NyaXB0aW9uOiBBZGQgYSBwYWRkbGUgdG8gdGhlIHBsYXllciBzIHBhZGRsZS4iLCJ1c2VyIjp7InNjaGVtYSI6eyJtaWdyYXR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5waWVjZXMuY2xvdWQifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Our PlayerPaddle is a PositionComponent with the HasGameRef and CollisionCallbacks mixing. The HasGameRef mixing will allow us to get the game reference and check for any values in our game world. CollisionCallbacks mixing, as we discussed, will add support for setting collision callbacks.

In the onLoad method, we’re setting the size for our paddle component and positioning it at the center-right of the screen. We also added a RectangleHitBox of the same size as our paddle so that it can detect collisions.

Within the onload method of the PongGame component, add the PlayerPaddle:

`@override
 Future<void> onLoad() async {
   addAll(
     [
       ...
       .....
       PlayerPaddle(),
 
     ],
   );
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiI0M2NkMmVhZS0yYzQyLTQ0NzUtYWE1NS1kZGM4ZTQ2NTllNjAiLCJhc3NldCI6IjA4ZjFkNjBhLTEyZDUtNGFjZS1iYWEwLTBhOTUxNzE0ODJiZSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJEYXJ0IFNuaXBwZXQiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IG9uTG9hZCAtIExvYWQgYWxsIG9iamVjdHMgaW4gdGhlIHN5c3RlbSIsInVzZXIiOnsic2NoZW1hIjp7Im1pZ3JhdGlvbiI6MCwic2VtYW50aWMiOiJNQUpP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J2YW5pdHkiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vdGFrcnV0dmlrLnBpZWNlcy5jbG91ZCJ9fSwic3RhdHVzIjp7ImNsb3VkIjoiUlVOTklORyJ9LCJwcm9qZWN0IjoicnVudGltZS11c2VyLWNsb3VkLWdyb3VwLTAiLCJ1cGRhdGVkIjp7InZhbHVlIjoiMjAyMi0wNy0xNFQwNjowOTo0NC42MzUzODFaIiwicmVhZGFibGUiOiI4IG1vbnRocyBhZ28ifSwidmVyc2lvbiI6IjMuMS4wIn19fQ==&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Build & run: flutter flame example

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nx2fhz2du8abkuprekc.png)

## Player keyboard controls
Flame offers two different ways to take keyboard inputs; one at the game level and the other at the component level.

Let’s take a look at receiving keyboard inputs at the component level. You can learn more about other ways of taking keyboard input here.

We’ll make sure our PongGame component has the HasKeyboardHandlerComponents mixing. Within our PlayerPaddle component, we’ll use the KeyboardListenerComponent, through which we can set callbacks for different key events.

Add the following component within your onload method:

`add(
     KeyboardListenerComponent(
       keyDown: {
         LogicalKeyboardKey.arrowDown: (keysPressed) {
 
 return true;
         },
         LogicalKeyboardKey.arrowUp: (keysPressed) {
 
 return true;
         },
       },
       keyUp: {
         LogicalKeyboardKey.arrowDown: (keysPressed) {
 
 return true;
         },
         LogicalKeyboardKey.arrowUp: (keysPressed) {
 
 return true;
         },
       },
     ),
   );`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiIxYzU4MWFjNi1jNGM1LTQ3NTUtYjM0ZC00OGYzYjE2ZWFiMTkiLCJhc3NldCI6IjBlNmE2MTU1LTM3MmUtNGMwYi05NjlmLWFkNzMxZjI5YWRlMSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJwbGF5ZXJfa2V5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YyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLWZoY21iaGVrbHEtdWMuYS5ydW4uYXBwIn0sImlkIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovLzhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS5waWVjZXMuY2xvdWQifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

This adds the KeyboardListenerComponent. We’ll be registering callbacks for arrowDown and arrowUp events when the respective keys are either pressed or released.

## Moving Player Paddle
Now that we’re receiving keyboard events, let’s see how we can move our paddle.

Let’s try updating our paddle position along the y-axis by 50 when the down arrow is pressed and by -50 when the up arrow is pressed. Update keyDown within the KeyboardListenerComponent with the following (you may need to hot restart your game to reflect the new changes):

`keyDown: {
         LogicalKeyboardKey.arrowDown: (keysPressed) {
           position.y += 50;
 
 return true;
         },
         LogicalKeyboardKey.arrowUp: (keysPressed) {
           position.y -= 50;
 
 return true;
         },
       },`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiJiYjczZmU0Yi02ODM2LTQ5ZTctYmJkNi1hMmM3OTA4NDJlNTgiLCJhc3NldCI6IjE0NWYyNGIzLWUyNTUtNDQyMC1iYzVhLTcyNzU5MTliMGU2MCIsIm5hbWUiOi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HVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vdXNlci04ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEtZmhjbWJoZWtscS11Yy5hLnJ1bi5hcHAifSwiaWQiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLnBpZWNlcy5jbG91ZCJ9LCJ2YW5pdHkiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vdGFrcnV0dmlrLnBpZWNlcy5jbG91ZCJ9fSwic3RhdHVzIjp7ImNsb3VkIjoiUlVOTklORyJ9LCJwcm9qZWN0IjoicnVudGltZS11c2VyLWNsb3VkLWdyb3VwLTAiLCJ1cGRhdGVkIjp7InZhbHVlIjoiMjAyMi0wNy0xNFQwNjowOTo0NC42MzUzODFaIiwicmVhZGFibGUiOiI4IG1vbnRocyBhZ28ifSwidmVyc2lvbiI6IjMuMS4wIn19fQ==&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Build & run: game in flutter


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ptznmijj86eug3skyzf.png)

You’ll see that the paddle moves, but its movement is janky. It’s not smooth. 🤷

This is because the position is not updated consistently with the passage of time. To achieve smooth movement, we’ll need to update its position from within the update method.

Currently, in Flame, there’s no possible way to know which keys are pressed within the update method. For this, we’ll first set up a variable that’ll tell us which key was pressed so we can update the position accordingly.

Replace the // TODO: add key event enum within player_paddle.dart with the following code:

`enum KeyEventEnum {
  up,
  down,
  none,
}`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiI4YTQ1ZjBmMy1lZTI1LTQyOTYtYTc3NS04YjE3Mjc1MTY4YmYiLCJhc3NldCI6IjU4MGQzMmY4LWJhZTYtNDI2Zi05MDNhLTYyMj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iJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly91c2VyLThmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS1maGNtYmhla2xxLXVjLmEucnVuLmFwcCJ9LCJpZCI6eyJzdGF0dXMiOiJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly84ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEucGllY2VzLmNsb3VkIn0sInZhbml0eSI6eyJzdGF0dXMiOiJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIn19LCJzdGF0dXMiOnsiY2xvdWQiOiJSVU5OSU5HIn0sInByb2plY3QiOiJydW50aW1lLXVzZXItY2xvdWQtZ3JvdXAtMCIsInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjQ0LjYzNTM4MVoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJ2ZXJzaW9uIjoiMy4xLjAifX19&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Declare the following variables within the paddle component:

`KeyEventEnum keyPressed = KeyEventEnum.none;
 static const double speed = 400;`

1. keyPressed: Lets us know which key is pressed. When none of the keys are pressed, we’ll update this variable to KeyboardEventEnum.none, so we can know to stop updating the position.

2. speed: Paddle moving speed.
Replace the previously added KeyboardListenerComponent with the following:

`add(
      KeyboardListenerComponent(
        keyDown: {
          LogicalKeyboardKey.arrowDown: (keysPressed) {
            keyPressed = KeyEventEnum.down;

 return true;
          },
          LogicalKeyboardKey.arrowUp: (keysPressed) {
            keyPressed = KeyEventEnum.up;

 return true;
          },
        },
        keyUp: {
          LogicalKeyboardKey.arrowDown: (keysPressed) {
            keyPressed = KeyEventEnum.none;

 return true;
          },
          LogicalKeyboardKey.arrowUp: (keysPressed) {
            keyPressed = KeyEventEnum.none;

 return true;
          },
        },
      ),
    );`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiY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iOnsic2NoZW1hIjp7Im1pZ3JhdGlvbiI6MCwic2VtYW50aWMiOiJNQUpPUl8wX01JTk9SXzBfUEFUQ0hfMSJ9LCJpZCI6IjhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MSIsInVzZXIiOiI4ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEiLCJ1cmxzIjp7ImJhc2UiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vdXNlci04ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEtZmhjbWJoZWtscS11Yy5hLnJ1bi5hcHAifSwiaWQiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLnBpZWNlcy5jbG91ZCJ9LCJ2YW5pdHkiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vdGFrcnV0dmlrLnBpZWNlcy5jbG91ZCJ9fSwic3RhdHVzIjp7ImNsb3VkIjoiUlVOTklORyJ9LCJwcm9qZWN0IjoicnVudGltZS11c2VyLWNsb3VkLWdyb3VwLTAiLCJ1cGRhdGVkIjp7InZhbHVlIjoiMjAyMi0wNy0xNFQwNjowOTo0NC42MzUzODFaIiwicmVhZGFibGUiOiI4IG1vbnRocyBhZ28ifSwidmVyc2lvbiI6IjMuMS4wIn19fQ==&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Here, we’re doing two things:

1. Setting the KeyboardEventEnum to up/down based on the key pressed.

2. Resetting it to KeyEventEnum.none when the key is released.
Add the following code, which overrides the update method for the component:

`@override
 void update(double dt) {
 // TODO: implement update
 super.update(dt);
 if (keyPressed == KeyEventEnum.down) {
 final updatedPosition = position.y + speed * dt;
 if (updatedPosition < gameRef.size.y - paddle.height) {
       position.y = updatedPosition;
     }
   }
 if (keyPressed == KeyEventEnum.up) {
 final updatedPosition = position.y - speed * dt;
 if (updatedPosition > 0) {
       position.y = updatedPosition;
     }
   }
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiIzOTgwNDZhMS1lMDI5LTQzY2QtYWVkZS1mMGMzMGVjNWE2YTUiLCJhc3NldCI6ImJmOWM4ZTlkLTQxYTYtNGFmMi05Mjc0LTM2NjA0NjkzMWYyOCIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJEYXJ0IFNuaXBwZXQiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IFVwZGF0ZSB0aGUgcG9zaXRpb24gaW4gdGhlIGdhbWUuIiwidXNlciI6eyJzY2hlbWEiOnsibWlncmF0aW9uIjowLCJzZW1hbnRpYyI6Ik1BSk9SXzBfTUlOT1JfMF9QQVRDSF8xIn0sInBpY3R1cmUiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS0vQUZkWnVjcnVoV1dPQUlxaTgwQ0MwUUhFYnExaGJMd0lFZzdCMEtUZHUzeU9EUT1zOTYtYyIsImVtYWlsIjoidGFrcnV0dmlrQGdtYWlsLmNvbSIsImNyZWF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjE4LjIzMFoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJ1cGRhdGVkIjp7InZhbHVlIjoiMjAyMi0wNy0xNFQwNjowOToxOS4wNjVaIiwicmVhZGFibGUiOiI4IG1vbnRocyBhZ28ifSwiaWQiOiI4ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEiLCJuYW1lIjoiUnV0dmlrIFRhayIsImFlc3RoZXRpY3MiOnsidGhlbWUiOnsiZGFyayI6dHJ1ZX0sImZvbnQiOnsic2l6ZSI6MTZ9fSwiYWxsb2NhdGlvbiI6eyJzY2hlbWEiOnsibWlncmF0aW9uIjowLCJzZW1hbnRpYyI6Ik1BSk9SXzBfTUlOT1JfMF9QQVRDSF8xIn0sImlkIjoiOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxIiwidXNlciI6IjhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MSIsInVybHMiOnsiYmFzZSI6eyJzdGF0dXMiOiJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly91c2VyLThmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS1maGNtYmhla2xxLXVjLmEucnVuLmFwcCJ9LCJpZCI6eyJzdGF0dXMiOiJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly84ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEucGllY2VzLmNsb3VkIn0sInZhbml0eSI6eyJzdGF0dXMiOiJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIn19LCJzdGF0dXMiOnsiY2xvdWQiOiJSVU5OSU5HIn0sInByb2plY3QiOiJydW50aW1lLXVzZXItY2xvdWQtZ3JvdXAtMCIsInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjQ0LjYzNTM4MVoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJ2ZXJzaW9uIjoiMy4xLjAifX19&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Here, we update the paddle position based on the key pressed. This time, instead of passing a fixed displacement, we’re updating the position by the speed*dt(=distance) value.

We also check if our paddle is going out of the bounds of the game window. If it is, then we stop updating the position.

We can test our updates by holding down the up or down arrow keys and seeing the paddle move smoothly.

Build & run:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqe8t2yxfx2etermai2x.png)

## Flutter Game: Adding the Ball
Create a new file called ball.dart and add the following code to it:

`import 'dart:math' as math;
 
class Ball extends CircleComponent
 with HasGameRef<PongGame>, CollisionCallbacks {
 Ball() {
   paint = Paint()..color = Colors.white;
   radius = 10;
 }
 
 // 1.
 late Vector2 velocity;
 // 2.
 static const double speed = 500;
 // 3.
 static const degree = math.pi / 180;
 
 // 6.
 @override
 Future<void>? onLoad() {
   _resetBall;
 final hitBox = CircleHitbox(
     radius: radius,
   );
 
   addAll([
     hitBox,
   ]);
 
 return super.onLoad();
 }
 
 @override
 void update(double dt) {
 super.update(dt);
    position += velocity * dt;
  }
 
 
 // 4.
 void get _resetBall {
   position = gameRef.size / 2;
 final spawnAngle = getSpawnAngle;
 
 final vx = math.cos(spawnAngle * degree) * speed;
 final vy = math.sin(spawnAngle * degree) * speed;
   velocity = Vector2(
     vx,
     vy,
   );
 }
 
 // 5.
 double get getSpawnAngle {
 final sideToThrow = math.Random().nextBool();
 
 final random = math.Random().nextDouble();
 final spawnAngle = sideToThrow
       ? lerpDouble(-35, 35, random)!
       : lerpDouble(145, 215, random)!;
 
 return spawnAngle;
 }
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiIxYjJkMWJmMy00MjFlLTQ1YzQtYjEzZS01ZjFkYjM5Y2VkODQiLCJhc3NldCI6ImQ1MzJhYjdhLWY4N2EtNDYxZS1hNThkLTMxZWNkM2NjNTc0YiIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJEYXJ0IFNuaXBwZXQiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IFByaXZhdGUgbWV0aG9kIGZvciBjcmVhdGluZyBhIGJhbGwuIiwidXNlciI6eyJzY2hlbWEiOnsibWlncmF0aW9uIjowLCJzZW1hbnRpYyI6Ik1BSk9SXzBfTUlOT1JfMF9QQVRDSF8xIn0sInBpY3R1cmUiOiJodHRwczovL2xoMy5nb29nbGV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R2aWsucGllY2VzLmNsb3VkIn19LCJzdGF0dXMiOnsiY2xvdWQiOiJSVU5OSU5HIn0sInByb2plY3QiOiJydW50aW1lLXVzZXItY2xvdWQtZ3JvdXAtMCIsInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjQ0LjYzNTM4MVoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJ2ZXJzaW9uIjoiMy4xLjAifX19&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Our ball is a CircleComponent, which is a PositionedComponent but circular with HasGameRef and CollisionCallbacks mixing. We also defined the color and radius of the ball within its constructor.

Along with defining the HitBox for our ball in the onload method, we have some other things here:

1. velocity: A 2D vector representing the ball's velocity.

2. speed: A constant value that will calculate the ball's velocity.

3. degree: The degree to radian constant.

4. _resetBall: Spawns (positions) the ball at the center of the 
screen and launches it in a random direction with some initial velocity.

5. getSpawnAngle: Calculates the angle at which the ball will be thrown upon spawning.

Finally, within the update method, we update the ball's position with respect to its velocity and the time passed, i.e., dt.

Let’s add the ball component to our PongGame component:

`@override
 Future<void> onLoad() async {
   addAll(
     [
       ...
       .....
       Ball(),
     ],
   );
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiIzYjc5YTg4Zi1kNjYxLTQyMDItYjg0OS0yM2ZmN2UxZDBkNTgiLCJhc3NldCI6ImMwY2Q2Zjg4LTRlZTctNGQ1NC05MmJmLTBjMzY5N2U0ZjIzMiIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJEYXJ0IFNuaXBwZXQiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IG9uTG9hZCAtIExvYWQgYWxsIGl0ZW1zIGluIHRoZSBzeXN0ZW0iLCJ1c2VyIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudG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ifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Build & run:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxc6to4nmzekge41c3w9.png)

## Collision Detection with the Ball
Now that we have our Ball spawning in the center of the screen and moving, let's get to the interesting part of the game: making the ball bounce when it collides with a PlayerPaddle or the top and bottom edges of the game.

Add the following code, which overrides the onCollisionStart method within the Ball component.

`@override
 @mustCallSuper
 void onCollisionStart(
 Set<Vector2> intersectionPoints,
   PositionComponent other,
 ) {
 super.onCollisionStart(intersectionPoints, other);
 final collisionPoint = intersectionPoints.first;

 // TODO: add edges collision update
 
  // TODO: add player paddle collision update
 
 // TODO: add ai paddle collision update
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiI4YTZhMmE0Yy1hOTRiLTQ0OTYtODA1Ny1jOTE5ZmI5OTQ3NzciLCJhc3NldCI6IjNkYWYzNDZlLWJjYjQtNDlkNy04ZmI1LWMxMWY5YTU5OGI5ZCIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJvbl9jb2xsaXNpb25fc3RhcnQiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IE92ZXJyaWRlIG9uQ29sbGlzaW9uIHRvIGNhbGwgc3VwZXIgY2xhc3MgbW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OGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLnBpZWNlcy5jbG91ZCJ9LCJ2YW5pdHkiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vdGFrcnV0dmlrLnBpZWNlcy5jbG91ZCJ9fSwic3RhdHVzIjp7ImNsb3VkIjoiUlVOTklORyJ9LCJwcm9qZWN0IjoicnVudGltZS11c2VyLWNsb3VkLWdyb3VwLTAiLCJ1cGRhdGVkIjp7InZhbHVlIjoiMjAyMi0wNy0xNFQwNjowOTo0NC42MzUzODFaIiwicmVhZGFibGUiOiI4IG1vbnRocyBhZ28ifSwidmVyc2lvbiI6IjMuMS4wIn19fQ==&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

This callback provides us with the intersection/collision points for our component and the ref to the component we are colliding with. These will be useful in the next section, where we deal with collision logic for different bodies.

## Edge Collision Update
We’ll first update the ball velocity to bounce off of the top and bottom edges of the screen. Replace the // TODO: add edge collision update with the following code:

`if (other is ScreenHitbox) {
 // Left Side Collision
 if (collisionPoint.x == 0) {
 // TODO: update player score
     }
 // Right Side Collision
 if (collisionPoint.x == gameRef.size.x) {
 // TODO: update ai score
     }
 // Top Side Collision
 if (collisionPoint.y == 0) {
       velocity.x = velocity.x;
       velocity.y = -velocity.y;
   // TODO: play the collision sound
     }
 // Bottom Side Collision
 if (collisionPoint.y == gameRef.size.y) {
       velocity.x = velocity.x;
       velocity.y = -velocity.y;
  // TODO: play the collision sound
     }
   }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiJhZjY5Yzc2OC1hOTUyLTQxOTgtYmUzMy1iNjlhYzAyNmE2MGYiLCJhc3NldCI6IjNjN2E5NjkwLTU1OGItNDc0OS1hMmQ4LWYzZWE1NDMxYWEzMiIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJlZGdlX2N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1cmwiOiJodHRwczovL3VzZXItOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLWZoY21iaGVrbHEtdWMuYS5ydW4uYXBwIn0sImlkIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovLzhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS5waWVjZXMuY2xvdWQifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Here, we’re first checking if the body that our ball collided with is ScreenHitBox or not. If it is, we check for the edge with which our ball collided.

We don’t want the ball to bounce off of the left and right edges. We’ll later add some code there to update the players’ scores.

If it’s the top or bottom edge, we reverse the ball’s velocity in the y direction. Test it by changing the ball’s spawnAngle to 90 such that it’ll be thrown towards the top or bottom edges.

Build & run:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8fwol7l7kzrhncvpo30.png)

## Paddle Collision Update
Replace the // TODO: paddle collision update with the following:

`if (other is PlayerPaddle) {
 final paddleRect = other.paddle.toAbsoluteRect();
 
     updateBallTrajectory(collisionPoint, paddleRect);
 // TODO: play the collision sound
   }`

If the collided object is the PlayerPaddle, we first calculate the paddleRect, which is the bounding rectangle of the component in the global coordinate space.

Within the `Ball` component, add the following method:

`void updateBallTrajectory(Vector2 collisionPoint, Rect paddleRect) {
 final isLeftHit = collisionPoint.x == paddleRect.left;
 final isRightHit = collisionPoint.x == paddleRect.right;
 final isTopHit = collisionPoint.y == paddleRect.bottom;
 final isBottomHit = collisionPoint.y == paddleRect.top;
 
 final isLeftOrRight = isLeftHit || isRightHit;
 final isTopOrBottom = isTopHit || isBottomHit;
 
 if (isLeftOrRight) {
     velocity.x = -velocity.x;
     velocity.y = velocity.y;
   }
 if (isTopOrBottom) {
     velocity.x = velocity.x;
     velocity.y = -velocity.y;
   }
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiIyNDVkYzI5OS1hYjg5LTRlNjctOTUzMC1iNzc1ZjgzMzIwZGEiLCJhc3NldCI6IjAzNjA3MjJmLTQyODYtNGQxNi04NGJmLTQzMThkMGI4YmViZSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJ1cGRhdGVCYWxsVHJhamVjdG9yeSIsImRlc2NyaXB0aW9uIjoi8J+SoSBTbWFydCBEZXNjcmlwdGlvbjogVXBkYXRlIHRoZSBiYWxsIHRyYWplY3RvcnkgYmFzZWQgb24gdGhlIGNvbGxpc2lvbiBwb2ludC4iLCJ1c2VyIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIXzEifSwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BRmRadWNydWhXV09BSXFpODBDQzBRSEVicTFoYkx3SUVnN0IwS1RkdTN5T0RRPXM5Ni1jIiwiZW1haWwiOiJ0YWtydXR2aWtAZ21haWwuY29tIiwiY3JlYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6MTguMjMwWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjE5LjA2NVoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJpZCI6IjhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwiYWVzdGhldGljcyI6eyJ0aGVtZSI6eyJkYXJrIjp0cnVlfSwiZm9udCI6eyJzaXplIjoxNn19LCJhbGxvY2F0aW9uIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIXzEifSwiaWQiOiI4ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEiLCJ1c2VyIjoiOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxIiwidXJscyI6eyJiYXNlIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3VzZXItOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLWZoY21iaGVrbHEtdWMuYS5ydW4uYXBwIn0sImlkIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovLzhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS5waWVjZXMuY2xvdWQifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

This method will reverse the ball’s velocity along the x- or y-axis, depending on where it touches the paddle, which is known by checking the collisionPoint with the paddleRect position. If the collision is on the left or right side, we reverse the velocity along the x-axis. If the collision is on the top or bottom, we reverse the velocity along the y-axis.

Build & run:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xg49vavh1wnas6sd68nr.png)

## Flutter Game: AI Paddle
Now that we’ve got the ball bouncing off the edges and the paddle, let’s add the AI opponent 🤖 you can play against.

It’ll be very similar to how we did the PlayerPaddle; the only part that’s going to be different is how it moves.

Add the following code to a new file called ai_paddle.dart:

`class AIPaddle extends PositionComponent
 with HasGameRef<FlameGame>, CollisionCallbacks {
 late final RectangleHitbox paddleHitBox;
 late final RectangleComponent paddle;
 
 @override
 Future<void>? onLoad() {
 // TODO: implement onLoad
 
 final worldRect = gameRef.size.toRect();
 
   size = Vector2(10, 100);
   position.x = worldRect.width * 0.1;
   position.y = worldRect.height / 2 - size.y / 2;
   paddle = RectangleComponent(
     size: size,
     paint: Paint()..color = Colors.red,
   );
 
   paddleHitBox = RectangleHitbox(
     size: size,
   );
 
   addAll([
     paddle,
     paddleHitBox,
   ]);
 
 return super.onLoad();
 }
}`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiI2YjEzZjg2ZC1lMTIxLTQ4NjItODE4Yy04ZDVmODRmYmRkZDciLCJhc3NldCI6IjVlODY0YzRjLTQ4OWUtNGM0Yy1iYjdkLTk5M2VlZDUzNzk0NyIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJhaV9wYWRkbGUiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IEEgaVBhZGRsZSBpcyBhIHBhZGRsZSB0aGF0IGlzIGEgY29sbGlzaW9uIHdpdGggdGhlIHBhZGQiLCJ1c2VyIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIX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5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Construction of our AI paddle is very similar to the PlayerPaddle, except we position it at the center on the left side.

Don’t forget to add the AIPaddle component to our PongGame component:

`@override
 Future<void> onLoad() async {
   addAll(
     [
       ...
       .....
       AIPaddle(),
     ],
   );
 }`

Build & run:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ekiisdm2t46l2dd37lf.png)

## AI Paddle Movement Logic
There are many different ways to build this AI opponent, control its behavior, detect how it should move, set how fast it should move and decide how challenging it should be to play against.

For our game, we won’t be building an AI that will be literally impossible to beat, just a simple AI that we can play against peacefully. ✌️

Our AI Paddle will follow two rules depending on the ball's position:

1. If the AIPaddle is below the Ball, it should move up towards the ball.

2. If the AIPaddle is above the Ball, it should move down towards the ball.

Following these rules, override the update method for AIPaddle with the following:

`@override
 void update(double dt) {
 // TODO: implement update
 super.update(dt);
 
 final ball = gameRef.children.singleWhere((child) => child is Ball) as Ball;
 
 if (ball.y > position.y) {
     position.y += (400 * dt);
   }
 
 if (ball.y < position.y) {
     position.y -= (400 * dt);
   }
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiI5N2YxNGNmMC0wNDNkLTQwNGItYjYwNS01NjQzYzIyYTcwOTkiLCJhc3NldCI6Ijk4OTllYzg1LTg1NzUtNDQ1MC1iNzMzLWYwN2Q4YjYwZDViNyIsI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OTklORyIsInVybCI6Imh0dHBzOi8vdXNlci04ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEtZmhjbWJoZWtscS11Yy5hLnJ1bi5hcHAifSwiaWQiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLnBpZWNlcy5jbG91ZCJ9LCJ2YW5pdHkiOnsic3RhdHVzIjoiUlVOTklORyIsInVybCI6Imh0dHBzOi8vdGFrcnV0dmlrLnBpZWNlcy5jbG91ZCJ9fSwic3RhdHVzIjp7ImNsb3VkIjoiUlVOTklORyJ9LCJwcm9qZWN0IjoicnVudGltZS11c2VyLWNsb3VkLWdyb3VwLTAiLCJ1cGRhdGVkIjp7InZhbHVlIjoiMjAyMi0wNy0xNFQwNjowOTo0NC42MzUzODFaIiwicmVhZGFibGUiOiI4IG1vbnRocyBhZ28ifSwidmVyc2lvbiI6IjMuMS4wIn19fQ==&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Here, we first get the reference to the ball from our game world. Depending on the earlier rules we defined, we move the AIPaddle up or down.

In some cases, the AIPaddle will follow the ball even if it goes outside the game boundaries; to prevent this, replace the code after we query/get the ball with the following:

`final ballPositionWrtPaddleHeight = ball.y + (size.y);
 final isOutOfBounds =
       ballPositionWrtPaddleHeight > gameRef.size.y || ball.y < 0;
 
 if (!isOutOfBounds) {
 if (ball.y > position.y) {
       position.y += (400 * dt);
     }
 
 if (ball.y < position.y) {
       position.y -= (400 * dt);
     }
   }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiI4MDgyNTMxMy1iNWEyLTQxNWQtODE0MS1hMzA3MDA2ZDE4ODEiLCJhc3NldCI6IjcxOWY0YjYyLTEwNjMtNGJmZi1hYjMyLTM1Y2I5MjRiOTg5ZSIsIm5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I6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3VzZXItOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLWZoY21iaGVrbHEtdWMuYS5ydW4uYXBwIn0sImlkIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovLzhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS5waWVjZXMuY2xvdWQifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Here, we check if the updated position will be within the boundaries of our game world. If it isn’t, we don’t update the position of the paddle.

Build & run:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycfcxcuqb3ew61f18lfy.png)


## AI Collision Update
Within the Ball component’s update method, replace // TODO: add AI paddle collision update with the following:
`
if (other is AIPaddle) {
 final paddleRect = other.paddle.toAbsoluteRect();

      updateBallTrajectory(collisionPoint, paddleRect);

 // TODO: play the collision sound
    }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiJjMjkyZWQ1ZC05NDRiLTRjZmMtYTc0Ni03OTk0M2VhNzI4ODQiLCJhc3NldCI6IjcyNjc5NzljLWRiYzQtNGI5NC1iNGY5LWJjYzJmNTE1NzRjYiIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0b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yJzdGF0dXMiOiJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly91c2VyLThmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS1maGNtYmhla2xxLXVjLmEucnVuLmFwcCJ9LCJpZCI6eyJzdGF0dXMiOiJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly84ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEucGllY2VzLmNsb3VkIn0sInZhbml0eSI6eyJzdGF0dXMiOiJSVU5OSU5HIiwidXJsIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIn19LCJzdGF0dXMiOnsiY2xvdWQiOiJSVU5OSU5HIn0sInByb2plY3QiOiJydW50aW1lLXVzZXItY2xvdWQtZ3JvdXAtMCIsInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjQ0LjYzNTM4MVoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJ2ZXJzaW9uIjoiMy4xLjAifX19&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

Now, our ball will also collide with the AIPaddle and bounce off of it after a collision.

Build & run:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jjd2mtt8xiviq11y5xy.png)

## Add the Scoring System
Now onto the final part of the game— adding the scoring system. Create a new file called score_text.dart and add the following to it:

`class ScoreText extends TextComponent with HasGameRef<PongGame> {
 late int score;
 
 ScoreText.aiScore({
 this.score = 0,
 })  : _textPaint = TextPaint(textDirection: TextDirection.ltr),
 super(
         anchor: Anchor.center,
       );
 
 ScoreText.playerScore({
 this.score = 0,
 })  : _textPaint = TextPaint(textDirection: TextDirection.rtl),
 super(
         anchor: Anchor.center,
       );
 
 late final TextPaint _textPaint;
 
 @override
 Future<void>? onLoad() {
   score = 0;
 final textOffset =
       (_textPaint.textDirection == TextDirection.ltr ? -1 : 1) * 50;
   position.setValues(gameRef.size.x / 2 + textOffset, gameRef.size.y * 0.1);
   text = score.toString();
 
 return super.onLoad();
 }
 
 @override
 void render(Canvas canvas) {
   _textPaint.render(canvas, '$score', Vector2.zero());
 }
}`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiI1MGY1M2FiMi1iNDg2LTQ0YmEtYTE2Mi1lOWNlNzJmMTM4MDQiLCJhc3NldCI6IjkzYzdhMGRkLWM4NzctNGYxYy1iMTg3LTkzMGU2NGVjMDExOSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJEYXJ0IFNuaXBwZXQiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IFNjb3JlVGV4dCBpbXBsZW1lbnRzIHRoZSBzY29yZSB0ZXh0IGNvbXBvbmVudC4iLCJ1c2VyIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIXzEifSwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BRmRadWNydWhXV09BSXFpODBDQzBRSEVicTFoYkx3SUVnN0IwS1RkdTN5T0RRPXM5Ni1jIiwiZW1haWwiOiJ0YWtydXR2aWtAZ21haWwuY29tIiwiY3JlYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6MTguMjMwWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjE5LjA2NVoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJpZCI6IjhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwiYWVzdGhldGljcyI6eyJ0aGVtZSI6eyJkYXJrIjp0cnVlfSwiZm9udCI6eyJzaXplIjoxNn19LCJhbGxvY2F0aW9uIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIXzEifSwiaWQiOiI4ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEiLCJ1c2VyIjoiOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxIiwidXJscyI6eyJiYXNlIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3VzZXItOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLWZoY21iaGVrbHEtdWMuYS5ydW4uYXBwIn0sImlkIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovLzhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS5waWVjZXMuY2xvdWQifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

This ScoreText will hold and display the score for each player. It has two factory constructors; one for aiScore and one for player. Within its onLoad method, we position our scores at the top center and offset them a little in the left or right direction based on whether it’s the player’s or the AI’s score.

We’ve also overridden the render method to show the latest score as it’s updated.

Now, within our PongGame component, add the following aiScore and playerScore variables which will hold the ScoreText component:

`late final ScoreText aiPlayer;
late final ScoreText player;`


Update the addAll method by adding these two components:

`aiPlayer = ScoreText.aiScore(),
player = ScoreText.playerScore(),`


Now that we have the score components in place, the next thing we want to do is update the scores whenever the player or the AI scores.

## Update the Score
Within the onCollisionStart method of our Ball component, replace the code from // Left Side Collision to // Right Side Collision with the following:

`// Left Side Collision
 if (collisionPoint.x == 0) {
 final player = gameRef.player;
       updatePlayerScore(player);
     }
 // Right Side Collision
 if (collisionPoint.x == gameRef.size.x) {
 final player = gameRef.aiPlayer;
       updatePlayerScore(player);
     }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiIxMTdmMzc1Yy1hZTBhLTRhMWYtYmI4My1iNTkyODA2ZGViMjYiLCJhc3NldCI6IjdhNTkzNDMzLWQ1ZTQtNGNhOC1iZmUwLWFkZDhhZTM4NjRjNSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJlZGdlX2NvbGxpc2lvbl9zY29yZV91cGRhdGUiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IENhbGxiYWNrIG1ldGhvZCBmb3IgY29sbGlzaW9uIGRldGVjdGlvbi4iLCJ1c2VyIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIXzEifSwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BRmRadWNydWhXV09BSXFpODBDQzBRSEVicTFoYkx3SUVnN0IwS1RkdTN5T0RRPXM5Ni1jIiwiZW1haWwiOiJ0YWtydXR2aWtAZ21haWwuY29tIiwiY3JlYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6MTguMjMwWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5O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Ball component:

`import 'dart:async' as dartAsync;
 
void updatePlayerScore(ScoreText player) {
   player.score += 1;
   dartAsync.Timer(const Duration(seconds: 1), () {
     _resetBall;
   });
 }`
[Save this code](https://shared.code.pieces.app/?compressed=eyJiYXNlIjoiaHR0cHM6Ly90YWtydXR2aWsucGllY2VzLmNsb3VkIiwic2hhcmUiOiIxMTdmMzc1Yy1hZTBhLTRhMWYtYmI4My1iNTkyODA2ZGViMjYiLCJhc3NldCI6IjdhNTkzNDMzLWQ1ZTQtNGNhOC1iZmUwLWFkZDhhZTM4NjRjNSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwidGl0bGUiOiJlZGdlX2NvbGxpc2lvbl9zY29yZV91cGRhdGUiLCJkZXNjcmlwdGlvbiI6IvCfkqEgU21hcnQgRGVzY3JpcHRpb246IENhbGxiYWNrIG1ldGhvZCBmb3IgY29sbGlzaW9uIGRldGVjdGlvbi4iLCJ1c2VyIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIXzEifSwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BRmRadWNydWhXV09BSXFpODBDQzBRSEVicTFoYkx3SUVnN0IwS1RkdTN5T0RRPXM5Ni1jIiwiZW1haWwiOiJ0YWtydXR2aWtAZ21haWwuY29tIiwiY3JlYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6MTguMjMwWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInVwZGF0ZWQiOnsidmFsdWUiOiIyMDIyLTA3LTE0VDA2OjA5OjE5LjA2NVoiLCJyZWFkYWJsZSI6IjggbW9udGhzIGFnbyJ9LCJpZCI6IjhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MSIsIm5hbWUiOiJSdXR2aWsgVGFrIiwiYWVzdGhldGljcyI6eyJ0aGVtZSI6eyJkYXJrIjp0cnVlfSwiZm9udCI6eyJzaXplIjoxNn19LCJhbGxvY2F0aW9uIjp7InNjaGVtYSI6eyJtaWdyYXRpb24iOjAsInNlbWFudGljIjoiTUFKT1JfMF9NSU5PUl8wX1BBVENIXzEifSwiaWQiOiI4ZjIwMWQ4OC1mYTA5LTRiYzItODQyNi05NzZmYjRkNWM3OTEiLCJ1c2VyIjoiOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxIiwidXJscyI6eyJiYXNlIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3VzZXItOGYyMDFkODgtZmEwOS00YmMyLTg0MjYtOTc2ZmI0ZDVjNzkxLWZoY21iaGVrbHEtdWMuYS5ydW4uYXBwIn0sImlkIjp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovLzhmMjAxZDg4LWZhMDktNGJjMi04NDI2LTk3NmZiNGQ1Yzc5MS5waWVjZXMuY2xvdWQifSwidmFuaXR5Ijp7InN0YXR1cyI6IlJVTk5JTkciLCJ1cmwiOiJodHRwczovL3Rha3J1dHZpay5waWVjZXMuY2xvdWQifX0sInN0YXR1cyI6eyJjbG91ZCI6IlJVTk5JTkcifSwicHJvamVjdCI6InJ1bnRpbWUtdXNlci1jbG91ZC1ncm91cC0wIiwidXBkYXRlZCI6eyJ2YWx1ZSI6IjIwMjItMDctMTRUMDY6MDk6NDQuNjM1MzgxWiIsInJlYWRhYmxlIjoiOCBtb250aHMgYWdvIn0sInZlcnNpb24iOiIzLjEuMCJ9fX0=&user=8f201d88-fa09-4bc2-8426-976fb4d5c791)

This method takes in the ScoreText object and increments its score by 1. After that, we set up a timer for 1 second to respawn the ball in the center by calling _resetBall.

Now as you or the AI opponent misses the ball, the opposite player will get the point and their score will be updated.

Build & run:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50pgcvqpoq7pdk8ya0h0.png)


## Flutter Game: Adding Collision Audio
A game without audio is definitely not something you would play. So, let’s add a collision sound whenever the ball collides with other game bodies.

Run the following command to add the flame_audio dependency:

`flutter pub add flame_audio`

Once that’s done, download the audio file for the collision sound here. Add the audio files to the assets/audio folder. Make sure to add the audio folder to the assets section in the pubspec as shown:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gh605faab873rib79p99.png)

Let’s add the following method in our Ball component:

`void get _playCollisionAudio {
  FlameAudio.play("ball_hit.wav");
}`

We’ll need to play the collision sound after every collision. Within the onCollisionStart method of the Ball component, replace the //TODO: play the collision sound with:

`_playCollisionAudio`

Final demo:
<iframe title="vimeo-player" src="https://player.vimeo.com/video/744509891?h=fee931dbde" width="640" height="360" frameborder="0"    allowfullscreen></iframe>

## Bonus Flutter Game Material
In the final demo, our Ball speeds up a little when it collides with either the player’s paddle or the AI paddle. For this, we’re simply increasing the ball's velocity in the y-direction by giving it some additional nudgeSpeed. I suggest making the nudge speed 300/200, but you can make it whatever you prefer.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/188gre2osrth0nwryxvf.png)

## Flutter Game: Summary
Congrats! 🥳 We just built a Pong game with Flame!🔥

While building this game, we learned about:

- CollisionDetection API in Flame.
- Building a simple AI opponent.
- Adding a scoring system to the game.
- Adding audio to your game.

You can [download the source code here](https://github.com/rutvik110/Pong-Game).

### Next
Flame has been growing steadily in the Flutter community and many exciting things are coming up in the recent updates. Check out the [Awesome Flame repository](https://github.com/flame-engine/awesome-flame#articles--tutorials) for some amazing examples built with Flame.😋

Flame will continue to grow and allow us to build cool games with Flutter. We at Pieces are really excited about it. Stay tuned for our upcoming articles where we’ll explore Flame to build amazing games! 🎮