def draw_objects(): breakout.clear_screen() # Draw the paddle, ball, and wall of bricks # TODO for brick in bricks: x = breakout.get_x(brick) y = breakout.get_y(brick) width = breakout.get_width(brick) height = breakout.get_height(brick) color = breakout.get_color(brick) breakout.draw_rectangle(x, y, width, height, color) x = breakout.get_x(paddle) y = breakout.get_y(paddle) width = breakout.get_width(paddle) height = breakout.get_height(paddle) color = breakout.get_color(paddle) breakout.draw_rectangle(x, y, width, height, color) x = breakout.get_x(ball) y = breakout.get_y(ball) radius = breakout.get_radius(ball) color = breakout.get_color(ball) breakout.draw_circle(x, y, radius, color) # Tell pygame to actually redraw everything (DON'T CHANGE) pygame.display.flip()
def draw_objects(): # First wipe canvas clean breakout.clear_screen() # Draw the paddle, ball, and wall of bricks breakout.draw_rectangle(breakout.get_x(paddle), breakout.get_y(paddle), breakout.get_width(paddle), breakout.get_height(paddle), breakout.get_color(paddle)) breakout.draw_circle(breakout.get_x(ball), breakout.get_y(ball), breakout.get_radius(ball), breakout.get_color(ball)) for brick in bricks: breakout.draw_rectangle(breakout.get_x(brick), breakout.get_y(brick), breakout.get_width(brick), breakout.get_height(brick), breakout.get_color(brick)) # Tell pygame to actually redraw everything pygame.display.flip()
def ball_update_position(ball): x = breakout.get_x(ball) y = breakout.get_y(ball) x += breakout.get_x_velocity(ball) y += breakout.get_y_velocity(ball) breakout.set_x(ball, x) breakout.set_y(ball, y) # If it hits the side walls, bounce off them if breakout.get_x(ball) >= constants.SCREEN_WIDTH: breakout.set_x_velocity(ball, -breakout.get_x_velocity(ball)) if breakout.get_x(ball) < 0: breakout.set_x_velocity(ball, -breakout.get_x_velocity(ball)) # If it hits the ceiling, bounce off it if breakout.get_y(ball) < 0: breakout.set_y_velocity(ball, -breakout.get_y_velocity(ball))
def ball_update_position(ball): x = breakout.get_x(ball) y = breakout.get_y(ball) x_velocity = breakout.get_x_velocity(ball) y_velocity = breakout.get_y_velocity(ball) new_x = x + x_velocity new_y = y + y_velocity breakout.set_x(ball, new_x) breakout.set_y(ball, new_y) if new_x >= constants.SCREEN_WIDTH - constants.BALL_RADIUS: x_velocity = x_velocity * -1 elif new_x <= constants.BALL_RADIUS: x_velocity = x_velocity * -1 elif new_y <= constants.BALL_RADIUS: y_velocity = y_velocity * -1 breakout.set_x_velocity(ball, x_velocity) breakout.set_y_velocity(ball, y_velocity) if new_y == constants.SCREEN_HEIGHT - constants.BALL_RADIUS: x_velocity = x_velocity y_velocity = y_velocity
running = False # Only if start is True you want the ball to be moving. This boolean is used to keep the ball at the centre before the user hits the UP key if start: ball_update_position(ball) #Update the position of the paddle based on the mouse paddle_update_position(paddle) #Check for collisions if breakout.ball_did_collide_with(ball, paddle, constants.PADDLE_WIDTH, constants.PADDLE_HEIGHT): ball_bounce_off(ball) # If ball went out of bounds, we lose a life, and start # with a new ball. elif (breakout.get_y(ball) > constants.SCREEN_HEIGHT): lives -= 1 ball = breakout.create_new_ball() start = False # If bricks are over, you won! if len(bricks) == 0: running = False; # Else, loop through the entire bricks array to see if the ball collided with any brick else: for brick in bricks: if breakout.ball_did_collide_with(ball, brick, constants.BRICK_WIDTH, constants.BRICK_HEIGHT): ball_bounce_off(ball) bricks.remove(brick)
constants.BRICK_HEIGHT): ball_bounce_off(ball) if breakout.ball_did_collide_with(ball, brick, constants.BRICK_WIDTH, constants.BRICK_HEIGHT): bricks.remove(brick) if len(bricks) == 0: running = False # Update the position of the paddle based on the mouse # TODO # Check for collisions using breakout.ball_did_collide_with(ball, obj, width, height) # TODO y = breakout.get_y(ball) if y >= constants.SCREEN_HEIGHT - constants.BALL_RADIUS: running = False # TODO # If bricks are all broken, you won! # TODO # Else, loop through the entire bricks list to see if the ball collided with any brick # TODO # Redraw everything at the end of the while loop draw_objects()