OLED Bounce
Welcome to Bounce Animation
In this lab, you will animate a bouncing ball inside a border on your OLED display. This is how simple video games work! Let's build something amazing!
In this lesson, you will draw a box around the edge of the display. Then you will make a ball that bounces off the walls. You will use the hline and vline functions to draw the box edges.
How the Ball Moves
The ball has an X position and a Y position. Every loop, you add a small number to X and Y to move the ball. When the ball reaches a wall, you flip the direction by multiplying it by -1.
This is the same math used in old video games like Breakout and Pong.
Draw a Border
First, let's draw a box around the screen edges. We use four line commands:
hlinedraws a horizontal line (going left-right).vlinedraws a vertical line (going up-down).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
What each line does:
machine.Pin(0)andmachine.Pin(1)— set up the two I2C wires.machine.I2C(0, sda=sda, scl=scl)— creates the I2C connection.SSD1306_I2C(width, height, i2c)— creates the display object.oled.hline(0, 0, width - 1, 1)— draws the top edge. Parameters: start x, start y, length, color.oled.hline(0, height - 1, width - 1, 1)— draws the bottom edge at y = 63.oled.vline(0, 0, height - 1, 1)— draws the left edge. Parameters: x position, start y, length, color.oled.vline(width - 1, 0, height - 1, 1)— draws the right edge at x = 127.oled.show()— sends the border to the screen.
Key Idea
hline(x, y, length, color) starts at position (x, y) and draws a line going right for length pixels. vline(x, y, length, color) starts at (x, y) and draws a line going down.
Make a Ball Bounce Around Inside the Wall
Now let's add a bouncing ball. The ball starts in the center of the screen. Each loop, it moves one pixel. When it reaches a wall, it bounces back.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
What each section does:
draw_border()— a function that draws the four walls usinghlineandvline.draw_ball(x, y, ball_size, pixel_color)— draws or erases the ball by setting pixels. Passingpixel_color = 1draws it white;pixel_color = 0erases it.direction_x = 1anddirection_y = -1— the ball starts moving right and up.draw_ball(..., 1)thenoled.show()thendraw_ball(..., 0)— this draws the ball, shows it, then erases it. Next loop, the ball appears one pixel over.if current_x < 2: direction_x = 1— when the ball reaches the left wall, flip to move right.current_x = current_x + direction_x— move the ball by adding the direction number.
Watch Out!
OLED screens can get burn-in if the same image stays on screen too long. Running the bounce animation is fine for a lab session, but do not leave static images on the screen for hours.
Monty's Tip
Try changing ball_size to 4 or 6. Try changing direction_x and direction_y to 2 to make the ball move faster. What happens if you use different speeds for x and y?
Great Work!
You built a bouncing ball animation! This is the core of how arcade games like Breakout work. Next, you will add a sensor to your display to show real-world data.