์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ/ํŒŒ์ด์ฌ

[ํŒŒ์ด์ฌ ๊ฒŒ์ž„] Chimp Memory Game

HHRR 2023. 5. 10. 05:39
  • chimp memory game
    • ๊ฒฉ์ž : 5 rows(ํ–‰), 9 columns(์—ด)
    • start ๋ฒ„ํŠผ
    • 6 numbers (์ˆœ์ฐจ ์ฆ๊ฐ€)
    • ๊ฒฉ์ž์— ์ˆซ์ž ๋žœ๋ค ์œ„์น˜ํ•˜๊ธฐ
    • Hide Numbers : 1. Display Time, 2. First Number
        1. ์‹œ๊ฐ„์ด ์ง€๋‚œ ํ›„์— ์ˆซ์ž ์ˆจ๊ฒจ์ง
        1. 1์„ ๋ˆ„๋ฅด๋ฉด ๋‚˜๋จธ์ง€ ์ˆซ์ž ์ˆจ๊ฒจ์ง
    • Correct → Next Level
    • Wrong → Game Over
  • ํ”„๋ ˆ์ž„ : 1_frame
    • ๊ฐ€๋กœ 1280, ์„ธ๋กœ 720
import pygame

#์ดˆ๊ธฐํ™”
pygame.init()
screen_width = 1280 #๊ฐ€๋กœํฌ๊ธฐ
screen_height = 720 #์„ธ๋กœํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Memory Game")

#๊ฒŒ์ž„ ๋ฃจํ”„
running = True #๊ฒŒ์ž„์ด ์‹คํ–‰์ค‘์ธ๊ฐ€?ใ…‡
while running :
    #์ด๋ฒคํŠธ ๋ฃจํ”„
    for event in pygame.event.get(): #์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
        if event.type == pygame.QUIT : #์ฐฝ์ด ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ์ธ๊ฐ€?
            running = False #๊ฒŒ์ž„์ด ๋”์ด์ƒ ์‹คํ–‰์ค‘์ด ์•„๋‹˜
            
#๊ฒŒ์ž„ ์ข…๋ฃŒ
pygame.quit()
  • start screen
    • start_button (120*120, ์ค‘์‹ฌ ์ขŒํ‘œ ๋™์ผ, ๋ฐ˜์ง€๋ฆ„ 60, ๋‘๊ป˜ 5)
    • pygame.draw.circle ์ด์šฉํ•˜์—ฌ ์› ๊ทธ๋ฆฌ๊ธฐ
    import pygame
    
    #์‹œ์ž‘ ํ™”๋ฉด ๋ณด์—ฌ์ฃผ๊ธฐ
    def display_start_screen() :
        pygame.draw.circle(screen, WHITE, start_button.center, 60, 5) #ํฐ์ƒ‰ ์›, ์ค‘์‹ฌ์ขŒํ‘œ start_button์˜ ์ค‘์‹ฌ์ขŒํ‘œ, ๋ฐ˜์ง€๋ฆ„ 60, ๋‘๊ผ 5
    
    #์ดˆ๊ธฐํ™”
    pygame.init()
    screen_width = 1280 #๊ฐ€๋กœํฌ๊ธฐ
    screen_height = 720 #์„ธ๋กœํฌ๊ธฐ
    screen = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption("Memory Game")
    
    #์‹œ์ž‘ ๋ฒ„ํŠผ
    start_button = pygame.Rect(0,0,120,120) #๊ฐ€๋กœ ์„ธ๋กœ 120
    start_button.center = (120, screen_height-120 ) #x์ขŒํ‘œ,y์ขŒํ‘œ ๊ธฐ์ค€์œผ๋กœ ์ค‘์‹ฌ๊ฐ’ ์„ค์ •
    
    #์ƒ‰๊น”
    BLACK = (0,0,0) #RGB 
    WHITE = (255,255,255)
    
    #๊ฒŒ์ž„ ๋ฃจํ”„
    running = True #๊ฒŒ์ž„์ด ์‹คํ–‰์ค‘์ธ๊ฐ€?ใ…‡
    while running :
        #์ด๋ฒคํŠธ ๋ฃจํ”„
        for event in pygame.event.get(): #์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
            if event.type == pygame.QUIT : #์ฐฝ์ด ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ์ธ๊ฐ€?
                running = False #๊ฒŒ์ž„์ด ๋”์ด์ƒ ์‹คํ–‰์ค‘์ด ์•„๋‹˜
    
        #ํ™”๋ฉด ์ „์ฒด๋ฅผ ๊นŒ๋งฃ๊ฒŒ ์น ํ•จ
        screen.fill(BLACK)
    
        #์‹œ์ž‘ ํ™”๋ฉด ํ‘œ์‹œ
        display_start_screen()
    
        #ํ™”๋ฉด ์—…๋ฐ์ดํŠธ
        pygame.display.update()
    
                
    #๊ฒŒ์ž„ ์ข…๋ฃŒ
    pygame.quit()
    
  • start button click
import pygame

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

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

#pos์— ํ•ด๋‹นํ•˜๋Š” ๋ฒ„ํŠผ ํ™•์ธ
def check_buttons(pos):
    global start #start ๋ณ€์ˆ˜ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ ์–ธ
    if start_button.collidepoint : #์‚ฌ๊ฐํ˜• ์ •๋ณด ๋‚ด์—์„œ pos ์œ„์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
        start = True #display_game_screen() ์‹คํ–‰

#์ดˆ๊ธฐํ™”
pygame.init()
screen_width = 1280 #๊ฐ€๋กœํฌ๊ธฐ
screen_height = 720 #์„ธ๋กœํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Memory Game")

#์‹œ์ž‘ ๋ฒ„ํŠผ
start_button = pygame.Rect(0,0,120,120) #๊ฐ€๋กœ ์„ธ๋กœ 120
start_button.center = (120, screen_height-120 ) #x์ขŒํ‘œ,y์ขŒํ‘œ ๊ธฐ์ค€์œผ๋กœ ์ค‘์‹ฌ๊ฐ’ ์„ค์ •

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

#๊ฒŒ์ž„ ์‹œ์ž‘ ์—ฌ๋ถ€
start = False

#๊ฒŒ์ž„ ๋ฃจํ”„
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()

            
#๊ฒŒ์ž„ ์ข…๋ฃŒ
pygame.quit()
  • game screen
    • number_count = (level//3)+5 #//:๋ชซ
    • number_count = min(number_count, 20) #์ˆซ์ž ์ตœ๋Œ“๊ฐ’ 20level number_count
    • 5 row (5๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ), 9 column → 2์ฐจ์› ๋ฐฐ์—ด
    • grid = [[0 for col in range(columns)] for row in range(rows)]
    • [[0,0,0,0,0,0,0,0,0],…,[0,0,0,0,0,0,0,0,0]]
import pygame
from random import *

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

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

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

    # [[0, 0, 0, 0, 0, 0, 0, 0, 0],
    # [0, 0, 5, 0, 0, 0, 0, 3, 0],
    # [0, 0, 0, 1, 0, 0, 0, 0, 0],
    # [0, 4, 0, 0, 0, 2, 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 x 9

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

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

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

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

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

#pos์— ํ•ด๋‹นํ•˜๋Š” ๋ฒ„ํŠผ ํ™•์ธ
def check_buttons(pos):
    global start #start ๋ณ€์ˆ˜ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ ์–ธ
    if start_button.collidepoint : #์‚ฌ๊ฐํ˜• ์ •๋ณด ๋‚ด์—์„œ pos ์œ„์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
        start = True #display_game_screen() ์‹คํ–‰

#์ดˆ๊ธฐํ™”
pygame.init()
screen_width = 1280 #๊ฐ€๋กœํฌ๊ธฐ
screen_height = 720 #์„ธ๋กœํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Memory Game")

#์‹œ์ž‘ ๋ฒ„ํŠผ
start_button = pygame.Rect(0,0,120,120) #๊ฐ€๋กœ ์„ธ๋กœ 120
start_button.center = (120, screen_height-120 ) #x์ขŒํ‘œ,y์ขŒํ‘œ ๊ธฐ์ค€์œผ๋กœ ์ค‘์‹ฌ๊ฐ’ ์„ค์ •

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

#๊ฒŒ์ž„ ์‹œ์ž‘ ์—ฌ๋ถ€
start = False

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

#๊ฒŒ์ž„ ๋ฃจํ”„
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()

            
#๊ฒŒ์ž„ ์ข…๋ฃŒ
pygame.quit()
  • ๊ฒฉ์ž
    • cell_size = 130
    • button_size = 110
    • screen_left_margin = 55
    • screen_top_margin = 20
    • ๊ฐ ์…€์˜ ์ค‘๊ฐ„ ์ขŒํ‘œ๊ฐ’ :
      • center_x = screen_left_margin + (col_indexcell_size) + (cell_size/2)
      • center_y = screen_top_margin + (row_idxcell_size) + (cell_size/2)
  • ์ˆซ์ž ์ˆจ๊ธฐ๊ธฐ
    1. ์‹œ๊ฐ„ ์ดˆ๊ณผ
      - display_time = 5 - (level//3)
      - display_time = max(display_time, 1)level display_time
    2. 1 ํด๋ฆญ
      [(x1,y1)…(x6,y6)] click→๋ฒ„ํŠผ ์—†์• ์คŒ
      (x1,y1) ๋ฆฌ์ŠคํŠธ์—์„œ ๋นผ๊ธฐ ์ˆœ์„œ๋Œ€๋กœ ํด๋ฆญํ•ด์•ผํ•จ. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฒŒ์ž„์ข…๋ฃŒ
  • setup
    • list ๊ณต๋ฐฑ → next level
    • ์‹ค์ˆ˜ → ๋‹น์‹œ ๋ ˆ๋ฒจ ๋ณด์—ฌ์ค€ ํ›„ ๊ฒŒ์ž„ ์ข…๋ฃŒ
  • ์ตœ์ข… ์ฝ”๋“œ
import pygame
from random import *

#๋ ˆ๋ฒจ์— ๋งž๊ฒŒ ์„ค์ •
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)

#์ˆซ์ž ์„ž๊ธฐ(์ด ํ”„๋กœ์ ํŠธ์—์„œ ๊ฐ€์žฅ ์ค‘์š”)
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, 0, 0],
    # [0, 0, 5, 0, 0, 0, 0, 3, 0],
    # [0, 0, 0, 1, 0, 0, 0, 0, 0],
    # [0, 4, 0, 0, 0, 2, 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 x 9

    number = 1 #์‹œ์ž‘ ์ˆซ์ž 1๋ถ€ํ„ฐ number_count๊นŒ์ง€, ๋งŒ์•ฝ 5๋ผ๋ฉด 5๊นŒ์ง€ ์ˆซ์ž๋ฅผ ๋žœ๋ค์œผ๋กœ ๋ฐฐ์น˜
    while number <= number_count :
        row_idx = randrange(0, rows) #0~5 ์ค‘์—์„œ ๋žœ๋ค์œผ๋กœ ๋ฝ‘๊ธฐ
        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)
    


#์‹œ์ž‘ ํ™”๋ฉด ๋ณด์—ฌ์ฃผ๊ธฐ
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) : #๋ฆฌ์ŠคํŠธ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ณ  ๊ฐ’์„ rect์— ์ง‘์–ด๋„ฃ๊ณ  ์ธ๋ฑ์Šค idx์— ์ง‘์–ด๋„ฃ์Œ
        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)


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

    if start : #๊ฒŒ์ž„์ด ์‹œ์ž‘ํ–ˆ์œผ๋ฉด?
        check_number_buttons(pos)
    elif start_button.collidepoint(pos) : #์‚ฌ๊ฐํ˜• ์ •๋ณด ๋‚ด์—์„œ pos ์œ„์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
        start = True #display_game_screen() ์‹คํ–‰
        start_ticks = pygame.time.get_ticks() #ํƒ€์ด๋จธ ์‹œ์ž‘(ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ €์žฅ)

def check_number_buttons(pos) :
    global hidden, start, curr_level #hidden์€ ์ „์—ญ๊ณต๊ฐ„, hidden์— ๊ฐ’ ๋„ฃ๊ธฐ ์œ„ํ•ด global -> ์ „์—ญ๊ณต๊ฐ„์— ์žˆ๋Š” ๋ณ€์ˆ˜ ์“ธ๊ฑฐ๋ผ๋Š” ๊ฒƒ ๋ช…์‹œ

    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()
                print("Wrong")
            break

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

#๊ฒŒ์ž„ ์ข…๋ฃŒ ์ฒ˜๋ฆฌ. ๋ฉ”์‹œ์ง€๋„ ๋ณด์—ฌ์คŒ
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)

#์ดˆ๊ธฐํ™”
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) #๊ฐ€๋กœ ์„ธ๋กœ 120
start_button.center = (120, screen_height-120 ) #x์ขŒํ‘œ,y์ขŒํ‘œ ๊ธฐ์ค€์œผ๋กœ ์ค‘์‹ฌ๊ฐ’ ์„ค์ •

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

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

#๊ฒŒ์ž„ ์‹œ์ž‘ ์—ฌ๋ถ€
start = False

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

#๊ฒŒ์ž„ ์‹œ์ž‘ ์ „์— ๊ฒŒ์ž„ ์„ค์ • ํ•จ์ˆ˜ ์ˆ˜ํ–‰
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()

 

 

์ถœ์ฒ˜ : https://www.youtube.com/watch?v=Qsk-xsi73YA&list=PLMsa_0kAjjrdqJ1rJba9MFWYv-GHluK4_&index=6