snake2.py

Created by maelg0000

Created on December 20, 2024

6.55 KB


from kandinsky import *
from time import sleep
from ion import *
from random import randint

# Couleurs aléatoires
j = randint(0, 255)
k = randint(0, 255)
l = randint(0, 255)

config = {}  # Configuration importée
UP, DOWN, LEFT, RIGHT = 0, 1, 2, 3

# Positionnement des cases
def posx(x): return 10 * x - 5
def posy(y): return 10 * y - 4

class Snake:
    def __init__(self, config, high_score=0, init_len=3):
        self.imported_config = config
        self.high_score = high_score
        self.init_len = init_len
        self.init_snk()
        self.show_score()
        self.start()

    def set_config(self):
        self.x, self.y = 16, 11
        self.body = [(self.x, self.y)] * self.init_len
        self.sleep = 0.01
        self.decrement = 0.0003
        self.inc = 0
        self.direction = UP
        self.tdirection = UP
        self.brd_co = (j, k, 100)
        self.bg_co = (j, k, 200)
        self.snk_co = (j, k, 100)
        self.fd_co = (250, k, l)
        for key, value in self.imported_config.items():
            setattr(self, key, value)

    def init_snk(self):
        self.set_config()
        self.len = self.init_len
        self.eat = []
        fill_rect(0, 0, 320, 222, self.bg_co)
        fill_rect(0, 0, 320, 6, self.brd_co)
        fill_rect(0 ,0, 5, 222, self.brd_co)
        fill_rect(320 - 5, 0, 5, 222, self.brd_co)
        fill_rect(0, 222 - 6, 320 ,6, self.brd_co)
        for _ in range(3): self.spawn_food()
        self.show_score()

    def spawn_food(self):
        while True:
            x = randint(1, 31)
            y = randint(1, 21)
            if (x, y) not in self.body or (x, y) not in self.eat:
                break
        fill_rect(posx(x), posy(y), 10, 10, self.fd_co)
        self.eat.append((x, y))

    def show_score(self, sx=205, sy=6):
        draw_string("Score : {:0>2}".format(self.len - self.init_len), sx, sy)

    def start(self):
        def sub_iter(i1, i2):
            return tuple(v1 - v2 for v1, v2 in zip(i1, i2))

        while True:
            if self.len < len(self.body):
                self.body.pop(0)

            x, y = self.body[0]
            x, y = posx(x), posy(y)
            ddir = sub_iter(self.body[0], self.body[1])
            l, h = 10, 10

            if ddir[0] < 0:  # RIGHT
                l = self.inc
            if ddir[0] > 0:  # LEFT
                l = self.inc
                x += 10 - self.inc
            if ddir[1] < 0:  # DOWN
                h = self.inc
            if ddir[1] > 0:  # UP
                h = 10
                y += 10 - self.inc

            fill_rect(x, y, l, h, self.bg_co)

            x, y = posx(self.x), posy(self.y)
            ddir = sub_iter(self.body[0], self.body[1])
            l, h = 10, 10

            if self.direction == RIGHT: l = self.inc
            if self.direction == LEFT: l = self.inc
            x += 10 - self.inc
            if self.direction == DOWN: h = self.inc
            if self.direction == UP: h = self.inc
            y += 10 - self.inc

            fill_rect(x, y, l, h, self.snk_co)
            sleep(self.sleep)

            if keydown(KEY_EXE):
                while keydown(KEY_EXE):
                    pass
                while not keydown(KEY_EXE):
                    pass
                while keydown(KEY_EXE):
                    pass

            if keydown(KEY_UP) and self.direction != DOWN:
                self.tdirection = UP
            if keydown(KEY_DOWN) and self.direction != UP:
                self.tdirection = DOWN
            if keydown(KEY_RIGHT) and self.direction != LEFT:
                self.tdirection = RIGHT
            if keydown(KEY_LEFT) and self.direction != RIGHT:
                self.tdirection = LEFT

            if self.inc == 10: self.inc = 0
            self.direction = self.tdirection

            if self.direction == UP: self.y -= 1
            if self.direction == DOWN: self.y += 1
            if self.direction == RIGHT: self.x += 1
            if self.direction == LEFT: self.x -= 1

            self.body.append((self.x, self.y))

            if (self.x, self.y) in self.eat:
                self.len += 1
                del self.eat[self.eat.index((self.x, self.y))]
                self.spawn_food()
                self.show_score()

            if self.sleep > self.decrement: 
                self.sleep -= self.decrement
            elif (self.x, self.y) in self.body[1:-1] or not 0 < self.x < 32 or not 0 < self.y < 22:
                fill_rect(5, 6, 310, 210, self.bg_co)
                draw_string("Game Over", 120, 100)
                draw_string("Press EXE to play again", 55, 120)
                draw_string("Press HOME to return home", 55, 137)
                self.high_score = max(self.len - self.init_len, self.high_score)
                draw_string("Highscore : " + str(self.high_score), 80, 190)
                self.show_score(120, 170)
                while True:
                    if keydown(KEY_EXE):
                        break
                    sleep(0.01)

                self.init_snk()
                self.inc += 1

def main_menu():
    fill_rect(0, 0, 320, 222, (0, 0, 0))
    draw_string("Snake Game", 120, 50)
    draw_string("1. Taille petite", 100, 80)
    draw_string("2. Taille moyenne", 100, 100)
    draw_string("3. Taille grande", 100, 120)
    draw_string("Appuyez sur EXE pour commencer", 55, 150)

    current_option = 0
    options = ["Taille petite", "Taille moyenne", "Taille grande"]
    y_positions = [80, 100, 120]

    while True:
        # Mettre en surbrillance l'option sélectionnée
        for i, y_pos in enumerate(y_positions):
            color = (255, 255, 255) if i == current_option else (0, 0, 0)
            draw_string(options[i], 100, y_pos, color)

        # Attente des entrées clavier
        if keydown(KEY_UP):
            current_option = (current_option - 1) % len(options)  # Se déplacer vers le haut
            while keydown(KEY_UP): pass  # Attendre la fin de la pression

        if keydown(KEY_DOWN):
            current_option = (current_option + 1) % len(options)  # Se déplacer vers le bas
            while keydown(KEY_DOWN): pass  # Attendre la fin de la pression

        if keydown(KEY_EXE):
            # Démarrer le jeu avec la taille choisie
            if current_option == 0:
                Snake(config, high_score=0, init_len=3)  # Taille petite
            elif current_option == 1:
                Snake(config, high_score=0, init_len=5)  # Taille moyenne
            elif current_option == 2:
                Snake(config, high_score=0, init_len=7)  # Taille grande
            break

        sleep(0.1)

# Lancer le menu principal
main_menu()

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.