# Leo Salazar # 2/15/25 # Bouncing Ball solution w/ extras import pygame pygame.init() # basic setup screen = pygame.display.set_mode(((800, 600))) pygame.display.set_caption("intro to pygame") clock = pygame.time.Clock() running = True dt = 0 # making Rectangle Objects ball = pygame.Rect(400, 400, 100, 100) # variables for speed and velocity velocity_x = 300 velocity_y = 300 # color stuff color_i = 0 colors = ['red', 'blue', 'green', 'yellow', 'orange', 'purple'] # score stuff score = 0 text = pygame.font.Font(None, 60) # game loop while running: for event in pygame.event.get(): # to quit the game if event.type == pygame.QUIT: running = False # auto moves the ball vel_x distance and vel_y distance every loop ball.x += velocity_x * dt ball.y += velocity_y * dt # bounds checker if ball.left < 0: ball.left = 0 velocity_x *= -1 # flipping velocity velocity_x += 10 # making it faster color_i = (color_i + 1) % len(colors) # changing the color score += 1 # incrementing the score # the same for the other 3 sides if ball.right > screen.get_width(): ball.right = screen.get_width() velocity_x += 10 velocity_x *= -1 color_i = (color_i + 1) % len(colors) score += 1 if ball.top < 0: ball.top = 0 velocity_y *= -1 velocity_y += 10 color_i = (color_i + 1) % len(colors) score += 1 if ball.bottom > screen.get_height(): ball.bottom = screen.get_height() velocity_y += 10 velocity_y *= -1 color_i = (color_i + 1) % len(colors) score += 1 # filling the background as black (always goes first) screen.fill("black") # show score text_box = text.render(f"Score: {score}", None, "white") screen.blit(text_box, (10, 10)) # drawing the ball pygame.draw.ellipse(screen, colors[color_i], ball) # showing our changes on the screen pygame.display.flip() # time > fps dt = clock.tick(60) / 1000 # quits the process (otherwise window wont close) pygame.quit()