That’s right, today it’s time to add paddles to pong. finally time to make this somewhat like a game with a player controlled element.
First step is input. Godot uses an input map through the player settings, allowing multiple inputs to translate to specific actions. For this I added three, padde_up, paddle_down, and an action I called launch, which will be used to launch the ball after a point is scored. This last one will not be used today, since the scoring mechanism is tomorrow’s task.
The input actions we’ll be able to use in code to move the paddle. Speaking of which, it was also time to make the paddles.
The paddles have a similar node structure to the ball, they are KinematicBody2D nodes, with a sprite and a collision shape attached. Putting them both in the scene with no scripting attached made the ball simply bounce off them, which is exactly what we wanted.
First up, the player paddle. The script here was relatively simple, getting the input is the only thing new about this script compared to what we wrote for the ball, and unlike the ball we are using move_and_slide instead of move_and_collide, since I don’t want the paddle to stop moving when you hit the ball.
Next, we’ll do the. Later on I want to play around with an algorithm that will be fun to play against, but for now I’m going to make it super dumb.
The plan is to have the COM paddle simply follow the y coordinate of the ball, however, it’s going to have a slightly lower speed than the ball, so it lags behind a bit, hopefully allowing the player to score sometimes. When we get to the scoring stuff tomorrow I might change this up, but for today’s task it’s all about keeping it simple and stupid.
So here, we use get_node() to get a reference to our Ball object, and just simply move the paddle up and down to follow the ball. We can adjust the speed to make the paddle more likely to miss, but for now this works great.
In thisI am controlling the left paddle with the up and down arrows! As you can see, nothing happens when I miss and the ball goes behind me, but that is tomorrows task. See you then!