This article is participating in Python Theme Month. See the link to the event for more details

Hello, everybody. I found another interesting little thing today. I’m an old poem about funny gadgets.

I believe many people have played the game of cat and mouse, the mouse run at will, and the cat to intercept, as long as the cat caught the mouse, then we can lose, the game is over

As usual, start with the renderings

Import packages

import turtle
import time
import random
Copy the code

The first step is to import the package. This game uses the turtle drawing package. Many previous texts have said this package, this sentence does not repeat description.

Action to monitor

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)
Copy the code

In this case, the mouse moves up, down, left and right, but we can control the character. It’s really just controlling the steering and then doing forward(20).

Action to register

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') # listening to the playground. Listen ()Copy the code

The above code is to register the cat and mouse images, this GIF is because they are always running state, we directly show their movements in GIF. Then onkey is the most active registration, and the last execution to up, down, left, right went up.

Set up the brush

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 ", the font = (" Comic sans MS ", 20, "bold")) time. Sleep (3)Copy the code

These are the basic Settings for the brush properties

A drawing of a cat and mouse

Tom = turtle. The turtle () Tom. Shape (' Tom. GIF) Tom. Penup () Tom. Goto (random. Randint (200200), the random. Randint (200200)) tom.pendown() tom.pensize(3) tom.color('blue') jerry=turtle.Turtle() jerry.shape('jerry.gif') jerry.speed(0) Jerry. Penup () jerry. Goto (random. Randint (200200), the random. Randint (200200)) jerry. The color (' brown ')Copy the code

A complete logic

while True: tom.setheading(tom.towards(jerry)) tom.forward(5) if tom.distance(jerry)<10: End =time.time() playground. Clear () jery.goto (0,0) jery.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() breakCopy the code

The last logical update is that the cat is controlled by the computer, so it is controlled by the cat to the mouse, and when the distance between the mouse and the cat is less than 10, the game is over. The end window is displayed.

Overall, the game is not too difficult and can be learned easily. The above idea is still said relatively clear. There is a need to take the complete source code, please move to the public number: like the code poem. Now that I’m in, it’s not easy to be original. Let’s go with a “like”.