import pygame
import sys
import random

# Initialize Pygame
pygame.init()
pygame.font.init()

# Screen dimensions
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (230, 0, 0)
GREEN = (0, 230, 0)

# Set up display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Snake Game')

# Clock to control the game's frame rate
clock = pygame.time.Clock()
SNAKE_SPEED = 15  # Adjust this value to change game speed

# Snake block size
BLOCK_SIZE = 20

# Fonts
font_name = 'Arial'  # Use a font that is installed on your system
font_style = pygame.font.SysFont(font_name, 35)
score_font = pygame.font.SysFont(font_name, 30)

def draw_snake(snake_body):
    for block in snake_body:
        pygame.draw.rect(screen, GREEN, [block[0], block[1], BLOCK_SIZE, BLOCK_SIZE])

def message(msg, color, position):
    mesg = font_style.render(msg, True, color)
    rect = mesg.get_rect(center=position)
    screen.blit(mesg, rect)

def game_loop():
    game_over = False
    game_close = False

    # Starting position
    x = SCREEN_WIDTH / 2
    y = SCREEN_HEIGHT / 2

    # Movement
    dx = 0
    dy = 0

    snake_body = []
    snake_length = 1

    # Food position
    food_x = round(random.randrange(0, SCREEN_WIDTH - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
    food_y = round(random.randrange(0, SCREEN_HEIGHT - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE

    while not game_over:

        while game_close:
            screen.fill(BLACK)
            message("You Lost! Press C to Play Again or Q to Quit", RED, [SCREEN_WIDTH / 2, SCREEN_HEIGHT / 3])
            pygame.display.update()

            # Event handling for game over screen
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    game_over = True
                    game_close = False
                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()

        # Event handling for game play
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key in [pygame.K_LEFT, pygame.K_a] and dx != BLOCK_SIZE:
                    dx = -BLOCK_SIZE
                    dy = 0
                elif event.key in [pygame.K_RIGHT, pygame.K_d] and dx != -BLOCK_SIZE:
                    dx = BLOCK_SIZE
                    dy = 0
                elif event.key in [pygame.K_UP, pygame.K_w] and dy != BLOCK_SIZE:
                    dy = -BLOCK_SIZE
                    dx = 0
                elif event.key in [pygame.K_DOWN, pygame.K_s] and dy != -BLOCK_SIZE:
                    dy = BLOCK_SIZE
                    dx = 0

        # Update snake position
        x += dx
        y += dy

        # Boundary collision
        if x >= SCREEN_WIDTH or x < 0 or y >= SCREEN_HEIGHT or y < 0:
            game_close = True

        # Fill screen
        screen.fill(BLACK)

        # Draw food
        pygame.draw.rect(screen, RED, [food_x, food_y, BLOCK_SIZE, BLOCK_SIZE])

        # Snake body growing mechanism
        snake_head = [x, y]
        snake_body.append(snake_head)

        if len(snake_body) > snake_length:
            del snake_body[0]

        # Check for self collision
        for block in snake_body[:-1]:
            if block == snake_head:
                game_close = True

        # Draw snake
        draw_snake(snake_body)

        # Display score
        score_text = score_font.render(f"Score: {snake_length - 1}", True, WHITE)
        screen.blit(score_text, [10, 10])

        pygame.display.update()

        # Check if snake has eaten the food
        if x == food_x and y == food_y:
            food_x = round(random.randrange(0, SCREEN_WIDTH - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
            food_y = round(random.randrange(0, SCREEN_HEIGHT - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
            snake_length += 1

        clock.tick(SNAKE_SPEED)

    pygame.quit()
    sys.exit()

if name == 'main':
    game_loop()

name is not defined in this code fix the code