Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-11-20· OpenAIAi2 WildChat-4.8M (AI2)

give me a long code for snake game

  1. User

    give me a long code for snake game

  2. Assistant

    Certainly! Below is a long but complete Python implementation for a simple Snake Game using the pygame library. Before you run the code, make sure you have Python installed along with the required library pygame. You can install pygame using:

    pip install pygame
    

    Here is the snake game source code:

    import pygame
    import time
    import random
    
    # Initialize pygame
    pygame.init()
    
    # Colors
    white = (255, 255, 255)
    yellow = (255, 255, 102)
    black = (0, 0, 0)
    red = (213, 50, 80)
    green = (0, 255, 0)
    blue = (50, 153, 213)
    
    # Display size
    display_width = 800
    display_height = 600
    
    # Initialize the game display
    game_display = pygame.display.set_mode((display_width, display_height))
    pygame.display.set_caption("Snake Game by Assistant")
    
    # Clock for controlling game speed
    clock = pygame.time.Clock()
    
    snake_block = 10  # Size of each block of the snake
    snake_speed = 15  # Initial speed of the snake (can be adjusted)
    
    # Fonts
    font_style = pygame.font.SysFont("bahnschrift", 25)
    score_font = pygame.font.SysFont("comicsansms", 35)
    
    
    def our_snake(snake_block, snake_list):
        """Draws the snake on the screen."""
        for x in snake_list:
            pygame.draw.rect(game_display, black, [x[0], x[1], snake_block, snake_block])
    
    
    def your_score(score):
        """Displays the current score on the screen."""
        value = score_font.render("Your Score: " + str(score), True, yellow)
        game_display.blit(value, [0, 0])
    
    
    def message(msg, color):
        """Displays the main game message."""
        mesg = font_style.render(msg, True, color)
        game_display.blit(mesg, [display_width / 6, display_height / 3])
    
    
    def game_loop():
        """Main game loop."""
        game_over = False
        game_close = False
    
        # Initial position of the snake
        x1 = display_width / 2
        y1 = display_height / 2
    
        # Movement variables
        x1_change = 0
        y1_change = 0
    
        # Snake body
        snake_list = []
        length_of_snake = 1
    
        # Food position
        foodx = round(random.randrange(0, display_width - snake_block) / 10.0) * 10.0
        foody = round(random.randrange(0, display_height - snake_block) / 10.0) * 10.0
    
        while not game_over:
    
            while game_close:
                game_display.fill(blue)
                message("You Lost! Press Q-Quit or C-Play Again", red)
                your_score(length_of_snake - 1)
                pygame.display.update()
    
                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_q:
                            game_over = True
                            game_close = False
                        if event.key == pygame.K_c:
                            game_loop()
    
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    game_over = True
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        x1_change = -snake_block
                        y1_change = 0
                    elif event.key == pygame.K_RIGHT:
                        x1_change = snake_block
                        y1_change = 0
                    elif event.key == pygame.K_UP:
                        y1_change = -snake_block
                        x1_change = 0
                    elif event.key == pygame.K_DOWN:
                        y1_change = snake_block
                        x1_change = 0
    
            if x1 >= display_width or x1 < 0 or y1 >= display_height or y1 < 0:
                game_close = True
    
            x1 += x1_change
            y1 += y1_change
            game_display.fill(blue)
            pygame.draw.rect(game_display, green, [foodx, foody, snake_block, snake_block])
            snake_head = []
            snake_head.append(x1)
            snake_head.append(y1)
            snake_list.append(snake_head)
            if len(snake_list) > length_of_snake:
                del snake_list[0]
    
            for block in snake_list[:-1]:
                if block == snake_head:
                    game_close = True
    
            our_snake(snake_block, snake_list)
            your_score(length_of_snake - 1)
    
            pygame.display.update()
    
            if x1 == foodx and y1 == foody:
                foodx = round(random.randrange(0, display_width - snake_block) / 10.0) * 10.0
                foody = round(random.randrange(0, display_height - snake_block) / 10.0) * 10.0
                length_of_snake += 1
    
            clock.tick(snake_speed)
    
        pygame.quit()
        quit()
    
    
    # Run the game loop
    game_loop()
    

    How to play

    • The snake moves automatically.
    • Use the arrow keys (Up, Down, Left, Right) to control the direction of the snake.
    • Eat the green block (food) to grow the snake and increase your score.
    • If the snake runs into itself or the edge of the screen, the game ends.

    When the game is over, you can press C to play again or Q to exit the game.

    Let me know if you need any changes or modifications! Happy coding :)