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 4 min read

Python Turtle Tom & Jerry Game Tutorial

This tutorial demonstrates how to build a simple Tom & Jerry chase game using Python's turtle graphics, covering interface setup, keyboard controls for the mouse, random positioning of the cat and mouse, collision detection, time tracking, and displaying the final score on the screen.

This article provides a step‑by‑step guide to creating a simple Tom & Jerry chase game with Python’s turtle module. It explains how to set up the main window, register custom GIF shapes for the cat and mouse, and display introductory text such as “Tom & JERRY” and “READY? 3,2,1, GO”.

The game uses keyboard bindings (Up, Down, Left, Right) to move the mouse character, while the cat character moves automatically toward the mouse. Random initial positions are assigned, and the distance between the two turtles is checked each frame; when the cat gets within ten pixels, the game ends, showing “GAME OVER” and the survival time.

import turtle
import time
import random

def up():
    jerry.setheading(90)
    jerry.forward(20)
def down():
    jerry.setheading(270)
    jerry.forward(20)
def left():
    jerry.setheading(180)
    jerry.forward(20)
def right():
    jerry.setheading(0)
    jerry.forward(20)
playground=turtle.Screen()
playground.register_shape('tom.gif')
playground.register_shape('jerry.gif')
playground.onkey(up,'Up')
playground.onkey(down,'Down')
playground.onkey(left,'Left')
playground.onkey(right,'Right')
#监听
playground.listen()

writer=turtle.Turtle()
writer.color('brown')
writer.hideturtle()
writer.penup()
writer.home()
writer.write("Tom & JERRY",align='center',font=("Comic sans MS",50,"bold"))
writer.goto(0,-50)
writer.write("READY?3,2,1,GO",align='center',font=("Comic sans MS",20,"bold"))
time.sleep(3)

writer.clear()

tom=turtle.Turtle()
tom.shape('tom.gif')
tom.penup()
tom.goto(random.randint(-200,200),random.randint(-200,200))
tom.pendown()
tom.pensize(3)
tom.color('blue')

jerry=turtle.Turtle()
jerry.shape('jerry.gif')
jerry.speed(0)
jerry.penup()
jerry.goto(random.randint(-200,200),random.randint(-200,200))
jerry.color('brown')

start=time.time()
while True:
    tom.setheading(tom.towards(jerry))
    tom.forward(5)
    if tom.distance(jerry)<10:
        end=time.time()
        playground.clear()
        jerry.goto(0,0)
        jerry.write("GAME OVER",align='center',font=("Comic sans MS",50,"bold"))
        jerry.goto(0,-50)
        jerry.write("YOU SURVIVED {:.1f} SECONDS".format(end-start),align='center',font=("Comic sans MS",20,"bold"))
        tom.pu()
        tom.goto(-50,-70)
        tom.stamp()
        jerry.pu()
        jerry.goto(50,-70)
        jerry.stamp()
        break

The article also includes screenshots of the start screen, gameplay, and the final result, and provides a link to the original CSDN blog post.

About this article

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

game-developmentTutorialTom and Jerry

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion