pacman.py

Created by gaetan-etienne35

Created on September 22, 2025

1.99 KB


from kandinsky import *
from ion import *
from time import sleep

# Labyrinthe (20 colonnes x 15 lignes)
# # = mur, . = pellet,   = vide
maze = [
 "####################",
 "#........##........#",
 "#.##.###.##.###.##.#",
 "#.................#",
 "#.##.#.######.#.##.#",
 "#....#....##....#..#",
 "####.### ####.###.##",
 "   #.#        #.#   ",
 "####.# ##  ## #.####",
 "#........##........#",
 "#.##.###.##.###.##.#",
 "#..#...........#..#",
 "##.#.#.######.#.#.##",
 "#........##........#",
 "####################"
]

# Joueur (Pac-Man)
px, py = 1, 1
score = 0

# Fantôme (très basique)
gx, gy = 9, 7
gdir = 1

def draw_maze():
    for y,row in enumerate(maze):
        for x,c in enumerate(row):
            if c == "#":
                fill_rect(x*12,y*12,12,12,(0,0,200))
            elif c == ".":
                fill_rect(x*12+5,y*12+5,2,2,(255,255,255))
            else:
                fill_rect(x*12,y*12,12,12,(0,0,0))

def draw_pacman(x,y):
    fill_rect(x*12,y*12,12,12,(255,255,0))

def draw_ghost(x,y):
    fill_rect(x*12,y*12,12,12,(255,0,0))

def can_move(x,y):
    if maze[y][x] == "#": return False
    return True

while True:
    # Efface écran
    fill_rect(0,0,320,240,(0,0,0))

    # Dessin
    draw_maze()
    draw_pacman(px,py)
    draw_ghost(gx,gy)
    draw_string("Score:"+str(score),5,220,(255,255,255),(0,0,0))

    # Déplacements joueur
    if keydown(KEY_UP) and can_move(px,py-1): py-=1
    if keydown(KEY_DOWN) and can_move(px,py+1): py+=1
    if keydown(KEY_LEFT) and can_move(px-1,py): px-=1
    if keydown(KEY_RIGHT) and can_move(px+1,py): px+=1

    # Ramasser pellets
    if maze[py][px] == ".":
        maze[py] = maze[py][:px] + " " + maze[py][px+1:]
        score += 1

    # Déplacement fantôme simple (horizontal)
    if can_move(gx+gdir,gy):
        gx += gdir
    else:
        gdir = -gdir
        gx += gdir

    # Collision avec fantôme
    if px==gx and py==gy:
        draw_string("GAME OVER",100,100,(255,0,0),(0,0,0))
        sleep(2)
        break

    sleep(0.15)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.