๐Ÿ“š ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ 0์ฃผ์ฐจ ๊ณผ์ œ #๊ธฐ์–ต๋ ฅ ๊ฒŒ์ž„ ๋งŒ๋“ค๊ธฐ

๋ฌธ๋ช…์ฃผยท2022๋…„ 4์›” 15์ผ
0

ํŒŒ์ด์ฌ์—์„œ ์‰ฝ๊ฒŒ ๊ฒŒ์ž„์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ pygame์„ ์ด์šฉํ•ด ์˜ค๋ฝ์‹ค ํŒก๊ฒŒ์ž„๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธฐ์–ต๋ ฅ๊ฒŒ์ž„์„ ๋งŒ๋“ค์—ˆ๋‹ค.
๊ฐ€์žฅ ๋จผ์ € ์•„๋ž˜์™€ ๊ฐ™์ด pygame์„ ์ž„ํฌํŠธํ•ด์ค€๋‹ค. ์ˆซ์ž์˜ ์œ„์น˜๋ฅผ ๋žœ๋ค์œผ๋กœ ๋‚˜ํƒ€๋‚˜๊ฒŒํ•˜๊ธฐ ์œ„ํ•ด random๋„ ํ•จ๊ป˜ ์ž„ํฌํŠธํ•ด์ค€๋‹ค.

import pygame
from random import *

๊ทธ ๋‹ค์Œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ฃผ๊ณ , ํ™”๋ฉดํฌ๊ธฐ์„ค์ • ๋ฐ ํ™”๋ฉดํƒ€์ดํ‹€ ์„ค์ • ๋“ฑ ๊ธฐ๋ณธ ํ‹€์„ ๋งŒ๋“ ๋‹ค.

# ์ดˆ๊ธฐํ™”
pygame.init()
screen_width = 1280 # ๊ฐ€๋กœํฌ๊ธฐ
screen_height = 720 # ์„ธ๋กœํฌ๊ธฐ 
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Memory Game')
game_font = pygame.font.Font(None, 120) # ํฐํŠธ ์ •์˜

๋‹ค์Œ์—๋Š” ๊ฒŒ์ž„์—์„œ ์ด์šฉํ•  ์ƒ‰์ƒ๋“ค๊ณผ, ์‹œ์ž‘๋ฒ„ํŠผ ๋ฐ ํ˜„์žฌ๋‹จ๊ณ„ ๋ ˆ๋ฒจ์ด๋‚˜ ์‹œ๊ฐ„๊ณ„์‚ฐํ•˜๋Š” ๊ฐ’์„ ๋‘๊ณ 

# ์‹œ์ž‘ ๋ฒ„ํŠผ
start_button = pygame.Rect(0, 0, 120, 120)
start_button.center = (120, screen_height - 120)

# ์ƒ‰๊น”
BLACK = (0, 0, 0) #RGB
WHITE = (255, 255, 255)
GRAY = (50, 50, 50)

number_buttons = [] # ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๋ˆŒ๋Ÿฌ์•ผ ํ•˜๋Š” ๋ฒ„ํŠผ๋“ค
curr_level = 1 # ํ˜„์žฌ ๋ ˆ๋ฒจ
display_time = None # ์ˆซ์ž๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์‹œ๊ฐ„
start_ticks = None # ์‹œ๊ฐ„ ๊ณ„์‚ฐ (ํ˜„์žฌ ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ์ €์žฅ)

์‹œ์ž‘ ํ™”๋ฉด๊ณผ ๊ฒŒ์ž„ ํ™”๋ฉด์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์„ค์ •ํ•œ๋‹ค.

# ์‹œ์ž‘ ํ™”๋ฉด ๋ณด์—ฌ์ฃผ๊ธฐ
def display_start_screen():
    pygame.draw.circle(screen, WHITE, start_button.center, 60, 5)
    # ํฐ์ƒ‰์œผ๋กœ ๋™๊ทธ๋ผ๋ฏธ๋ฅผ ๊ทธ๋ฆฌ๋Š”๋ฐ ์ค‘์‹ฌ์ขŒํ‘œ๋Š” start_button ์˜ ์ค‘์‹ฌ์ขŒํ‘œ๋ฅผ ๋”ฐ๋ผ๊ฐ€๊ณ ,
    # ๋ฐ˜์ง€๋ฆ„์€ 60, ์„  ๋‘๊ป˜๋Š” 5

    msg = game_font.render(f'{curr_level}', True, WHITE)
    msg_rect = msg.get_rect(center=start_button.center)
    screen.blit(msg, msg_rect)

# ๊ฒŒ์ž„ ํ™”๋ฉด ๋ณด์—ฌ์ฃผ๊ธฐ
def display_game_screen():
    global hidden

    if not hidden:
        elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000 # ms -> sec
        if elapsed_time > display_time:
            hidden =True

    for idx, rect in enumerate(number_buttons, start=1):
        if hidden: # ์ˆจ๊น€ ์ฒ˜๋ฆฌ
            # ๋ฒ„ํŠผ ์‚ฌ๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ
            pygame.draw.rect(screen, WHITE, rect)
        else:
            # ์‹ค์ œ ์ˆซ์ž ํ…์ŠคํŠธ
            cell_text = game_font.render(str(idx), True, WHITE)
            text_rect = cell_text.get_rect(center=rect.center)
            screen.blit(cell_text, text_rect)

๊ทธ๋ฆฌ๊ณ  ๊ฒŒ์ž„์‹œ์ž‘์—ฌ๋ถ€์™€ ์ˆซ์ž์ˆจ๊น€์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•˜๊ณ ,

# ๊ฒŒ์ž„ ์‹œ์ž‘ ์—ฌ๋ถ€
start = False
# ์ˆซ์ž ์ˆจ๊น€ ์—ฌ๋ถ€ (์‚ฌ์šฉ์ž๊ฐ€ 1์„ ํด๋ฆญํ–ˆ๊ฑฐ๋‚˜, ๋ณด์—ฌ์ฃผ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผํ–ˆ์„ ๋•Œ)
hidden = False

์—ฌ๊ธฐ์„œ ์ œ์ผ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ ์ˆซ์ž๋ฅผ ์„ž๋Š” (๋žœ๋ค)์œผ๋กœ ๋ฐฐ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

# ์ˆซ์ž ์„ž๊ธฐ (์ด ํ”„๋กœ์ ํŠธ์—์„œ ๊ฐ€์žฅ ์ค‘์š”)
def shuffle_grid(number_count):
    rows = 5
    columns = 9

    cell_size = 130 # ๊ฐ Grid cell ๋ณ„ ๊ฐ€๋กœ, ์„ธ๋กœ ํฌ๊ธฐ
    button_size = 110 # Grid cell ๋‚ด์— ์‹ค์ œ๋กœ ๊ทธ๋ ค์งˆ ๋ฒ„ํŠผ ํฌ๊ธฐ
    screen_left_margin = 55 # ์ „์ฒด ์Šคํฌ๋ฆฐ ์™ผ์ชฝ ์—ฌ๋ฐฑ
    screen_top_margin = 20 # ์ „์ฒด ์Šคํฌ๋ฆฐ ์œ„์ชฝ ์—ฌ๋ฐฑ

    # [[0,0,0,0,0,0,0,5,0],
    #  [0,0,0,0,0,4,0,0,0],
    #  [0,0,1,0,0,0,2,0,0],
    #  [0,0,0,0,3,0,0,0,0],
    #  [0,0,0,0,0,0,0,0,0]]]
    grid = [[0 for col in range(columns)] for row in range(rows)] # 5 * 9

    number = 1 # ์‹œ์ž‘ ์ˆซ์ž 1๋ถ€ํ„ฐ number_count๊นŒ์ง€, ๋งŒ์•ฝ 5๋ผ๋ฉด 5๊นŒ์ง€ ์ˆซ์ž๋ฅผ ๋žœ๋ค์œผ๋กœ ๋ฐฐ์น˜
    while number <= number_count:
        row_idx = randrange(0, rows) # 0, 1, 2, 3, 4 ์ค‘์—์„œ ๋žœ๋ค์œผ๋กœ ๋ฝ‘๊ธฐ
        col_idx = randrange(0, columns) # 0 ~ 8 ์ค‘์—์„œ ๋žœ๋ค์œผ๋กœ ๋ฝ‘๊ธฐ

        if grid[row_idx][col_idx] == 0:
            grid[row_idx][col_idx] = number # ์ˆซ์ž ์ง€์ •
            number += 1

            # ํ˜„์žฌ grid cell ์œ„์น˜ ๊ธฐ์ค€์œผ๋กœ x,y ์œ„์น˜๋ฅผ ๊ตฌํ•จ
            center_x = screen_left_margin + (col_idx * cell_size) + (cell_size / 2)
            center_y = screen_top_margin + (row_idx * cell_size) + (cell_size / 2)

            # ์ˆซ์ž ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ
            button = pygame.Rect(0, 0, button_size, button_size)
            button.center = (center_x, center_y)

            number_buttons.append(button)

    # ๋ฐฐ์น˜๋œ ๋žœ๋ค ์ˆซ์ž ํ™•์ธ
    print(grid)

๋˜ํ•œ ์—ฌ๊ธฐ์„œ ๋ ˆ๋ฒจ์— ๋งž๊ฒŒ ์„ค์ •ํ•˜์—ฌ ์–ผ๋งˆ๋™์•ˆ ์ˆซ์ž๋ฅผ ๋ณด์—ฌ์ค„์ง€, ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ˆซ์ž๋ฅผ ๋ณด์—ฌ์ค„์ง€ max,min ๊ฐ’ ๋“ฑ์„ ์„ค์ •ํ•œ๋‹ค.

# ๋ ˆ๋ฒจ์— ๋งž๊ฒŒ ์„ค์ •
def setup(level):
    # ์–ผ๋งˆ๋™์•ˆ ์ˆซ์ž๋ฅผ ๋ณด์—ฌ์ค„์ง€
    global display_time
    display_time = 5 - (level // 3)
    display_time = max(display_time, 1) # 1์ดˆ ๋ฏธ๋งŒ์ด๋ฉด 1์ดˆ๋กœ ์ฒ˜๋ฆฌ
    
    # ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ˆซ์ž๋ฅผ ๋ณด์—ฌ์ค„ ๊ฒƒ์ธ๊ฐ€?
    number_count = (level // 3) + 5
    number_count = min(number_count, 20) # ๋งŒ์•ฝ 20์„ ์ดˆ๊ณผํ•˜๋ฉด 20์œผ๋กœ ์ฒ˜๋ฆฌ

    # ์‹ค์ œ ํ™”๋ฉด์— gridํ˜•ํƒœ๋กœ ์ˆซ์ž๋ฅผ ๋žœ๋ค์œผ๋กœ ๋ฐฐ์น˜
    shuffle_grid(number_count)

ํƒ€์ด๋จธ์™€ ์˜ฌ๋ฐ”๋ฅธ ์ˆซ์ž๊ฐ’์„ ๋ˆŒ๋ €์„๋•Œ 0๋ฒˆ์งธ์ˆซ์ž๋ฅผ ์‚ญ์ œํ•ด์ฃผ๊ณ , ์ž˜๋ชป๋œ ์ˆซ์ž๋ฅผ ๋ˆŒ๋ €์„๋•Œ ๊ฒŒ์ž„์˜ค๋ฒ„ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค.

# pos์— ํ•ด๋‹นํ•˜๋Š” ๋ฒ„ํŠผ ํ™•์ธ
def check_buttons(pos): 
    global start, start_ticks

    if start: # ๊ฒŒ์ž„์ด ์‹œ์ž‘ํ–ˆ์œผ๋ฉด?
        check_number_buttons(pos)
    elif start_button.collidepoint(pos):
        start = True
        start_ticks = pygame.time.get_ticks() # ํƒ€์ด๋จธ ์‹œ์ž‘ (ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ €์žฅ)

def check_number_buttons(pos):
    global start, hidden, curr_level

    for button in number_buttons:
        if button.collidepoint(pos):
            if button == number_buttons[0]: # ์˜ฌ๋ฐ”๋ฅธ ์ˆซ์ž ํด๋ฆญ
                print('Correct')
                del number_buttons[0]
                if not hidden:
                    hidden = True # ์ˆซ์ž ์ˆจ๊น€ ์ฒ˜๋ฆฌ
            else: # ์ž˜๋ชป๋œ ์ˆซ์ž ํด๋ฆญ
                game_over()
            break

    # ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋‹ค ๋งžํ˜”๋‹ค๋ฉด? ๋ ˆ๋ฒจ์„ ๋†’์—ฌ์„œ ๋‹ค์‹œ ์‹œ์ž‘ ํ™”๋ฉด์œผ๋กœ ๊ฐ
    if len(number_buttons) ==0:
        start = False
        hidden = False
        curr_level += 1
        setup(curr_level)

๊ฒŒ์ž„๋ฃจํ”„๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

# ๊ฒŒ์ž„ ์‹œ์ž‘ ์ „์— ๊ฒŒ์ž„ ์„ค์ • ํ•จ์ˆ˜ ์ˆ˜ํ–‰
setup(curr_level)

# ๊ฒŒ์ž„ ๋ฃจํ”„
running = True # ๊ฒŒ์ž„์ด ์‹คํ–‰์ค‘์ธ๊ฐ€?
while running:
    click_pos = None

    # ์ด๋ฒคํŠธ ๋ฃจํ”„
    for event in pygame.event.get(): # ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
        if event.type == pygame.QUIT: # ์ฐฝ์ด ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ์ธ๊ฐ€?
            running = False # ๊ฒŒ์ž„์ด ๋” ์ด์ƒ ์‹คํ–‰์ค‘์ด ์•„๋‹˜
        elif event.type == pygame.MOUSEBUTTONUP: # ์‚ฌ์šฉ์ž๊ฐ€ ๋งˆ์šฐ์Šค๋ฅผ ํด๋ฆญํ–ˆ์„๋•Œ 
            click_pos = pygame.mouse.get_pos()
            print(click_pos)

    # ํ™”๋ฉด ์ „์ฒด๋ฅผ ๊นŒ๋งฃ๊ฒŒ ์น ํ•จ
    screen.fill(BLACK)

    if start: 
        display_game_screen() # ๊ฒŒ์ž„ ํ™”๋ฉด ํ‘œ์‹œ
    else:
        display_start_screen() # ์‹œ์ž‘ ํ™”๋ฉด ํ‘œ์‹œ

    # ์‚ฌ์šฉ์ž๊ฐ€ ํด๋ฆญํ•œ ์ขŒํ‘œ๊ฐ’์ด ์žˆ๋‹ค๋ฉด (์–ด๋”˜๊ฐ€ ํด๋ฆญํ–ˆ๋‹ค๋ฉด)
    if click_pos: 
        check_buttons(click_pos)


    # ํ™”๋ฉด ์—…๋ฐ์ดํŠธ
    pygame.display.update()

# 5์ดˆ์ •๋„ ๋ณด์—ฌ์คŒ
pygame.time.delay(5000)

# ๊ฒŒ์ž„ ์ข…๋ฃŒ
pygame.quit()

๊ฒŒ์ž„ ์ข…๋ฃŒ์ฒ˜๋ฆฌ์‹œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ”์‹œ์ง€๋„ ํ•จ๊ป˜ ๋ณด์—ฌ์ค„์ˆ˜์žˆ๋„๋ก ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.

# ๊ฒŒ์ž„ ์ข…๋ฃŒ ์ฒ˜๋ฆฌ. ๋ฉ”์‹œ์ง€๋„ ๋ณด์—ฌ์คŒ
def game_over():
    global running
    running = False

    msg = game_font.render(f'Your level is {curr_level}', True, WHITE)
    msg_rect = msg.get_rect(center=(screen_width/2, screen_height/2))

    screen.fill(BLACK)
    screen.blit(msg, msg_rect)
  • ๋Š๋‚€ ์ 

    ์˜ค๋ฝ์‹ค ํŒก๊ฒŒ์ž„์„ ๋งŒ๋“ค๋ฉด์„œ ํ•œ ๋ฒˆ ๊ณต๋ถ€ํ–ˆ๋˜๋ถ€๋ถ„์ด ์žˆ์–ด์„œ์˜€๋˜๊ฑด์ง€ ์ด๋ฒˆ ๊ณผ์ œ๋Š” ํŒก๊ฒŒ์ž„๋ณด๋‹ค๋Š” ์ข€ ๋” ์ˆ˜์›”ํ•œ ๋Š๋‚Œ์ด์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ ํ—ท๊ฐˆ๋ ค์„œ ํ˜ผ๋‚ฌ๋‹ค.. ๊ฐ„๋‹จํ•œ ๊ฒŒ์ž„ ํ•˜๋‚˜ ๋งŒ๋“œ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๋งŽ์€ ์‹œ๊ฐ„๊ณผ ๋…ธ๋ ฅ์ด ๋“ ๋‹ค๋Š”๊ฒŒ ๊ต‰์žฅํžˆ ์‹ ๊ธฐํ–ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๋ฌด์‚ฌํžˆ ๊ณผ์ œ๋ฅผ ๋งˆ์ณ์„œ ๋‹คํ–‰์ด๋‹น๐Ÿ˜‚๐Ÿ‘

profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด