Python Turtle Pong Game with AI Paddle
This tutorial walks you through building a fully functional Pong game in Python using the turtle library, featuring an AI paddle that automatically tracks the ball, complete with step‑by‑step code snippets and a complete source listing for easy copy‑paste implementation.
This article provides a step‑by‑step tutorial for creating a Pong game in Python using the turtle module, including an AI‑controlled paddle that always tracks the ball.
Step 1 imports turtle and random , creates the screen, sets its title, background color, and size. Step 2 creates the ball object, sets its shape, color, initial position, and movement vectors ( dx and dy ). Step 3 creates the AI paddle, positioning it on the left side and stretching it vertically. Step 4 creates the player paddle on the right side with the same dimensions. Step 5 defines move_ai_paddle() to move the AI paddle toward the ball’s y‑coordinate. Step 6 defines paddle2_up() and paddle2_down() to move the player paddle with the Up/Down keys and binds these functions to keypress events. Step 7 runs an infinite while True loop that updates the screen, moves the ball, checks for wall collisions, moves the AI paddle, detects scoring conditions, and handles collisions with both paddles, including a 90% chance of collision for the AI paddle.
The complete source code is provided below for direct copying.
<code># Step 1 import set up turtle and Screen
import turtle
import random
s = turtle.Screen()
s.title("Pong")
s.bgcolor("black")
s.setup(width=600, height=400)
# Step 2 Create 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 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 a paddle For You
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 Create Function to move 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 Create a Function to move the your paddle
def paddle2_up():
y = you.ycor()
y += 20
you.sety(y)
def paddle2_down():
y = you.ycor()
y -= 20
you.sety(y)
# Your Paddle control it with key
s.listen()
s.onkeypress(paddle2_up, "Up")
s.onkeypress(paddle2_down, "Down")
# Step 7 Start the game with a while loop
while True:
s.update()
# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# Check for collisions with the walls
if ball.ycor() > 190 or ball.ycor() < -190:
ball.dy *= -1
# Move the robot paddle towards the ball
if ball.ycor() > ai.ycor():
ai.sety(ai.ycor() + 4)
elif ball.ycor() < ai.ycor():
ai.sety(ai.ycor() - 4)
# Check for 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
# Check for collisions with the robot 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
# Check for collisions with the user 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()</code>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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.