import pygame
import time
import random

pygame.init()

# 색상 설정
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
gray = (200, 200, 200)

# 그리드 크기 설정
width, height = 680, 600
snake_block = 40  # 각 칸 크기
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
pygame.display.set_caption('Snake Game')

clock = pygame.time.Clock()
snake_speed = 8.5

font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)


def show_score(score):
    value = score_font.render(f"Your Score: {score}", True, black)
    screen.blit(value, [10, 10])


def message(msg, color):
    mesg = font_style.render(msg, True, color)
    screen.blit(mesg, [width / 6, height / 3])

# ** 시작 화면 그리기 함수 **
def game_start():
    screen.fill(white)
    pygame.display.update()

    game_start_flag = True
    while game_start_flag:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()
                if event.key == pygame.K_c:
                    game_loop()


# ** 그리드 그리기 함수 **
def draw_grid():
    for x in range(0, width, snake_block):
        for y in range(0, height, snake_block):
            rect = pygame.Rect(x, y, snake_block, snake_block)
            pygame.draw.rect(screen, gray, rect, 1)


# ** 뱀 그리기 함수 **
def draw_snake(snake_block, snake_list):
    for block in snake_list:
        pygame.draw.rect(screen, green, [block[0], block[1], snake_block, snake_block])


# ** 게임 루프 함수 **
def game_loop():
    game_over = False
    game_close = False

    x, y = (width // 2) - (width // 2) % snake_block, (height // 2) - (height // 2) % snake_block
    dx, dy = snake_block, 0

    snake_list = []
    length_of_snake = 3

    food_x = round(random.randrange(0, width - snake_block) / snake_block) * snake_block
    food_y = round(random.randrange(0, height - snake_block) / snake_block) * snake_block

    while not game_over:

        while game_close:
            screen.fill(white)
            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 and dx == 0:
                    dx, dy = -snake_block, 0
                elif event.key == pygame.K_RIGHT and dx == 0:
                    dx, dy = snake_block, 0
                elif event.key == pygame.K_UP and dy == 0:
                    dx, dy = 0, -snake_block
                elif event.key == pygame.K_DOWN and dy == 0:
                    dx, dy = 0, snake_block

        x += dx
        y += dy

        if x >= width or x < 0 or y >= height or y < 0:
            game_close = True

        screen.fill(white)
        draw_grid()
        pygame.draw.rect(screen, red, [food_x, food_y, snake_block, snake_block])
        snake_head = [x, y]
        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

        draw_snake(snake_block, snake_list)
        show_score(length_of_snake - 1)

        pygame.display.update()

        if x == food_x and y == food_y:
            food_x = round(random.randrange(0, width - snake_block) / snake_block) * snake_block
            food_y = round(random.randrange(0, height - snake_block) / snake_block) * snake_block
            length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()
    quit()


game_start()

pip install pygame 해야함 게임 방법 C = 시작 Q = 나가기