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.

As usual, start with the renderings

This is a little pac-Man game. We in the post-8090 generation will definitely encounter it. The yellow dots are us, the red dots are the monsters. This is the original video game.

Then we can create a new game code anywhere and use the package code to implement our own game logic.

1 Importing a Module

from random import choice
from turtle import *
from freegames import floor, vector
Copy the code

Do some initialization

state = {'score': 0}
path = Turtle(visible=False)
writer = Turtle(visible=False)
aim = vector(5.0)
pacman = vector(-40, -80)
ghosts = [
    [vector(-180.160), vector(5.0)],
    [vector(-180, -160), vector(0.5)],
    [vector(100.160), vector(0, -5)],
    [vector(100, -160), vector(-5.0)],
]
tiles = [
    0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.1.1.1.1.1.1.0.1.1.1.1.1.1.1.0.0.0.0.0.1.0.0.1.0.0.1.0.1.0.0.1.0.0.1.0.0.0.0.0.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.0.0.0.0.0.1.0.0.1.0.1.0.0.0.1.0.1.0.0.1.0.0.0.0.0.1.1.1.1.0.1.1.0.1.1.0.1.1.1.1.0.0.0.0.0.1.0.0.1.0.0.1.0.1.0.0.1.0.0.0.0.0.0.0.0.1.0.0.1.0.1.1.1.1.1.0.1.0.0.0.0.0.0.0.0.1.1.1.1.1.1.0.0.0.1.1.1.1.1.1.0.0.0.0.0.0.0.0.1.0.1.1.1.1.1.0.1.0.0.1.0.0.0.0.0.0.0.0.1.0.1.0.0.0.1.0.1.0.0.1.0.0.0.0.0.1.1.1.1.1.1.1.0.1.1.1.1.1.1.1.0.0.0.0.0.1.0.0.1.0.0.1.0.1.0.0.0.0.0.1.0.0.0.0.0.1.1.0.1.1.1.1.1.1.1.1.1.0.1.1.0.0.0.0.0.0.1.0.1.0.1.0.0.0.1.0.1.0.1.0.0.0.0.0.0.1.1.1.1.0.1.1.0.1.1.0.1.1.1.1.0.0.0.0.0.1.0.0.0.0.0.1.0.1.0.0.0.0.0.1.0.0.0.0.0.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,]Copy the code

This includes scores, paths, Pac-Man’s location, map scenes, and ghost paths.

Pac-man moves

onkey(lambda: change(5.0), 'Right')
onkey(lambda: change(-5.0), 'Left')
onkey(lambda: change(0.5), 'Up')
onkey(lambda: change(0, -5), 'Down')
Copy the code

4 Move the ghost operation

def move() :
    "Move pacman and all ghosts."
    writer.undo()
    writer.write(state['score'])

    clear()

    if valid(pacman + aim):
        pacman.move(aim)

    index = offset(pacman)

    if tiles[index] == 1:
        tiles[index] = 2
        state['score'] + =1
        x = (index % 20) * 20 - 200
        y = 180 - (index // 20) * 20
        square(x, y)

    up()
    goto(pacman.x + 10, pacman.y + 10)
    dot(20.'yellow')

    for point, course in ghosts:
        if valid(point + course):
            point.move(course)
        else:
            options = [
                vector(5.0),
                vector(-5.0),
                vector(0.5),
                vector(0, -5),
            ]
            plan = choice(options)
            course.x = plan.x
            course.y = plan.y

        up()
        goto(point.x + 10, point.y + 10)
        dot(20.'red')

    update()

    for point, course in ghosts:
        if abs(pacman - point) < 20:
            return

    ontimer(move, 100)
Copy the code

There is a need to take the complete source code, please move to the public number: like the code poem. Now that you’re in, you can click “like” before you leave.

Collection series:

Pac-man, Childhood Memories | Python theme month

Snake, childhood Memories | Python theme month

Tic-tac-toe, childhood memories | Python theme month

Memory Palace, childhood memories | Python theme month