Tutorials & Doc
Get started (Python)
Change graphics and difficulty

Change graphics and difficulty

Step 7
Step completed?

We have applied what we have learned about variables. Let's play with the graphics and game mechanics!

Challenge #5

Our racket and ball have a height and width, in pixels. These values are defined in variables. Moreover, they are all white.... We would like to make the game a little easier. Try to make the racket and ball a little bigger, and display both in different colors!

Need help?

  • Sizes are stored in variables, find out where they are! The racket has a height and a width, while the ball has only one variable (because its height and width are identical).
  • As far as colour is concerned, it is exactly the same principle as for the text. All you have to do is find where the desired elements are displayed, then add an instruction to change the color just before. For example, for red: display.setColor(color.RED)

Expected result

You are free to choose the sizes and colours of your choice, but here is an example of what you can do!

Solution

See

This is a possible solution, yours may be different!

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

# 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

# Size, speed and initial position of the players's racket:
player_height = 12
player_width = 3
player_speed = 2
player_x_position = 10
player_y_position = 30

# Score
score = 0
total = 0

begin()

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 racket 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 (    (ball_x_position <= player_x_position + player_width)
            and (ball_y_position + ball_size >= player_y_position)
            and (ball_y_position <= player_y_position + 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.setColor(color.PINK)
    display.fillRect(ball_x_position, ball_y_position, ball_size, ball_size)

    # Display of the player's racket
    display.setColor(color.BROWN)
    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)

Explanations

By changing the value of ball_size to 6, our ball is now 6 pixels high, and 6 pixels wide. For the racket, we modified player_height and player_width. That's all! That's all! Finally, we changed the color just before the display of our elements: the brown racket (color.BROWN), and the pink ball (color.PINK). The game is now a little easier!

Go further

Too simple, even? What if we did the opposite: make the game more difficult?

A few tips:

  • Reduce the size of the racket and ball
  • Increase the speed of the ball
  • Offer 2 points instead of 1 for each successful referral
  • ...

We don't give you an example here, but we are sure you will be creative!

We have reached the end of this workshop, and we hope that it will have made you want to go a little further in learning Python. More workshops will be coming soon, but until then, you have enough knowledge to have fun and practice on your side!

Remarks and information

Now that you've done your first steps, you sure can't wait to get started on a real creation and to be able to play it wherever an whenever you want !

However, the development of the Gamebuino's Python environment is very recent, and some features are not available yet at this time.

Code.py is not persistent :

When you save the code.py file onto CIRCUITPY volume of your Gamebuino, it is interpreted directly on the machine. But at this time it is not possible yet to save that file into the SD card and then mount it in CircuitPython without the Gamebuino being connected to a computer and you doing manually the operation. There is still a few things remaining to develop before this can be doable.

For now the only way to have a persistent creation being executed from the Gamebuino's menu is to do it thru the Gamebuino's Arduino/C++ environment.

Steps