메뉴 건너뛰기

kalmason

White Paper

Please understand my incorrect and poor korean expression

MSWINDOWS python 스네이크바이트

관리자 2025.06.11 20:40 조회 수 : 0

green

blue   먹이

red   폭탄

 

import pygame

import time

import random

 

# 초기 설정

pygame.init()

width, height = 600, 400

dis = pygame.display.set_mode((width, height))

pygame.display.set_caption('Snake Game - Blue = Grow, Red = Shrink')

 

clock = pygame.time.Clock()

snake_block = 10

snake_speed = 15

 

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

score_font = pygame.font.SysFont("comicsansms", 20)

 

# 색상

white = (255, 255, 255)

black = (0, 0, 0)

blue = (0, 0, 255)

red = (255, 0, 0)

green = (0, 255, 0)

 

# 점수 출력

def your_score(score):

    value = score_font.render("Score: " + str(score), True, black)

    dis.blit(value, [0, 0])

 

# 뱀 그리기

def draw_snake(snake_block, snake_list):

    for x in snake_list:

        pygame.draw.rect(dis, green, [x[0], x[1], snake_block, snake_block])

 

# 메시지 출력

def message(msg, color):

    mesg = font_style.render(msg, True, color)

    dis.blit(mesg, [width / 6, height / 3])

 

# 게임 루프

def game_loop():

    game_over = False

    game_close = False

 

    x1 = width / 2

    y1 = height / 2

    x1_change = 0

    y1_change = 0

 

    snake_list = []

    length_of_snake = 1

 

    foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0

    foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0

 

    bombx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0

    bomby = round(random.randrange(0, height - snake_block) / 10.0) * 10.0

 

    while not game_over:

 

        while game_close:

            dis.fill(white)

            message("Game Over! 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

 

        x1 += x1_change

        y1 += y1_change

 

        if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:

            game_close = True

 

        dis.fill(white)

        pygame.draw.rect(dis, blue, [foodx, foody, snake_block, snake_block])  # 파란 먹이

        pygame.draw.rect(dis, red, [bombx, bomby, snake_block, snake_block])   # 빨간 폭탄

 

        snake_head = [x1, y1]

        snake_list.append(snake_head)

 

        if len(snake_list) > length_of_snake:

            del snake_list[0]

 

        for x in snake_list[:-1]:

            if x == snake_head:

                game_close = True

 

        draw_snake(snake_block, snake_list)

        your_score(length_of_snake - 1)

 

        pygame.display.update()

 

        # 파란 먹이 먹음

        if x1 == foodx and y1 == foody:

            length_of_snake += 1

            foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0

            foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0

 

        # 빨간 폭탄 먹음

        if x1 == bombx and y1 == bomby:

            if length_of_snake > 1:

                length_of_snake -= 1

                del snake_list[0]  # 가장 오래된 꼬리 제거

            bombx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0

            bomby = round(random.randrange(0, height - snake_block) / 10.0) * 10.0

 

        clock.tick(snake_speed)

 

    pygame.quit()

    quit()

 

game_loop()