sudo.py

Created by cent20

Created on April 15, 2023

2.63 KB


# Type your text here
from time import sleep
from ion import keydown
from kandinsky import fill_rect, draw_string
from random import randint

def couleur(x,y,p=0):
    if (x//3 + y//3) %2 == 1:
        return (242-42*p,242-42*p,142-42*p)
    else:
        return (142-42*p,242-42*p,242-42*p)


def draw_grid(x, y, nb, gap, w, h):
    for i in range(nb):
        for j in range(nb):
            fill_rect(x+i*(w+gap), y+j*(h+gap), w, h, couleur(i,j))




def quadrillage(xi, yi, xn, yn, larg, haut, e1, col):
    for x in range(xn):
        for y in range(yn):
            fill_rect(xi+x*(larg+e1), yi+y*(haut+e1), larg, haut, col)         


color = [(r,g,b) for r in [42,142,242] for g in [42,142,242] for b in [42,142,242]]

def grille(xi, yi, xn, yn, larg, haut, e1):
   for x in range(xn):
        for y in range(yn):

            fill_rect(xi+x*(larg+e1), yi+y*(haut+e1), larg, haut, couleur(x,y))
            
def dessine_nb(x,y,nb):
    draw_string(str(nb),x*25+55,y*25+2,"black",couleur(x,y,2))
    

grille(50,1,9,9,20,20,5)

pos = [4,1] # départ
pmax = (9,9) # modulo



def wait(buttons=(0,1,2,3,4)):  # Attends qu'une des touches soit pressée
    while True:
        for i in buttons:
            if keydown(i):
                while keydown(i): True  # Fonction anti-rebond
                return i

def deplacement():
# On efface sélection précédente
    x, y = pos_old

    fill_rect(50+x*(20+5), 1+y*(20+5), 20, 20,(255,255,255))
    fill_rect(50+x*(20+5), 1+y*(20+5), 20, 20,couleur(x,y))

    x, y = pos
    fill_rect(50+x*(20+5), 1+y*(20+5), 20, 20,couleur(x,y,2))
    dessine_nb(x,y,9)

def affichage():
    draw_string(str(pos_old), 0, 0)
    draw_string(str(pos), 0, 20)
    #draw_string("couleur = "+str(color[pos[0]+pos[1]*pmax[1]])+"   ", 40, 170)

while True:
    key_pressed = None    
    key_pressed = wait()

    pos_old = list(pos) # On crée une copie indépendante
    
    if key_pressed == 1:  # fleche haut
        pos[1] = (pos[1]-1) % pmax[1]
    
    elif key_pressed == 2: # fleche bas
        pos[1] = (pos[1]+1) % pmax[1]
    
    elif key_pressed == 3: # fleche droite
        pos[0] = (pos[0]+1) % pmax[0]
    
    elif key_pressed == 0: # fleche gauche
        pos[0] = (pos[0]-1) % pmax[0]  
    elif  key_pressed == 4:
        print (4)
    elif  key_pressed == 5:
        print (5)
    elif  key_pressed == 6:
        print (6)
    elif  key_pressed == 7:
        print (7)
    elif  key_pressed == 8:
        print (8)
    elif  key_pressed == 9:
        print (9)
    elif key_pressed == 4: # Touche OK
        pass # Pas d'action ici
      
        
    if key_pressed != None:
        deplacement()
        #affichage()