๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/์‚ฌ์ „๊ณผ์ œ

[๋‚ด์ผ๋ฐฐ์›€์บ ํ”„] ์‚ฌ์ „๊ณผ์ œ - ์ถ”์–ต์˜ ์˜ค๋ฝ์‹ค ๊ฒŒ์ž„ ๋งŒ๋“ค๊ธฐ

  • -

github : https://github.com/DHL68/github-DHL68/tree/main/22_04_15_memory%20test%20game

 

GitHub - DHL68/github-DHL68: github-DHL68-practice

github-DHL68-practice. Contribute to DHL68/github-DHL68 development by creating an account on GitHub.

github.com

 

 

๊ธฐ์–ต๋ ฅ ํ…Œ์ŠคํŠธ, ์นจํŒฌ์ง€๋ฅผ ์ด๊ฒจ๋ผ

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, 0, 0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 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 x 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) # ์‹œ์ž‘ ํ™”๋ฉด ๋ณด์—ฌ์ฃผ๊ธฐ def display_start_screen(): pygame.draw.circle(screen, WHITE, start_button.center, 60, 5) # ํฐ์ƒ‰์œผ๋กœ ๋™๊ทธ๋ผ๋ฏธ๋ฅผ ๊ทธ๋ฆฌ๋Š”๋ฐ ์ค‘์‹ฌ์ขŒํ‘œ๋Š” start_button ์˜ ์ค‘์‹ฌ์ขŒํ‘œ๋ฅผ ๋”ฐ๋ผ๊ฐ€๊ณ , # ๋ฐ˜์ง€๋ฆ„์€ 60, ์„  ๋‘๊ป˜๋Š” 5 msg = game_font.render(f"Your level is {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, GRAY, 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 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) # ๊ฒŒ์ž„ ์ข…๋ฃŒ ์ฒ˜๋ฆฌ. ๋ฉ”์„ธ์ง€๋„ ๋ณด์—ฌ์คŒ 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) 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 # ์‹œ๊ฐ„ ๊ณ„์‚ฐ (ํ˜„์žฌ ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ์ €์žฅ) # ๊ฒŒ์ž„ ์‹œ์ž‘ ์—ฌ๋ถ€ 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://youtu.be/Qsk-xsi73YA

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.