Game Development 7 min read

Python Turtle Pong Game with AI Paddle – Step‑by‑Step Tutorial

This article provides a complete, step‑by‑step Python tutorial that builds a Pong game using the turtle library, including an AI‑controlled paddle, user paddle controls, collision detection, and game‑over logic, with all source code ready to copy and run.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Turtle Pong Game with AI Paddle – Step‑by‑Step Tutorial

This tutorial demonstrates how to create a classic Pong game in Python using the turtle library, featuring an AI opponent paddle that tracks the ball with 100% accuracy and a user‑controlled paddle.

Step 1 – Import turtle and set up the screen

import turtle
import random
s = turtle.Screen()
s.title("Pong")
s.bgcolor("black")
s.setup(width=600, height=400)

Step 2 – Create the ball

ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 4
ball.dy = 4

Step 3 – Create the AI paddle

ai = turtle.Turtle()
ai.speed(0)
ai.shape("square")
ai.color("white")
ai.penup()
ai.goto(-250, 0)
ai.shapesize(stretch_wid=5, stretch_len=1)

Step 4 – Create the player paddle

you = turtle.Turtle()
you.speed(0)
you.shape("square")
you.color("white")
you.penup()
you.goto(250, 0)
you.shapesize(stretch_wid=5, stretch_len=1)

Step 5 – Define a function to move the AI paddle

def move_ai_paddle():
    y = ball.ycor()
    if y > 0:
        ai.sety(ai.ycor() + 2)
    else:
        ai.sety(ai.ycor() - 2)

Step 6 – Define functions to move the player paddle with the keyboard

def paddle2_up():
    y = you.ycor()
    y += 20
    you.sety(y)

def paddle2_down():
    y = you.ycor()
    y -= 20
    you.sety(y)

s.listen()
s.onkeypress(paddle2_up, "Up")
s.onkeypress(paddle2_down, "Down")

Step 7 – Main game loop

while True:
    s.update()
    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)
    # Collision with top/bottom walls
    if ball.ycor() > 190 or ball.ycor() < -190:
        ball.dy *= -1
    # Move AI paddle toward the ball
    if ball.ycor() > ai.ycor():
        ai.sety(ai.ycor() + 4)
    elif ball.ycor() < ai.ycor():
        ai.sety(ai.ycor() - 4)
    # End‑game conditions
    if ball.xcor() > 300:
        turtle.textinput("Game End", "You Loss Pong Game With AI!")
        break
    if ball.xcor() < -300:
        turtle.textinput("Game End", "You Win Pong Game With AI!")
        break
    # Collision with AI paddle
    if (ball.xcor() < -240 and ball.xcor() > -250) and (ball.ycor() < ai.ycor() + 40 and ball.ycor() > ai.ycor() - 40):
        if random.random() < 0.9:  # 90% chance of collision
            ball.dx *= -1
    # Collision with player paddle
    if (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < you.ycor() + 40 and ball.ycor() > you.ycor() - 40):
        ball.dx *= -1

turtle.exitonclick()

The full source code is provided above, ready to copy into any Python editor and run to experience a functional Pong game with an AI opponent.

pythonAIgame developmentTutorialturtlePong
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.