Pygame Library

 Pygame Library


1. Installation

First, install Pygame using pip:


pip install pygame

2. Creating Your First Pygame Window

Here’s a simple example to create a window:


import pygame # Initialize Pygame pygame.init() # Set up the display screen = pygame.display.set_mode((800, 600)) # Width=800, Height=600 pygame.display.set_caption("My First Pygame Window") # Main loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: # Close the window running = False # Quit Pygame pygame.quit()
  • pygame.init(): Initializes all Pygame modules.
  • pygame.display.set_mode((width, height)): Creates a window of specified size.
  • pygame.event.get(): Handles events like closing the window or pressing keys.
  • pygame.quit(): Shuts down Pygame properly.

3. Adding Colors

Define colors using RGB values:


# Define colors WHITE = (255, 255, 255) BLUE = (0, 0, 255) # Fill the screen with color screen.fill(WHITE) pygame.display.flip() # Update the display

4. Drawing Shapes

Pygame provides functions to draw shapes:


# Draw a rectangle pygame.draw.rect(screen, BLUE, (50, 50, 200, 100)) # (x, y, width, height) # Draw a circle pygame.draw.circle(screen, (255, 0, 0), (400, 300), 50) # (center_x, center_y), radius # Update the display pygame.display.flip()

5. Handling Keyboard Input

You can detect key presses with pygame.KEYDOWN:


for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: # Left arrow key print("Left arrow pressed!")

6. Adding a Moving Object

Here’s how to move a rectangle on the screen:


x, y = 50, 50 # Initial position speed = 5 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: x -= speed if keys[pygame.K_RIGHT]: x += speed if keys[pygame.K_UP]: y -= speed if keys[pygame.K_DOWN]: y += speed # Clear the screen screen.fill(WHITE) # Draw the rectangle pygame.draw.rect(screen, BLUE, (x, y, 50, 50)) # Update the display pygame.display.flip() pygame.quit()

7. Adding Images

You can load and display images:


image = pygame.image.load("my_image.png") # Replace with your image file screen.blit(image, (100, 100)) # Draw the image at (100, 100) pygame.display.flip()

8. Adding Sounds

Pygame supports audio files like .wav:


pygame.mixer.init() sound = pygame.mixer.Sound("sound.wav") # Load sound sound.play() # Play sound

9. Game Loop Structure

A typical Pygame loop has these steps:

  1. Handle events: User inputs like keyboard or mouse actions.
  2. Update game logic: Update object positions, check collisions.
  3. Draw: Render objects on the screen.

10. Simple Game Example: Bouncing Ball


import pygame # Initialize Pygame pygame.init() # Set up display screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Bouncing Ball") # Define colors WHITE = (255, 255, 255) RED = (255, 0, 0) # Ball setup ball_x, ball_y = 100, 100 ball_dx, ball_dy = 5, 5 ball_radius = 20 # Main loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Move the ball ball_x += ball_dx ball_y += ball_dy # Bounce the ball 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 # Clear the screen screen.fill(WHITE) # Draw the ball pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius) # Update the display pygame.display.flip() pygame.quit()

Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe