Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

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.

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

AIgame-developmentPong

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion