pongodot day 5: ball launching

Before getting into the meat of today’s changes, two small changes I made yesterday after I finished the main bulk of yesterday’s changes.

First off I wanted to make it so the ball reacts differently when you hit it with different parts of the paddle. I did this by comparing the height of the ball to the height of the KinematicBody2D that it collides with. Since only the paddles have that Node type I was able to check that I had hit a paddle with is_class()

lines 25-26 are a weird solution to a particular edge case that I’m not going to go into right now

I might play around more with the exact thresholds for where on the paddle we get different angles, but I was happy with this change.

The other small change I did was to add some sprites down the middle of the playfield.

No, this one doesn’t move.

Originally, my plan for today was to get the scoring mechanisms added in, but as I started to break down what that required I realized that it was a bit bigger of a task than my mantra of “one small change a day” for this month-long project. So, I’m breaking that down into smaller chunks.

Today, my goal is to be able to spawn a new ball into the scene, and have everything work. After a point is scored the ball is destroyed and then it is launched by the side that scored. We could simply move the ball, however, I think the system works better if we are able to spawn the ball anew.

So, to start with, we need to create a ball to spawn. There is one in our scene already, but this isn’t really what we are needing. Drawing on my Unity experience, in Unity I would here create a prefab, and it turns out, in Godot we do something similar.

In Godot everything is nodes. Our game scene is really just a node. So, what we need to do is essentially create a new scene that contains our ball node that we can then load in. This works similar to prefabs in Unity, if Unity Scenes were also basically prefabs.

Ball is it’s own scene now.

So, I copied the structure of the Ball in our previous scene, and created a new scene, Ball.tscn. This new scene is where our ball will be housed. Next, we need a way to spawn the ball in.

It’s time for a game controller. In the Game Scene, on the root node I added a new script, called GameController. This script will be responsible for tracking interactions between nodes, as well as eventually tracking and displaying the score. For now, however, it will simply spawn the ball.

short and sweet.

So, let’s go ahead and run the scene and see what happens.

ball is spawned in by the script at the start.

Now, you might notice this broke something. Our “COM” paddle was just following the ball, however, since there is no ball in the tree at the start it’s unable to get it, so we need to instead be able to tell the paddle that there is now a ball available.

We tell the paddle what the ball is

With this change, the com paddle is now able to properly follow the ball. But! We can still do better. Instead of simply spawning the ball at a given location at the start, we instead use that input we cleverly set up yesterday and didn’t use. We’ll wait until the player presses space, and then launch the ball a point a few pixels away from the player paddle. That script looks like this.

no, your script names are inconsistent when it comes to presence of spaces.

Alright, so I’m not totally confident this will work, but let’s go ahead and try it.

working as intended.

Alright, so you might have noticed a few problems there. I should probably instead check for the first frame the spacebar is pressed, and only spawn then, rather than, you know, every frame.

Some quick fixes to the script and it now looks like this:

Should be better.

And with that, the issue is fixed. I can still press the spacebar multiple times to spit out additional balls, but fixing that (as well as deleting balls when you score) is tomorrow’s problem.

there we go.

-Ike

Leave a Reply

Your email address will not be published.