pacman.py

Created by julien-bernon

Created on October 23, 2020

7.05 KB


from kandinsky import *
from ion import *
from time import sleep,monotonic

pac_man_pos=[8,8]
vitesse=2
depart=0
precedent=0

def creation_terrain():
    global terrain
    grille=[
        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
        [1,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
        [1,0,1,1,1,1,0,1,1,1,1,1,0,1,0],
        [1,0,1,1,1,1,0,1,1,1,1,1,0,1,0],
        [1,0,1,1,1,1,0,1,1,1,1,1,0,1,0],
        [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [1,0,1,1,1,1,0,1,1,0,1,1,1,1,0],
        [1,0,1,1,1,1,0,1,1,0,1,1,1,1,0],
        [1,0,0,0,0,0,0,1,1,0,0,0,0,1,0],
        [1,1,1,1,1,1,0,1,1,1,1,1,0,1,0],
        [1,1,1,1,1,1,0,1,1,1,1,1,0,0,0],
        [1,1,1,1,1,1,0,1,1,0,0,0,0,0,0],
        [1,1,1,1,1,1,0,1,1,0,1,1,1,1,0],
        [1,1,1,1,1,1,0,1,1,0,1,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]
    ]
    terrain=[[0 for _ in range(29)] for _ in range(29)]

    for i in range(len(grille)):
        for j in range(len(grille[i])):
            terrain[i][j]=grille[i][j]
            terrain[len(terrain)-1-i][j]=grille[i][j]
            terrain[i][len(terrain[i])-1-j]=grille[i][j]
            terrain[len(terrain)-1-i][len(terrain[i])-1-j]=grille[i][j]

def crea_boules():
    global terrain,boules_restantes,nb_boules_restantes
    boules_restantes=[]
    nb_boules_restantes=0
    for i in range(len(terrain)):
        boules_restantes.append([])
        for j in terrain[i]:
            if not j:
                boules_restantes[i].append(True)
                nb_boules_restantes+=1
            else :
                boules_restantes[i].append(False)

def boule(i,j):
    fill_rect(8*j+1,8*i+2,6,4,(255,255,255))
    fill_rect(8*j+2,8*i+1,4,6,(255,255,255))

def dessine_boules():
    global boules_restantes
    for i in range(len(boules_restantes)):
        for j in range(len(boules_restantes[i])):
            if boules_restantes[i][j]:
                boule(i,j)

def dessine_terrain():
    fill_rect(0,0,320,225,(0,0,0))
    draw_string("Temps",240,50,(255,255,0),(0,0,0))
    draw_string("Reste :",240,10,(255,255,0),(0,0,0))
    for i in range(len(terrain)):
        for j in range(len(terrain[i])):
            if terrain[i][j]:
                couleur=(0,0,255)
            else:
                couleur=(0,0,0)
            largeur=3
            hauteur=3
            posx=8*j+3
            posy=8*i+3
            if terrain[i][j]:
                if terrain[i-1][j]:
                    posy-=3
                    hauteur+=3
                if terrain[(i+1)%len(terrain)][j]:
                    hauteur+=3
                if terrain[i][j-1]:
                    posx-=3
                    largeur+=3
                if terrain[i][(j+1)%len(terrain[i])]:
                    largeur+=3
            fill_rect(posx,posy,largeur,hauteur,couleur)

def pacman():
    global terrain,pac_man_pos,boules_restantes,nb_boules_restantes,depart,precedent
    deplacement=[keydown(KEY_UP),keydown(KEY_DOWN),keydown(KEY_LEFT),keydown(KEY_RIGHT)]
    if precedent != int(monotonic()-depart):
        draw_string(str(int(monotonic()-depart))+"  ",240,70,(255,255,0),(0,0,0))
        precedent = int(monotonic()-depart)
    
    if max(deplacement):#on ne dessine que si une touche est appuyée
        #on efface la stripe précédent
        fill_rect(pac_man_pos[0]-2,pac_man_pos[1]-2,12,11,(0,0,0))
        #on définit la prochaine position
        pac_man_pos_temp=[pac_man_pos[0]+vitesse*(deplacement[3]-deplacement[2]),pac_man_pos[1]+vitesse*(deplacement[1]-deplacement[0])]
        
        #on regarde si cette nouvelle position est possible, si oui, on l'affecte
        
        if deplacement[3] and not terrain[((pac_man_pos[1])//8)%29][((pac_man_pos_temp[0]+7)//8)%29]:
            pac_man_pos[0]=pac_man_pos_temp[0]%(29*8)
        elif deplacement[2] and not terrain[((pac_man_pos[1])//8)%29][((pac_man_pos_temp[0]+1)//8)%29]:
            pac_man_pos[0]=pac_man_pos_temp[0]%(29*8)
        elif deplacement[1] and not terrain[((pac_man_pos_temp[1]+7)//8)%29][((pac_man_pos[0])//8)%29]:
            pac_man_pos[1]=pac_man_pos_temp[1]%(29*8)
    
        elif deplacement[0] and not terrain[((pac_man_pos_temp[1]+1)//8)%29][((pac_man_pos[0])//8)%29]:
            pac_man_pos[1]=pac_man_pos_temp[1]%(29*8)    
        
        

        #on regarde si cette nouvelle position correspond à celle d'une boule
        if boules_restantes[pac_man_pos[1]//8][pac_man_pos[0]//8]:
            boules_restantes[pac_man_pos[1]//8][pac_man_pos[0]//8]=False
            fill_rect((pac_man_pos[0]>>3)<<3,(pac_man_pos[1]>>3)<<3,8,8,(0,0,0))
            nb_boules_restantes-=1
        
        #on redessine les boules éventuellement effacées par le passage de pacman
        
        if boules_restantes[pac_man_pos[1]//8-1][pac_man_pos[0]//8]:
            boule(pac_man_pos[1]//8-1,pac_man_pos[0]//8)
    
        if boules_restantes[(pac_man_pos[1]//8+1)%29][pac_man_pos[0]//8]:
            boule(pac_man_pos[1]//8+1,pac_man_pos[0]//8)
    
        if boules_restantes[pac_man_pos[1]//8][pac_man_pos[0]//8-1]:
            boule(pac_man_pos[1]//8,pac_man_pos[0]//8-1)    
    
        if boules_restantes[pac_man_pos[1]//8][(pac_man_pos[0]//8+1)%29]:
            boule(pac_man_pos[1]//8,pac_man_pos[0]//8+1)
        

            draw_string(str(nb_boules_restantes)+"  ",240,30,(255,255,0),(0,0,0))
        if deplacement[3] :
            fill_rect(pac_man_pos[0]+1,pac_man_pos[1],8,8,(255,255,0))
            fill_rect(pac_man_pos[0]+4,pac_man_pos[1]-2,3,2,(255,255,0))
            fill_rect(pac_man_pos[0]+6,pac_man_pos[1]+2,2,2,(0,0,0))
            fill_rect(pac_man_pos[0]+5,pac_man_pos[1]+4,3,3,(255,0,0))
        elif deplacement[2]:
            fill_rect(pac_man_pos[0]-1,pac_man_pos[1],8,8,(255,255,0))
            fill_rect(pac_man_pos[0]+2,pac_man_pos[1]-2,3,2,(255,255,0))
            fill_rect(pac_man_pos[0],pac_man_pos[1]+2,2,2,(0,0,0))
            fill_rect(pac_man_pos[0],pac_man_pos[1]+4,3,3,(255,0,0))
        elif deplacement[0]:
            fill_rect(pac_man_pos[0]+1,pac_man_pos[1],8,8,(255,255,0))
            fill_rect(pac_man_pos[0]-1,pac_man_pos[1]+3,2,3,(255,255,0))
            fill_rect(pac_man_pos[0]+3,pac_man_pos[1]+2,2,2,(0,0,0))
            fill_rect(pac_man_pos[0]+5,pac_man_pos[1]+1,3,3,(255,0,0))
        elif deplacement[1]:
            fill_rect(pac_man_pos[0],pac_man_pos[1],8,8,(255,255,0))
            fill_rect(pac_man_pos[0]+8,pac_man_pos[1]+3,2,3,(255,255,0))
            fill_rect(pac_man_pos[0]+4,pac_man_pos[1]+4,2,2,(0,0,0))
            fill_rect(pac_man_pos[0]+1,pac_man_pos[1]+4,3,3,(255,0,0))
    
def main():
    global depart,nb_boules_restantes,precedent
    creation_terrain()
    dessine_terrain()
    crea_boules()
    dessine_boules()
    fill_rect(pac_man_pos[0],pac_man_pos[1],8,8,(255,255,0))
    fill_rect(pac_man_pos[0]+3,pac_man_pos[1]-2,3,2,(255,255,0))
    fill_rect(pac_man_pos[0]+5,pac_man_pos[1]+2,2,2,(0,0,0))
    fill_rect(pac_man_pos[0]+4,pac_man_pos[1]+4,3,3,(255,0,0))
    depart=monotonic()
    while nb_boules_restantes>0:
        pacman()
        sleep(1/20)
    fill_rect(0,0,320,230,(0,0,0))
    draw_string("Votre temps :",100,70,(255,255,0),(0,0,0))
    draw_string(str(precedent),120,70,(255,255,0),(0,0,0))

main()

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.