snakegoogle.py

Created by maelg0000

Created on February 08, 2026

4.53 KB


from kandinsky import *
from ion import *
import time

# --- Config ---
W, H = 320, 222
CASE = 12
DIM = 15
OX, OY = (W - DIM * CASE) // 2, 30
BEST_SCORE = 0

# Couleurs
C_BG1, C_BG2 = color(170, 215, 81), color(162, 209, 73)
C_BORD, C_BARRE = color(87, 138, 52), color(74, 117, 44)
C_TXT = color(255, 255, 255)

cfg = {"degrade": True, "pomme": 2, "fluidite": 3}
seed = 0 # Graine pour l'aleatoire

def draw_apple(x, y, lvl):
    fill_rect(x+2, y+3, 8, 8, color(231, 71, 29))
    if lvl >= 1: fill_rect(x+4, y+5, 2, 2, color(237, 112, 85))
    if lvl >= 2:
        fill_rect(x+5, y+1, 2, 2, color(151, 110, 76))
        fill_rect(x+7, y+1, 2, 2, color(73, 197, 39))

def get_snake_col(i, total):
    if not cfg["degrade"] or total < 10: return color(78, 124, 246)
    ratio = i / total
    r = int(78 - ratio * 55)
    g = int(124 - ratio * 65)
    b = int(246 - ratio * 86)
    return color(r, g, b)

def draw_cell(c, r):
    col = C_BG1 if (c + r) % 2 == 0 else C_BG2
    fill_rect(OX + c*CASE, OY + r*CASE, CASE, CASE, col)

def wait_key():
    global seed
    while True:
        seed += 1 # Incrément pour l'aléatoire pendant l'attente
        if keydown(KEY_UP): return KEY_UP
        if keydown(KEY_DOWN): return KEY_DOWN
        if keydown(KEY_LEFT): return KEY_LEFT
        if keydown(KEY_RIGHT): return KEY_RIGHT
        if keydown(KEY_OK): return KEY_OK
        if keydown(KEY_NINE): return KEY_NINE

def menu():
    sel = 0
    while True:
        fill_rect(0, 0, W, H, C_BORD)
        for i, t in enumerate(["JOUER", "PARAMETRES"]):
            bg = color(255,255,255) if i == sel else C_BARRE
            fg = color(0,0,0) if i == sel else C_TXT
            draw_string(t, (W-len(t)*10)//2, 90+i*35, fg, bg)
        
        k = wait_key()
        if k == KEY_UP or k == KEY_DOWN: sel = 1 - sel
        if k == KEY_OK or k == KEY_NINE: return sel
        time.sleep(0.15)

def settings():
    sel = 0
    opts = ["DEGRADE", "POMME", "FLUIDITE"]
    while True:
        fill_rect(0, 0, W, H, C_BORD)
        draw_string("PARAMETRES", 110, 20, C_TXT, C_BORD)
        for i, name in enumerate(opts):
            bg = color(255,255,255) if i == sel else C_BARRE
            fg = color(0,0,0) if i == sel else C_TXT
            v = ""
            if i==0: v = "OUI" if cfg["degrade"] else "NON"
            elif i==1: v = ["Guez", "Mouais", "Wow"][cfg["pomme"]]
            else: v = str(cfg["fluidite"])
            draw_string(name, 20, 70+i*40, C_TXT, C_BORD)
            draw_string("< " + v + " >", 160, 70+i*40, fg, bg)
        
        draw_string("9: RETOUR", 110, 195, C_TXT, C_BORD)
        k = wait_key()
        if k == KEY_UP and sel > 0: sel -= 1
        elif k == KEY_DOWN and sel < 2: sel += 1
        elif k == KEY_LEFT or k == KEY_RIGHT:
            d = 1 if k == KEY_RIGHT else -1
            if sel==0: cfg["degrade"] = not cfg["degrade"]
            elif sel==1: cfg["pomme"] = max(0, min(2, cfg["pomme"]+d))
            else: cfg["fluidite"] = max(1, min(6, cfg["fluidite"]+d))
        elif k == KEY_NINE: return
        time.sleep(0.15)

def game():
    global seed
    fill_rect(0, 0, W, H, C_BORD)
    fill_rect(0, 0, W, OY, C_BARRE)
    for r in range(DIM):
        for c in range(DIM): draw_cell(c, r)
    
    snk = [[7, 7], [8, 7], [9, 7]]
    d = (-1, 0)
    score = 0
    app = [3, 3]
    draw_apple(OX+app[0]*CASE, OY+app[1]*CASE, cfg["pomme"])
    
    while True:
        seed += 1
        if keydown(KEY_UP) and d != (0, 1): d = (0, -1)
        elif keydown(KEY_DOWN) and d != (0, -1): d = (0, 1)
        elif keydown(KEY_LEFT) and d != (1, 0): d = (-1, 0)
        elif keydown(KEY_RIGHT) and d != (-1, 0): d = (1, 0)
        
        head = [snk[0][0]+d[0], snk[0][1]+d[1]]
        
        if not(0<=head[0]<DIM and 0<=head[1]<DIM) or head in snk:
            time.sleep(1)
            return

        snk.insert(0, head)
        if head == app:
            score += 1
            # Pseudo-random base sur la seed
            app = [(seed % 13) + 1, ((seed * 7) % 13) + 1]
            draw_apple(OX+app[0]*CASE, OY+app[1]*CASE, cfg["pomme"])
        else:
            last = snk.pop()
            draw_cell(last[0], last[1])
        
        for i, p in enumerate(snk):
            fill_rect(OX+p[0]*CASE, OY+p[1]*CASE, CASE, CASE, get_snake_col(i, len(snk)))
        
        # Barre Score
        draw_string("P:" + str(score), 10, 6, C_TXT, C_BARRE)
        draw_string("T:" + str(BEST_SCORE), W-60, 6, C_TXT, C_BARRE)

        time.sleep(0.2 / cfg["fluidite"])
        if keydown(KEY_NINE): return

while True:
    c = menu()
    if c == 0: game()
    else: settings()

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.