Pong, one of the earliest arcade video games, offers a simple yet captivating gameplay experience. It involves two paddles and a ball, with players controlling the paddles to hit the ball back and forth.

The game's simplicity makes it an excellent project for beginners in game development, especially when utilizing Python's PyGame library.

This article explores how to develop a traditional Pong game using PyGame and elevate it by incorporating an AI opponent, adding layers of complexity and fun.

0:00
/0:31

AI Pong


Check out also the YouTube video:


Getting Started with PyGame

PyGame is a set of Python modules designed for writing video games. It includes graphics and sound libraries that help in creating fully featured games and multimedia programs in Python.

Prerequisites

Before we begin, make sure you have the following prerequisites installed:

  • Python 3.x: You can download the latest version of Python from the official website: https://www.python.org/downloads/
  • Pygame: Once you have Python installed, you can install Pygame using pip by running the following command: pip install pygame

This tutorial will continue on the implementation of the previous article:

https://developer-service.blog/creating-a-classical-pong-game-with-pygame-a-step-by-step-tutorial/


Incorporating AI to Control a Paddle

To introduce an AI opponent, we implement a function named ai_movement to control the right paddle's movement.

This function calculates the ideal position for the paddle based on the ball's position and moves it accordingly. The AI uses simple logic: it moves the paddle up if the ball is above its center and down if it is below.

This basic AI makes the game playable against a computer-controlled opponent.

Let's see the corresponding code:

# AI movement
def ai_movement(paddle, ball):
    if ball.y < paddle.centery:
        paddle.top -= 5
    elif ball.y > paddle.centery:
        paddle.bottom += 5

    # Ensure it stays within the screen bounds
    if paddle.top < 0:
        paddle.top = 0
    elif paddle.bottom > HEIGHT:
        paddle.bottom = HEIGHT            

We also need to update our game loop to move the right paddle with the AI movement:

# Game loop
while True:
    # Event handling
    ...

    # Paddle movement left paddle
    ...

    # Paddle movement right paddle (AI-controlled)
    ai_movement(paddle_right, ball)

Let's see now our AI paddle in action:

0:00
/0:28

AI Pong - Hard Implementation

You might think the AI paddle rarely misses the ball, except in edge cases. Of course, this would make it difficult to play against, so let's see how we can implement an easier-to-play AI.


Excited to dive deeper into the world of Python programming? Look no further than my latest ebook, "Python Tricks - A Collection of Tips and Techniques".

Get the eBook

Inside, you'll discover a plethora of Python secrets that will guide you through a journey of learning how to write cleaner, faster, and more Pythonic code. Whether it's mastering data structures, understanding the nuances of object-oriented programming, or uncovering Python's hidden features, this ebook has something for everyone.

How Can AI Miss the Ball?

To make the AI miss the ball occasionally, we can introduce a small random chance for the AI to make a mistake.

We'll modify the ai_movement function to include a random factor that determines whether the AI will move correctly or make a mistake.

Here's the updated ai_movement function:

Tagged in: