diff --git a/snake/snake_template.py b/snake/snake_template.py index 9813891fe9a7c02e807040c258b0e1d96f3b4c63..90e951b9abb357b751307b5d1e84daf17a1abcbc 100644 --- a/snake/snake_template.py +++ b/snake/snake_template.py @@ -1,96 +1,160 @@ -## RUBIKA - SNAKE GAME +# RUBIKA - SNAKE GAME -#### IMPORTS #### +# imports import pygame import random -#### COLOUR DEFINITION #### - -white = (255, 255, 255) -yellow = (255, 255, 102) -black = (0, 0, 0) -red = (213, 50, 80) -green = (0, 255, 0) -blue = (50, 153, 213) +# colors +blanc = (255, 255, 255) +jaune = (255, 255, 102) +noir = (0, 0, 0) +rouge = (213, 50, 80) +vert = (0, 255, 0) +bleu = (50, 153, 213) # Step 1: Defining the window's graphical variables unite = 10 -largeur_zone = 60 -hauteur_zone = 40 -couleur_nourriture = red -couleur_serpent = green -couleur_fond = white +largeur_zone = 60 * unite +hauteur_zone = 40 * unite +couleur_nourriture = rouge +couleur_serpent = vert +couleur_fond = blanc # Step 2.3: Showing a square in the window -def draw_square(x: int, y: int, colour: tuple): - pygame.draw.rect() +def dessiner_carre(x: int, y: int, colour: tuple): + pygame.draw.rect( + console, + colour, + [x, y, unite, unite] + ) + # Step 4: Showing multiple squares from a list of positions and a colour +def dessiner_liste_carres(liste_positions: list, couleur: tuple): + for position in liste_positions: + dessiner_carre(position[0], position[1], couleur) + # Step 3.1: Get a random number between 0 and max (excluded) +def entier_aleatoire(valeur_max: int) -> int: + return random.randint(0, valeur_max-1) + # Step 3.2: Get random coordinates in the zone +def position_aleatoire() -> tuple: + position_0 = entier_aleatoire(largeur_zone) + position_1 = entier_aleatoire(hauteur_zone) + return position_0, position_1 + # Print a message at the top left of the screen in Comic Sans MS, size 20 -def print_message(message: str) : - console.blit(pygame.font.SysFont("comicsansms", 20).render(message, True, white), [0, 0]) +def affiche_message(message: str): + console.blit(pygame.font.SysFont("comicsansms", 20).render(message, True, blanc), [0, 0]) + # Step 7: Direction according to the pressed key +def actualiser_direction(keys: pygame.key, direction_actuelle: tuple) -> tuple: + if keys[pygame.K_UP]: + print('key is "up"') + direction_future = (0, -1) + elif keys[pygame.K_DOWN]: + print('key is "down"') + direction_future = (0, 1) + elif keys[pygame.K_LEFT]: + print('key is "left"') + direction_future = (-1, 0) + elif keys[pygame.K_RIGHT]: + print('key is "right"') + direction_future = (1, 0) + else: + direction_future = direction_actuelle + print(f"no keys pressed, direction_future: {direction_future}") + return direction_future + # Step 9: Getting the new position in the current direction +def nouvelle_position(serpent, direction_actuelle): + position = [serpent[-1][0] + direction_actuelle[0], serpent[-1][1] + direction_actuelle[1]] + return position + # Step 10: Checking is the position is in the game area +def dans_zone(position): + x = position[0] + y = position[1] + if x < 0 or x >= largeur_zone: + return False + if y < 0 or y >= hauteur_zone: + return False + return True + # One game turn -def game_turn(snake, end, direction, food): +def tour_jeu(serpent, fin, direction_actuelle, pomme): # Step 6.1: Show the food + dessiner_carre(pomme[0], pomme[1], couleur_nourriture) # Step 6.2: Show the score + affiche_message(str(len(serpent)-1)) # Step 6.3: Show the snake + dessiner_liste_carres(serpent, couleur_serpent) # Update display pygame.display.update() # Retrieve pressed keys for event in pygame.event.get(): - # Step 8.1: × is pressed, exit the game - - # Step 8.2: a key is pressed, update direction - print(event) + if event.type == pygame.QUIT: + fin = True - # Step 11: If the snake is moving + keys = pygame.key.get_pressed() + direction_future = actualiser_direction(keys, direction_actuelle) + position_future = nouvelle_position(serpent, direction_future) + if dans_zone(position_future): + serpent.append(position_future) + if serpent[-1] == pomme: + pomme = position_aleatoire() + else: + serpent = serpent[1:] + return serpent, fin, direction_actuelle, pomme - # Step 12: Is the snake eating? - - return snake, end, direction, food # Initialisation of the game window ==> do not delete pygame.init() + # Step 2.1: replace 100,100 by using the correct variables console = pygame.display.set_mode((largeur_zone, hauteur_zone)) # Set window title -pygame.display.set_caption("Snake by Rubika_BI1_Anim3D") +pygame.display.set_caption("Snakeeeeee") # Step 5: Game elements variables - +serpent = [[largeur_zone//2, hauteur_zone//2]] +fin_jeu = False +direction_serpent = (0, 0) +position_pomme = position_aleatoire() # the clock to make the time move clock = pygame.time.Clock() -tick = 1 +tick = 60 +print(f"Initialization done") # Step 6.4: As long as the game is not over -while False : +while fin_jeu is False: + + print(f"Looping") + # Step 2.2: colour the background - console.fill() + console.fill(noir) # Update the elements - snake_positions, game_end, snake_direction, food_position = game_turn(snake_positions, game_end, snake_direction, food_position) + serpent, fin_jeu, direction_serpent, position_pomme = tour_jeu(serpent, fin_jeu, direction_serpent, position_pomme) + # snake_positions, game_end, snake_direction, food_position = game_turn(snake_positions, game_end, snake_direction, food_position) clock.tick(tick) # End of game print("Game Over") pygame.quit() -quit() \ No newline at end of file +quit()