Play the classic pong game with your friends!
- 1 x micro:bit
- 1 x breakout board
- 2 x ADKeyboards
Connect the micro:bit to the breakout board.
Attach 1 ADKeyboard to Pin 0, and 1 ADKeyboard to Pin 1.
Connect your micro:bit using a Micro USB Cable to your computer.
1x breakout board
We will first start off by defining the variables as well as the start screen of the pong game.
bottom_position_of_A and bottom_position_of_B refers to the Y-coordinate of the position of the bottom half of the 2 pong boards.
ball_horizontal and ball_vertical refers to the direction of which the ball goes.
Now, we will define the functions for moving of the pong boards.
We can't have our pong boards moving off-screen, so we have set a boundary for the movements of the pong board.
Now, we will define the functions for the movement of the pong ball.
ball_bounce() refers to the ball bouncing off the pong boards.
ball_bounce_top_bottom() refers to the ball bouncing off the top and bottom boundaries of the screen.
ball_move() refers to the ball moving in the direction dictated by ball_vertical and ball_horizontal.
Now, we will combine all ball movements into one big function. This function will be called main_game.
Now, we will define the function game_over(). This function shows a string "A loses!" or "B loses!" depending on which player lost, and then resets the game.
Now, we will put main_game and game_over together to form the game itself. Pong board movements are also mapped to their respective buttons on the ADKeyboards.
After all that, download the code into your micro:bit and it's done. Find a friend to play the pong game with you!