Dec 7, 2024
Posted by:
Ever dreamed of developing the next blockbuster game or just want to build something for fun, but thought it was too intimidating? Well, Python's simplicity and the tools available from Pygame make it the perfect place to start.
In this blog, we’ll learn how to make a simple yet engaging game: "Catch the Falling Apples.
"Catch the Falling Apples" is a great beginner project because it teaches core game development concepts:
Pygame is a Python library that provides functions to create 2D games. It handles graphics, sounds, and inputs (keyboard/mouse), making it easier for you to focus on building the gameplay
Before starting, install Pygame using pip:
pip install pygame
In this game:
The code for this game is in a file catch_apples.py
catch_apples.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import pygame import random # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Catch the Falling Apples") clock = pygame.time.Clock() # Colors WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) # Basket dimensions basket_width, basket_height = 100, 20 # Apple dimensions apple_radius = 15 # Set up the basket and falling apples # Basket basket_x = WIDTH // 2 - basket_width // 2 basket_y = HEIGHT - 50 basket_speed = 10 # Apples apple_x = random.randint(0, WIDTH - apple_radius) apple_y = -apple_radius apple_speed = 5 # Score score = 0 font = pygame.font.Font(None, 36) # Create the game's main loop to handle logic, drawing and events running = True while running: screen.fill(WHITE) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Move basket keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and basket_x > 0: basket_x -= basket_speed if keys[pygame.K_RIGHT] and basket_x < WIDTH - basket_width: basket_x += basket_speed # Move apple apple_y += apple_speed if apple_y > HEIGHT: print("Game Over!") running = False # Check collision if ( apple_y + apple_radius > basket_y and basket_x < apple_x < basket_x + basket_width ): score += 1 apple_y = -apple_radius apple_x = random.randint(0, WIDTH - apple_radius) # Draw basket pygame.draw.rect(screen, BLUE, (basket_x, basket_y, basket_width, basket_height)) # Draw apple pygame.draw.circle(screen, RED, (apple_x, apple_y), apple_radius) # Draw score score_text = font.render(f"Score: {score}", True, (0, 0, 0)) screen.blit(score_text, (10, 10)) pygame.display.flip() clock.tick(30) pygame.quit() |
Save the script as catch_apples.py and run it :
python catch_apples.py
Move the basket using the arrow keys to catch apples. Each successful catch earns you a point. Missing an apple ends the game.
In later blogs, I will show you how you can make the game more exciting with the following extensions :
Game development is a rewarding way to learn programming. Start simple, experiment with ideas, and who knows? This small game might be the start of something big!
Happy Coding! 😊