Let’s combine all our
To
After each call we should
To start we’ll make a new action called draw_stage
, and we’ll start our draw loop.
Let’s get started with our paddle. We want a paddle that we can draw_stage
action to draw the paddle.
Let’s make variables called paddle_height
and paddle_width
. For the //
) to make sure we round down to a
Let’s add the following code to the
Now we can make 2 more variables, paddle_x
and paddle_y
, these are the x and y paddle_x
to be 0
. To get the paddle on the bottom you might be tempted to set paddle_y
to be fad_y@stáitse
(the height of the stage), but this won’t work, we need to subtract the paddle height to account for
Now our variables at the top of the program look like this:
paddle_height := 20
paddle_width := fad_x@stáitse // 5
paddle_x := 0
paddle_y := fad_y@stáitse - paddle_height
Now that we know where our paddle is, and how big it is, let’s add the logic on how to draw it to the draw_stage
action.
Before we draw our paddle, we should glan@stáitse
action to do this. “Glan” translates as “clean” or “clear”, and it clears the stage.
After we clear the stage we can dath@stáitse("dearg")
to change the colour to “dearg” (red).
Now we can draw our paddle! We can use the dron_lán@stáitse
action (short for “dronuilleog lán” meaning “
Now our draw_stage
action looks like this:
gníomh draw_stage() {
>-- Clear the stage
glan@stáitse()
dath@stáitse("dearg") >-- Red pen
dron_lán@stáitse(paddle_x, paddle_y, paddle_width, paddle_height)
}
Let’s try out our program now:
You should see a
Now that we can draw the paddle, how do we move it? We can use the “méarchlár” (meaning “
First let’s make a new action called key_control
. This is the action that will be called when the user presses a
Let’s fill in scríobh(key)
for now.
Now we can tell Setanta to use this action with the keyboard by passing it to the méarchlár@stáitse
action by calling méarchlár@stáitse(key_control)
. Let’s add that to our program.
Run this program, press the
You should see that “ArrowRight”, “ArrowLeft”, “ArrowDown” or “ArrowUp” have been printed in your console. Each key you press has a name that describes it, we can check the name of the key that was pressed in the key_control
action to behave differently depending on what key was pressed.
Note that the name of the keys are in English, this is because these names don’t come from Setanta, they come from the browser.
Let’s add a paddle_speed
to control the
Then we can change the code in the key_control
action to change the paddle_x
variable to move the paddle left and right depending on the key that was pressed:
paddle_speed := 50
gníomh key_control(key) {
má key == "ArrowLeft" {
paddle_x -= paddle_speed
} nó má key == "ArrowRight" {
paddle_x += paddle_speed
}
}
As you can see if the left arrow is pressed we
Try out the code now: The paddle should move when you press the arrow keys!
Next let’s add a ball_x
and ball_y
, let’s start the ball in the top left corner (0, 0).
Let’s add another variable for the ball_rad
, and start it with a value of 30.
ball_dx
and ball_dy
. ball_dx
is the change of the ball in the x direction, and ball_dy
is the change in the y direction. We’ll start with a value of 2 for each, meaning the ball will start off moving
Let’s add the draw_stage
action. We’ll switch the colour of the pen to ciorcal_lán
(meaning “
We want the ball to move, let’s make a new action called update_ball
that will be called in our draw loop. We should use this action to move the ball a ball_dx
and ball_dy
specifically).
Now we include a call to update_ball
into our draw loop
>-- Loop forever
nuair-a fíor {
>-- Call the draw_stage action
draw_stage()
>-- Update ball
update_ball()
>-- Sleep for a few milliseconds
codladh(10)
}
Let’s run the code we have so far:
Oh no! The ball just flies off the screen! This is because we haven’t programmed in what the ball should do when it hits the paddle or the
To add bouncing logic we should change our update_ball
action. Let’s start with the walls and we’ll do the paddle logic after.
Before we
For example, if the x coordinate is going to be fad_x@stáitse
we should turn the ball around in the x direction. If the y coordinate is less than 0, then the ball is going off the top side so we should change it’s y direction.
Let’s put two new variables in our update_ball
action called pred_x
and pred_y
, these will be the
gníomh update_ball() {
>-- Predicted coordinates
pred_x := ball_x + ball_dx
pred_y := ball_y + ball_dy
>-- We'll do our bounce logic here
>-- After bounce checks, update ball position
ball_x += ball_dx
ball_y += ball_dy
}
Now we x
direction we want to make ball_dx
equal to it’s ball_dy
.
When the ball goes over the bottom edge, the game is stop
action to completely stop the program.
gníomh update_ball() {
>-- Predicted coordinates
pred_x := ball_x + ball_dx
pred_y := ball_y + ball_dy
má pred_x < 0 {
>-- Gone over the left edge
>-- Change ball_dx direction
ball_dx = -ball_dx
}
má pred_x > fad_x@stáitse {
>-- Gone over the right edge
>-- Change ball_dx direction
ball_dx = -ball_dx
}
má pred_y < 0 {
>-- Gone over the top edge
>-- Change ball_dy direction
ball_dy = -ball_dy
}
má pred_y > fad_y@stáitse {
>-- Gone over the bottom edge
scríobh("GAME OVER")
stop()
}
>-- After bounce checks, update ball position
ball_x += ball_dx
ball_y += ball_dy
}
Now that we’ve taken care of the top, left and right edges, we need to take care of the paddle.
When we detect that the ball is going to go past the paddle, we can do an ball_x
is paddle_x
and paddle_x + paddle_width
.
If the ball has touched the paddle we can turn it around as if it hit a wall.
gníomh update_ball() {
>-- Predicted coordinates
pred_x := ball_x + ball_dx
pred_y := ball_y + ball_dy
má pred_x < 0 {
>-- Gone over the left edge
>-- Change ball_dx direction
ball_dx = -ball_dx
}
má pred_x > fad_x@stáitse {
>-- Gone over the right edge
>-- Change ball_dx direction
ball_dx = -ball_dx
}
má pred_y < 0 {
>-- Gone over the top edge
>-- Change ball_dy direction
ball_dy = -ball_dy
}
má pred_y > fad_y@stáitse {
>-- Gone over the bottom edge
scríobh("GAME OVER")
stop()
}
má pred_y > paddle_y {
>-- Ball gone past paddle_y
>-- Check if paddle is underneath
má pred_x >= paddle_x & pred_x <= paddle_x + paddle_width {
>-- Turn ball_dy around
ball_dy = -ball_dy
}
}
>-- After bounce checks, update ball position
ball_x += ball_dx
ball_y += ball_dy
}
Let’s try it out!
Ta-Da!
There are lots of changes we could make to our game to make it
ball_x, ball_y
. Add or subtract the radius of the ball to make the collisions work with the edge of the ball instead.