
Looking for a fun and beginner-friendly Python project? In this tutorial, you’ll build a real-time Tom & Jerry chase game using only Python and Pygame — without using any external images or sprites.
We’ll draw both Tom and Jerry using Python’s built-in drawing functions like circles, polygons, and ellipses. Perfect for students learning Python graphics and game development!
What You’ll Learn
- Drawing custom cartoon-style characters in Python
- Real-time movement using keyboard inputs
- Randomized character logic
- Collision detection
- Creating a complete playable game with minimal code
Tools You Need
-
Python (3.13.0)
-
Pygame (install via
pip install pygame
) -
Any code editor (we recommend VS Code or Thonny)
Game Overview
-
Tom (a cartoon-style cat) is controlled using the arrow keys.
-
Jerry (a cartoon-style mouse) moves randomly around the screen.
-
If Tom collides with Jerry, the game ends with a “Caught!” message.
All visuals are drawn directly with code — no external images or assets!
Full Python Code
Here’s the full code. Create a file named tom_and_jerry_game.py
and paste the following:
import pygame
import random
import sys
pygame.init()
# Window setup
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(“Tom & Jerry (Drawn Cartoon Style) – Learn Alloid”)
clock = pygame.time.Clock()
FPS = 30
# Colors
WHITE = (255, 255, 255)
GRAY = (120, 120, 120)
BLUE = (50, 50, 200)
BROWN = (139, 69, 19)
BLACK = (0, 0, 0)
PINK = (255, 182, 193)
YELLOW = (255, 255, 0)
# Sizes
TOM_RADIUS = 30
JERRY_RADIUS = 20
# Position
tom_x, tom_y = 100, 100
jerry_x, jerry_y = 500, 300
speed = 5
def draw_tom(surface, x, y):
# Body
pygame.draw.ellipse(surface, GRAY, (x – 25, y + 25, 50, 60)) # Body
pygame.draw.circle(surface, GRAY, (x, y), TOM_RADIUS) # Head
# Ears
pygame.draw.polygon(surface, GRAY, [(x – 30, y – 20), (x – 45, y – 50), (x – 10, y – 35)])
pygame.draw.polygon(surface, GRAY, [(x + 30, y – 20), (x + 45, y – 50), (x + 10, y – 35)])
# Eyes
pygame.draw.circle(surface, WHITE, (x – 10, y – 5), 6)
pygame.draw.circle(surface, WHITE, (x + 10, y – 5), 6)
pygame.draw.circle(surface, BLACK, (x – 10, y – 5), 3)
pygame.draw.circle(surface, BLACK, (x + 10, y – 5), 3)
# Nose
pygame.draw.circle(surface, BLACK, (x, y + 10), 4)
# Mouth
pygame.draw.arc(surface, BLACK, (x – 15, y + 5, 30, 20), 3.14, 2*3.14, 2)
# Tail
pygame.draw.line(surface, GRAY, (x + 20, y + 70), (x + 50, y + 90), 4)
def draw_jerry(surface, x, y):
# Body
pygame.draw.ellipse(surface, BROWN, (x – 15, y + 20, 30, 40)) # Body
pygame.draw.circle(surface, BROWN, (x, y), JERRY_RADIUS) # Head
# Ears
pygame.draw.circle(surface, PINK, (x – 18, y – 15), 10)
pygame.draw.circle(surface, PINK, (x + 18, y – 15), 10)
# Eyes
pygame.draw.circle(surface, WHITE, (x – 6, y – 3), 4)
pygame.draw.circle(surface, WHITE, (x + 6, y – 3), 4)
pygame.draw.circle(surface, BLACK, (x – 6, y – 3), 2)
pygame.draw.circle(surface, BLACK, (x + 6, y – 3), 2)
# Nose
pygame.draw.circle(surface, BLACK, (x, y + 4), 2)
# Whiskers
pygame.draw.line(surface, BLACK, (x – 20, y + 4), (x – 5, y + 4), 1)
pygame.draw.line(surface, BLACK, (x + 5, y + 4), (x + 20, y + 4), 1)
# Tail
pygame.draw.line(surface, BROWN, (x – 10, y + 60), (x – 30, y + 70), 3)
def show_message(text):
font = pygame.font.SysFont(“Arial”, 36)
txt = font.render(text, True, BLACK)
WIN.blit(txt, (WIDTH // 2 – txt.get_width() // 2, HEIGHT // 2 – txt.get_height() // 2))
pygame.display.update()
pygame.time.delay(2000)
def main():
global tom_x, tom_y, jerry_x, jerry_y
run = True
while run:
clock.tick(FPS)
WIN.fill(WHITE)
# Handle quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# Tom movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and tom_x – TOM_RADIUS > 0:
tom_x -= speed
if keys[pygame.K_RIGHT] and tom_x + TOM_RADIUS < WIDTH:
tom_x += speed
if keys[pygame.K_UP] and tom_y – TOM_RADIUS > 0:
tom_y -= speed
if keys[pygame.K_DOWN] and tom_y + TOM_RADIUS < HEIGHT:
tom_y += speed
# Jerry movement (random)
direction = random.choice([‘up’, ‘down’, ‘left’, ‘right’])
if direction == ‘up’ and jerry_y – JERRY_RADIUS > 0:
jerry_y -= speed
elif direction == ‘down’ and jerry_y + JERRY_RADIUS < HEIGHT:
jerry_y += speed
elif direction == ‘left’ and jerry_x – JERRY_RADIUS > 0:
jerry_x -= speed
elif direction == ‘right’ and jerry_x + JERRY_RADIUS < WIDTH:
jerry_x += speed
# Draw characters
draw_tom(WIN, tom_x, tom_y)
draw_jerry(WIN, jerry_x, jerry_y)
# Collision check
tom_rect = pygame.Rect(tom_x – TOM_RADIUS, tom_y – TOM_RADIUS, TOM_RADIUS * 2, TOM_RADIUS * 2)
jerry_rect = pygame.Rect(jerry_x – JERRY_RADIUS, jerry_y – JERRY_RADIUS, JERRY_RADIUS * 2, JERRY_RADIUS * 2)
if tom_rect.colliderect(jerry_rect):
show_message(“Caught Jerry! 🎉🐭”)
run = False
pygame.display.update()
pygame.quit()
sys.exit()
if __name__ == “__main__”:
main()
Make sure you have pygame
installed:
pip install pygame
To run the game:
python tom_and_jerry_game.py
🎥 Watch the Full Tutorial
We also created a full video tutorial for this project on our YouTube channel.
📺 Watch here:
This video walks through:
-
Drawing logic
-
Game loop explanation
-
Real-time gameplay
-
Custom enhancements
💡 Extension Ideas
Once you’re done, try upgrading the project:
-
Add a scoring system
-
Show a timer or high score
-
Add cheese pickups for Jerry
-
Play a sound effect when Jerry is caught
-
Restart game after ending
Challenge for You
Modify Jerry’s behavior to avoid Tom dynamically
Add boundaries like walls or obstacles
Add a start screen and reset button
Post your version and tag us — we’d love to see your creation!
Why This Project Is Perfect for Learners
This Tom & Jerry game is an excellent mini project for:
-
Python beginners
-
Class assignments
-
Summer internships
-
Final-year minor projects
-
Kids and hobby learners
It combines core Python concepts with fun and creativity — all under 100 lines of code!
🔗 Learn More with Learn Alloid
Want to master Python, game dev, and real-world projects like this?
Explore our Python course, internship programs, and 1-week challenge series.