πŸ“ 과제 제좜 μ•ˆλ‚΄

πŸ’‘ κ²Œμž„ μ„€λͺ…μ„œ

πŸ–₯️ 파이썬 ν”„λ‘œκ·Έλž¨ μ½”λ“œ

import pygame
import time
import random

# 색상 μ„€μ •
white = (255, 255, 255)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

# ν™”λ©΄ 크기
dis_width = 600
dis_height = 400

# λ±€ 블둝 크기
block_size = 10

# κ²Œμž„ 속도 μ„€μ •
speed = 15

# μ΄ˆκΈ°ν™”
pygame.init()

# λ””μŠ€ν”Œλ ˆμ΄ μ„€μ •
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('μŠ€λ„€μ΄ν¬ κ²Œμž„')

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

def snake(block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(dis, black, [x[0], x[1], block_size, block_size])

def message(msg, color, x, y):
    text = font_style.render(msg, True, color)
    dis.blit(text, [x, y])

def game_loop():
    game_over = False
    game_close = False

    x = dis_width / 2
    y = dis_height / 2

    x_change = 0
    y_change = 0

    snake_list = []
    length_of_snake = 1

    food_x = round(random.randrange(0, dis_width - block_size) / 10.0) * 10.0
    food_y = round(random.randrange(0, dis_height - block_size) / 10.0) * 10.0

    while not game_over:
        while game_close:
            dis.fill(white)
            message("gameover! restart Q, finish C", red, dis_width / 6, dis_height / 3)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_loop()
                    if event.key == pygame.K_c:
                        game_over = True
                        game_close = False

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -block_size
                    y_change = 0
                elif event.key == pygame.K_RIGHT:
                    x_change = block_size
                    y_change = 0
                elif event.key == pygame.K_UP:
                    y_change = -block_size
                    x_change = 0
                elif event.key == pygame.K_DOWN:
                    y_change = block_size
                    x_change = 0

        if x >= dis_width or x < 0 or y >= dis_height or y < 0:
            game_close = True

        x += x_change
        y += y_change
        dis.fill(blue)
        pygame.draw.rect(dis, green, [food_x, food_y, block_size, block_size])

        snake_head = []
        snake_head.append(x)
        snake_head.append(y)
        snake_list.append(snake_head)

        if len(snake_list) > length_of_snake:
            del snake_list[0]

        for segment in snake_list[:-1]:
            if segment == snake_head:
                game_close = True

        snake(block_size, snake_list)
        pygame.display.update()

        if x == food_x and y == food_y:
            food_x = round(random.randrange(0, dis_width - block_size) / 10.0) * 10.0
            food_y = round(random.randrange(0, dis_height - block_size) / 10.0) * 10.0
            length_of_snake += 1

        clock.tick(speed)

    pygame.quit()
    quit()

game_loop()

πŸ’Ύ κ²Œμž„ μ‹€ν–‰ μ˜μƒ 파일 및 μ•Œμ§‘ 파일