How to Create Your First Python Game with Pygame

Ever dreamed of creating your own simple video game? Python and Pygame make this dream easy and achievable, even if you’re brand new to programming!

In this beginner-friendly guide, we’ll create a simple yet addictive game where you control a blue square to catch randomly appearing red circles. It’s straightforward, fun, and an excellent way to start your journey into game development!

Step 1: Install Python and Pygame

First things first, ensure Python is installed on your computer. You can download it from python.org.

After installing Python, open your terminal (Command Prompt on Windows, Terminal on macOS) and type:

pip install pygame

Step 2: Setting Up Your Game Window

Start by creating a new file named main.py. Inside, initialize your game window:

import pygame, random
pygame.init()

screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("Catch the Ball")
  • pygame.init() prepares all pygame modules.
  • pygame.display.set_mode() sets the size of your game window.
  • pygame.display.set_caption() sets the title for your window.

Step 3: Creating Game Objects

Now let’s define our player and ball objects:

player = pygame.Rect(200, 200, 30, 30)  # Player square
ball = pygame.Rect(random.randint(0, 370), random.randint(0, 370), 20, 20)  # Randomly placed ball
  • pygame.Rect() defines shapes (in this case, rectangles and squares) by their position and dimensions.

Step 4: Adding Movement and Collision Detection

We’ll create a loop to continuously update our game:

running = True
score = 0
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 24)

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]: player.x -= 4
    if keys[pygame.K_RIGHT]: player.x += 4
    if keys[pygame.K_UP]: player.y -= 4
    if keys[pygame.K_DOWN]: player.y += 4

    player.clamp_ip(screen.get_rect())  # Keeps player within window

    if player.colliderect(ball):
        ball.topleft = random.randint(0, 370), random.randint(0, 370)
        score += 1
  • This loop checks user input (arrow keys) and moves the player accordingly.
  • Collision detection (player.colliderect(ball)) checks if the player touches the ball.
  • The score increases every time the ball is caught.

Step 5: Drawing Your Game

Add these lines within the loop to display everything:

    screen.fill("white")  # Clear the screen
    pygame.draw.rect(screen, "dodgerblue", player)  # Draw player
    pygame.draw.ellipse(screen, "red", ball)  # Draw ball
    screen.blit(font.render(f'Score: {score}', True, "black"), (10, 10))  # Display score

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
  • pygame.draw.rect() and pygame.draw.ellipse() draw our objects.
  • pygame.display.flip() updates the display each frame.
  • clock.tick(60) limits the frame rate.

🎉 Run Your Game!

You can download the complete code from our GitHub repository here: Download full code

Save your file and run it from your terminal:

python main.py

Control the square using arrow keys, catch as many red balls as possible, and have fun!

What’s Next?

Now that you’ve created your first Python game, consider adding new features:

  • Timers
  • Different difficulty levels
  • Sound effects

Keep experimenting, and happy coding! 🚀

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top