Bouncing Ball Explanation

Bouncing Ball Explanation

1. Importing the library

import pygame
  • This imports the pygame library, which is used for game development in Python. It provides tools for graphics, sound, and user interaction.

2. Initializing Pygame

pygame.init()
  • This initializes all the modules in pygame that are necessary for the game to run, such as display, sound, and input handling.

3. Setting up the display

screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Bouncing Ball")
  • pygame.display.set_mode((800, 600)): Creates a window with a size of 800 pixels wide and 600 pixels tall.
  • pygame.display.set_caption("Bouncing Ball"): Sets the title of the window to "Bouncing Ball".

4. Defining colors

WHITE = (255, 255, 255)
RED = (255, 0, 0)
  • Colors in pygame are defined using RGB values.
    • WHITE: Full intensity of red, green, and blue (255, 255, 255).
    • RED: Full intensity of red, with green and blue at 0 (255, 0, 0).

5. Ball setup

ball_x, ball_y = 100, 100
ball_dx, ball_dy = 5, 5 ball_radius = 20
  • ball_x and ball_y: Initial position of the ball (100, 100).
  • ball_dx and ball_dy: Change in position (speed) of the ball in the x and y directions, respectively.
  • ball_radius: The radius of the ball (20 pixels).

6. Main loop

running = True
while running:
  • The while loop keeps the game running until running is set to False.
  • running = True: Initially sets the game to be active.

7. Event handling

for event in pygame.event.get():
if event.type == pygame.QUIT: running = False
  • pygame.event.get(): Retrieves all the events (like mouse clicks, key presses, etc.).
  • Checks if the event is pygame.QUIT (when the user clicks the close button), and exits the loop by setting running = False.

8. Moving the ball

ball_x += ball_dx
ball_y += ball_dy
  • Updates the ball's position by adding ball_dx to ball_x and ball_dy to ball_y.
  • This creates movement.

9. Bouncing off walls

if ball_x - ball_radius < 0 or ball_x + ball_radius > 800:
ball_dx = -ball_dx if ball_y - ball_radius < 0 or ball_y + ball_radius > 600: ball_dy = -ball_dy
  • Checks if the ball hits the left or right edge (ball_x - ball_radius < 0 or ball_x + ball_radius > 800).
    • Reverses the direction of movement in the x-axis by negating ball_dx.
  • Checks if the ball hits the top or bottom edge (ball_y - ball_radius < 0 or ball_y + ball_radius > 600).
    • Reverses the direction of movement in the y-axis by negating ball_dy.

10. Clearing the screen

screen.fill(WHITE)
  • Fills the screen with the color WHITE to clear the previous frame.

11. Drawing the ball

pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius)
  • Draws a circle on the screen.
    • screen: The surface to draw on.
    • RED: The color of the circle.
    • (ball_x, ball_y): The position of the circle.
    • ball_radius: The radius of the circle.

12. Updating the display


pygame.display.flip()
  • Updates the display with the new frame.

13. Quitting the game


pygame.quit()
  • Exits all pygame modules cleanly after the game loop ends.

Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe