[et_pb_section][et_pb_row][et_pb_column type=”2_3″][et_pb_text admin_label=”Text”]
[box type=”info”] Before you do this tutorial you should go through Part 1. Be super careful to read the instructions here before you change code. Where the code goes is just as important as the code itself. [/box]
In the starter project, there are two lines inside the game loop that make the ball move.
This code moves the ball down and to the right on the screen. In order to make the ball bounce off the edges, we’ll replace these constant “10” values with variable values. The amount we move the ball each frame is the ball’s velocity. We’ll use a list of two items to represent this velocity. One line goes in the variable setup section of the code. Only the line with the arrow is new.
The we can replace the lines that move the ball by 10 in each direction by the following.
Make sure you are changing to these lines, not adding them to what is there already.
When you run the project now, nothing will have changed. The ball still moves down and to the right, and sails off the bottom of the screen. But we’re ready to make it bounce!
We want the ball to turn around when it reaches the bottom of the screen. So we add a line after the lines that make the ball move.
Try it. Why 580? The window is 600 tall. The ball is 20 tall. The bottom of the ball is touching the bottom when the top of the ball is at 580.
Challenge:
See if you can make the ball bounce off the other three sides of the window. When you succeed, the ball will bounce around the window until you quit. In the next section, we’ll learn how to test when the ball collides with the paddle.
Next: Pygame Tutorial 3: Collisions
[/et_pb_text][/et_pb_column][et_pb_column type=”1_3″][et_pb_sidebar admin_label=”Sidebar” orientation=”left” area=”et_pb_widget_area_8″ background_layout=”light” /][/et_pb_column][/et_pb_row][/et_pb_section]