Program a Pong!

Step 6
Step completed?

You learned some basic programming principles in the previous step, it's time to have some fun!

Delete your code (you can save it into another file if you wich to reuse it later), and put the code below instead. Remember to save to run it on your Gamebuino.

from gamebuino_meta import begin, waitForUpdate, display, color, buttons, collide

# Size, speed and initial position of the ball:
ball_size = 3
ball_x_speed = 1
ball_y_speed = 1
ball_x_position = 20
ball_y_position = 20

# Initial size of the player's racket:
player_height = 12
player_width = 3
player_speed = 2

# Initial position of the racket:
player_x_position = 10
player_y_position = 30

# Score
score = 0

while True:
    waitForUpdate()
    display.clear()

    # If you press the UP or DOWN buttons, we move the racket up or down:
    if buttons.repeat(buttons.UP, 0):
        player_y_position = player_y_position - player_speed

    if buttons.repeat(buttons.DOWN, 0):
        player_y_position = player_y_position + player_speed

    # We move the ball
    ball_x_position = ball_x_position + ball_x_speed
    ball_y_position = ball_y_position + ball_y_speed

    # Check if the ball collides with the top, bottom or right of the screen
    # If so, we reverse its direction
    if ball_y_position <= 0:
        ball_y_speed = -ball_y_speed

    if ball_y_position >= 64 - ball_size:
        ball_y_speed = -ball_y_speed

    if ball_x_position + ball_size >= 80:
        ball_x_speed = -ball_x_speed

    # We test if there is a collision between the ball and the player's racket
    if collide.rectRect(ball_x_position, ball_y_position, ball_size, ball_size, 
                        player_x_position, player_y_position, player_width, player_height):
        ball_x_speed = -ball_x_speed
        score = score + 1

    # Check if the ball is out of the screen
    if ball_x_position <= 0:
        score = 0  # Nous remettons le score à 0
        ball_x_speed = -ball_x_speed
				
		# Prevent the racket from getting out the screen
    if player_y_position <= 0:
        player_y_position = 0
    
    if player_y_position >= 64 - player_height:
        player_y_position = 64 - player_height

    # Ball display
    display.fillRect(ball_x_position, ball_y_position, ball_size, ball_size)

    # Display of the player's racket
    display.fillRect(player_x_position, player_y_position, player_width, player_height)
		

Well done, you just launched your first game! Simple, isn't it? Try to play! Use the arrows on your Gamebuino to move the racket, and return the ball.

Look at the code a little: don't worry, it's quite normal if you don't understand everything. But, if you look closely, you will notice things you already know: variables! There are many, but you must have noticed the one called "score": it is equal to 0 when you start the game.

The rules are as follows: as soon as you succeed in sending the ball back, you earn one point. As soon as you miss the ball, your score goes back to 0. Everything is already programmed, you don't have to do it! But there is a problem: the score is not displayed on the screen. That's where you come in!

Challenge #4

Try to display the following sentence on the screen, in green: "My score is X" (where X is of course the value of our score). Normally, you have all the knowledge to do it, but if you block, read the help below!

Need help?

  • Remember: the instruction to display something on the screen is display.print("My text"). You can also replace "My text" with a variable.
  • The instruction to change the color to green is as follows: display.setColor(color.GREEN). Place it just before the instruction to write your text!
  • You can write the display instructions anywhere in the code, but in general, we do it all at the end.

Expected result

Solution

See
from gamebuino_meta import begin, waitForUpdate, display, color, buttons, collide

# Size, speed and initial position of the ball:
ball_size = 3
ball_x_speed = 1
ball_y_speed = 1
ball_x_position = 20
ball_y_position = 20

# Initial size of the player's racket:
player_height = 12
player_width = 3
player_speed = 2

# Initial position of the racket:
player_x_position = 10
player_y_position = 30

# Score
score = 0

while True:
    waitForUpdate()
    display.clear()

    # If you press the UP or DOWN buttons, we move the racket up or down:
    if buttons.repeat(buttons.UP, 0):
        player_y_position = player_y_position - player_speed

    if buttons.repeat(buttons.DOWN, 0):
        player_y_position = player_y_position + player_speed

    # We move the ball
    ball_x_position = ball_x_position + ball_x_speed
    ball_y_position = ball_y_position + ball_y_speed

    # Check if the ball collides with the top, bottom or right of the screen
    # If so, we reverse its direction
    if ball_y_position <= 0:
        ball_y_speed = -ball_y_speed

    if ball_y_position >= 64 - ball_size:
        ball_y_speed = -ball_y_speed

    if ball_x_position + ball_size >= 80:
        ball_x_speed = -ball_x_speed

    # We test if there is a collision between the ball and the player's racket
    if collide.rectRect(ball_x_position, ball_y_position, ball_size, ball_size, 
                        player_x_position, player_y_position, player_width, player_height):
        ball_x_speed = -ball_x_speed
        score = score + 1

    # Check if the ball is out of the screen
    if ball_x_position <= 0:
        score = 0  # Nous remettons le score à 0
        ball_x_speed = -ball_x_speed
				
		# Prevent the racket from getting out the screen
    if player_y_position <= 0:
        player_y_position = 0
    
    if player_y_position >= 64 - player_height:
        player_y_position = 64 - player_height

    # Ball display
    display.fillRect(ball_x_position, ball_y_position, ball_size, ball_size)

    # Display of the player's racket
    display.fillRect(player_x_position, player_y_position, player_width, player_height)
		
		# Score display
    display.setColor(color.GREEN)
    display.print("My score is")
    display.print(score)

Explanations

You have simply put into practice what we have seen before. Pretty easy, isn't it? Just remember one thing: for clarity, display instructions are usually placed at the end. As seen above, the score variable is modified by the program, and we display its new value each time.

Go further

Our score is reset each time the ball hits the left side of the screen. What if we wanted to display the total number of times we sent the ball back? Try creating a variable called "total" and displaying it on the screen in a different color. Observe how we manage the "score" variable throughout the program. All you have to do is do the same with "total", but without the instruction that resets it to zero!

If you want inspiration, here's what we've done:

See
from gamebuino_meta import begin, waitForUpdate, display, color, buttons, collide

# Size, speed and initial position of the ball:
ball_size = 3
ball_x_speed = 1
ball_y_speed = 1
ball_x_position = 20
ball_y_position = 20

# Initial size of the player's racket:
player_height = 12
player_width = 3
player_speed = 2

# Initial position of the racket:
player_x_position = 10
player_y_position = 30

# Score
score = 0
total = 0

while True:
    waitForUpdate()
    display.clear()

    # If you press the UP or DOWN buttons, we move the racket up or down:
    if buttons.repeat(buttons.UP, 0):
        player_y_position = player_y_position - player_speed

    if buttons.repeat(buttons.DOWN, 0):
        player_y_position = player_y_position + player_speed

    # We move the ball
    ball_x_position = ball_x_position + ball_x_speed
    ball_y_position = ball_y_position + ball_y_speed

    # Check if the ball collides with the top, bottom or right of the screen
    # If so, we reverse its direction
    if ball_y_position <= 0:
        ball_y_speed = -ball_y_speed

    if ball_y_position >= 64 - ball_size:
        ball_y_speed = -ball_y_speed

    if ball_x_position + ball_size >= 80:
        ball_x_speed = -ball_x_speed

    # We test if there is a collision between the ball and the player's racket
    if collide.rectRect(ball_x_position, ball_y_position, ball_size, ball_size, 
                        player_x_position, player_y_position, player_width, player_height):
        ball_x_speed = -ball_x_speed
        score = score + 1
        total = total + 1

    # Check if the ball is out of the screen
    if ball_x_position <= 0:
        score = 0  # Nous remettons le score à 0
        ball_x_speed = -ball_x_speed
				
    # Prevent the racket from getting out the screen
    if player_y_position <= 0:
        player_y_position = 0
    
    if player_y_position >= 64 - player_height:
        player_y_position = 64 - player_height

    # Ball display
    display.fillRect(ball_x_position, ball_y_position, ball_size, ball_size)

    # Display of the player's racket
    display.fillRect(player_x_position, player_y_position, player_width, player_height)
		
		# Score display
    display.setColor(color.GREEN)
    display.print("My score is")
    display.print(score)
		    
    display.print("\n")
    
    display.setColor(color.BLUE)
    display.print("Total:")
    display.print(total)

Not bad, is it? Now, let's have some fun with the graphics and the gameplay.

Steps