snake.py

Created by matilin-pengloan

Created on May 27, 2025

3.49 KB

Snake réaliser par Chat GPT


import random
import kandinsky as kd
import ion
import time

# Dimensions écran complet
LARGEUR = 32  # 320 / 10
HAUTEUR = 24  # 240 / 10
TILE = 10

# Directions
DROITE = (1, 0)
GAUCHE = (-1, 0)
HAUT = (0, -1)
BAS = (0, 1)

# Couleurs
VERT_CLAIR = (200, 255, 200)
VERT_FONCE = (170, 230, 170)
BLEU_TETE = (0, 0, 180)
BLEU_CORPS = (50, 100, 255)
POMME = (255, 50, 50)
TEXTE = (0, 0, 0)

# Dessine un carré plein
def dessiner_case(x, y, couleur):
    for i in range(TILE):
        for j in range(TILE):
            kd.set_pixel(x*TILE+i, y*TILE+j, couleur)

# Dessine le fond damier
def dessiner_fond():
    for x in range(LARGEUR):
        for y in range(HAUTEUR):
            couleur = VERT_CLAIR if (x + y) % 2 == 0 else VERT_FONCE
            dessiner_case(x, y, couleur)

# Dessine la tête (remplie un carré avec bord plus foncé)
def dessiner_tete(x, y):
    for i in range(TILE):
        for j in range(TILE):
            if i == 0 or i == TILE-1 or j == 0 or j == TILE-1:
                kd.set_pixel(x*TILE+i, y*TILE+j, (0, 0, 100))
            else:
                kd.set_pixel(x*TILE+i, y*TILE+j, BLEU_TETE)

# Dessine un segment du corps
def dessiner_segment(x, y):
    for i in range(2, TILE-2):
        for j in range(2, TILE-2):
            kd.set_pixel(x*TILE+i, y*TILE+j, BLEU_CORPS)

# Dessine la pomme au centre de la case
def dessiner_pomme(x, y):
    for i in range(3, 7):
        for j in range(3, 7):
            kd.set_pixel(x*TILE+i, y*TILE+j, POMME)

# Affiche le serpent
def afficher_serpent(snake, old_tail=None):
    if old_tail:
        x, y = old_tail
        couleur = VERT_CLAIR if (x + y) % 2 == 0 else VERT_FONCE
        dessiner_case(x, y, couleur)
    for i, (x, y) in enumerate(snake):
        if i == 0:
            dessiner_tete(x, y)
        else:
            dessiner_segment(x, y)

# Détection des touches
def lire_direction(direction):
    if ion.keydown(ion.KEY_UP) and direction != BAS:
        return HAUT
    if ion.keydown(ion.KEY_DOWN) and direction != HAUT:
        return BAS
    if ion.keydown(ion.KEY_LEFT) and direction != DROITE:
        return GAUCHE
    if ion.keydown(ion.KEY_RIGHT) and direction != GAUCHE:
        return DROITE
    return direction

# Nouvelle pomme
def nouvelle_pomme(snake):
    while True:
        p = (random.randint(0, LARGEUR - 1), random.randint(0, HAUTEUR - 1))
        if p not in snake:
            return p

# Écran Game Over
def game_over(score):
    kd.draw_string("GAME OVER", 100, 100, TEXTE, VERT_CLAIR)
    kd.draw_string("Score: {}".format(score), 100, 120, TEXTE, VERT_CLAIR)

# Lancement du jeu
def main():
    dessiner_fond()
    snake = [(5, 5), (4, 5), (3, 5)]
    direction = DROITE
    pomme = nouvelle_pomme(snake)
    dessiner_pomme(pomme[0], pomme[1])
    vitesse = 0.12
    score = 0

    while True:
        direction = lire_direction(direction)
        dx, dy = direction
        tete_x, tete_y = snake[0]
        nouvelle_tete = (tete_x + dx, tete_y + dy)

        if (nouvelle_tete in snake or
            not 0 <= nouvelle_tete[0] < LARGEUR or
            not 0 <= nouvelle_tete[1] < HAUTEUR):
            game_over(score)
            break

        snake.insert(0, nouvelle_tete)

        if nouvelle_tete == pomme:
            pomme = nouvelle_pomme(snake)
            dessiner_pomme(pomme[0], pomme[1])
            score += 1
            vitesse = max(0.04, vitesse - 0.002)
        else:
            old_tail = snake.pop()
            afficher_serpent(snake, old_tail)

        afficher_serpent(snake)
        time.sleep(vitesse)

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.