[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 Parts 1 and 2. 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 this part of the tutorial we’ll use an if statement to test whether the ball and the paddle are touching each other. If they are, we’ll reverse the direction of the ball.
In the starter project, our paddle and ball were created as lists of four numbers that defined the rectangles where we later drew the objects.
In order to detect collisions, we’ll replace the two rectangle lines with lines that create pygame.Rect objects. These objects can be used just like the lists they replace, but they have other uses as well.
Inside the game loop, we’ll create our if statement.
See how nice this is? We have a function called colliderect that can detect whether the two rectangles intersect. If they do, we reverse the y velocity of the ball. This is a very simplistic way of deciding what to do in this collision, but it’s a good start.
Give it a try. You might see some strange behavior, especially when the ball hits the paddle on the side. It should basically work, though. We can revisit this collision later, but now, lets add some bricks.
Next: Pygame Tutorial Part 4: Bricks
[/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]