[python]記憶力テストゲーム


記憶力テストゲーム、チンパンジーに勝つ

🦧 コードメータ


明日の学習キャンプ第2期の事前課題を達成するために符号化を行った.昨日行われたGUI計算機をエンコードするのも面白いし、ゲームを作ったので、もっと斬新な感じがします.コードテストを学ぶ過程で困難に直面すると、このようなプロジェクトを探して後悔します.私は何度も復習して、それを私のものにします.

🦧 コンセプト


🔥 pygame

  • ビデオゲーム作成用プラットフォーム間Pythonモジュールキット
  • 🦧 コード#コード#


    🔥ゲームフレーム

    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()
    
  • 基本ゲームクリエイティブ情報の設定

  • 🔥 ゲーム開始画面

    # 시작화면 보여주기
    def display_start_screen():
        # 화면에 버튼을 그리는 데 하얀색이고 두께가 5인 반지름 60짜리의 원으로 그림.
        # 중심점은 이전에 설정한 시작버튼의 중심점 좌표를 사용함.
        pygame.draw.circle(screen, WHITE, start_button.center, 60, 5)
    
    # 시작 버튼
    start_button = pygame.Rect(0, 0, 120, 120) # 가로 세로가 120인 사각형 버튼을 생성
    # 해당 버튼의 중심점 좌표를 (120, 120)으로 설정.
    # x좌표는 0에서 120으로 해주면되고 y좌표는 전체 높이에서 120 뺀 값.
    start_button.center = (120, screen_height - 120)
    
    # 색깔
    BLACK = (0, 0, 0) # RGB
    WHITE = (255, 255, 255) #RGB
    
    # 게임 루프
    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()
  • 黒いデスクトップにフィレットボタンを作成するゲーム画面

  • 🔥 ゲーム開始

    # 게임 화면 보여주기
    def display_game_screen():
        print('game start!')
    
    # 포지션에 대응하는 버튼 확인
    def check_buttons(position):
        global start
        if start_button.collidepoint(position):
            start = True
            
    # 게임 시작 여부
    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()
  • ゲームの開始有無でゲームの開始画面を表示するために設定
  • 入力した場合
  • スタートボタン以外の座標
  • click_pos = pygame.mouse.get_pos()
                print(click_pos)
    したがって,端末には座標値が印刷されるが,スタートボタン内の座標が押されるとゲームが開始し,display game screen関数に置かれたprint値はwhile文により無限に印刷される.
  • マウスはclick pos変数を宣言し、アクティブサイクル内でpygameする.MOUSEBUTONU Pをアクティブタイプに設定し、ユーザーのマウスクリックを検出できます.
  • 🔥ゲーム画面の基本

  • ゲームのレベル設定によって表示される数字が決まる
  • grid形式で数字を表示
    現在はリスト形式のように見えますが、乱数の位置を特定できます
  • 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, 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)]
    
        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
    
        # 배치된 랜덤 숫자 확인
         print(grid)
    
    # 게임 시작 직전에 게임 설정 함수 수행
    setup(1)
    ### 🔥ゲーム画面の深化
    def shuffle_grid(number_count):
    	...
        ...
                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_game_screen():
        for idx, rect in enumerate(number_buttons, start=1):
            pygame.draw.rect(screen, GRAY, rect) # rect에는 center_x와 center_y의 정보가 있음. button_size의 정보도 있음.
    
            # 실제 숫자 텍스트
            cell_text = game_font.render(str(idx), True, WHITE)
            # 앞에서 가져온 버튼 rect의 center 값을 우리가 그릴 숫자 텍스트의 center 값으로 활용
            text_rect = cell_text.get_rect(center=rect.center)
            screen.blit(cell_text, text_rect)
            
    game_font = pygame.font.Font(None, 120) # 폰트 정의
    GRAY = (50, 50, 50)
    
    # 실제 플레이어가 눌러야 하는 버튼
    number_buttons = []
  • ゲーム画面でグレーの背景をランダムに選択し、各グリッドに白い数字を描画する.

  • 🔥数字1を隠す

    # 게임 화면 보여주기
    def display_game_screen():
        for idx, rect in enumerate(number_buttons, start=1):
            if hidden: # 숨김처리
                # 버튼 사각형 그리기
                pygame.draw.rect(screen, WHITE, rect) # rect에는 center_x와 center_y의 정보가 있음. button_size의 정보도 있음.
            else:
                # 실제 숫자 텍스트
                cell_text = game_font.render(str(idx), True, WHITE)
                # 앞에서 가져온 버튼 rect의 center 값을 우리가 그릴 숫자 텍스트의 center 값으로 활용
                text_rect = cell_text.get_rect(center=rect.center)
                screen.blit(cell_text, text_rect)
    
    # 포지션에 대응하는 버튼 확인
    def check_buttons(pos):
        global start
        if start: # 게임이 시작했으면?
            check_number_buttons(pos)
        elif start_button.collidepoint(pos):
            start = True
    
    def check_number_buttons(pos):
        global hidden
        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: # 잘못된 숫자 클릭
                    print("Wrong")
                break
        
    # 숫자 숨김 여부(사용자가 1을 클릭했거나, 보여주는 시간을 초과했을 때)
    hidden = False
    
    前の画面と違って、初めてゲームを始めると数字の背景色が消えてしまいます.
  • hidden変数、number_buttonsリストの正しい最初の数字、つまり1を押すと1が消えるにつれて残りの数字が白くなり、下の数字が隠れてしまいます.
  • check_number_buttons関数のprintを入れることで、正しい順番で数字を出力し、間違った数字でWrongを出力することができ、間違った数字でゲームを終了することはできません.
  • 🔥 数字2を隠す

    # 숫자를 보여주는 시간
    display_time = None
    # 시간 계산(현재 시간 정보를 저장)
    start_ticks = None
    
    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 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) # rect에는 center_x와 center_y의 정보가 있음. button_size의 정보도 있음.
            else:
                # 실제 숫자 텍스트
                cell_text = game_font.render(str(idx), True, WHITE)
                # 앞에서 가져온 버튼 rect의 center 값을 우리가 그릴 숫자 텍스트의 center 값으로 활용
                text_rect = cell_text.get_rect(center=rect.center)
                screen.blit(cell_text, text_rect)
    
    # 포지션에 대응하는 버튼 확인
    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() # 타이머 시작(현재 시간을 저장)
  • さっきと違って、今は数字を押さなくても、しばらくすると数字は白い箱に処理されます.
  • 🔥レベル設定&最終コード

    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 # 전체 스크린의 위쪽 여백
    
        grid = [[0 for col in range(columns)] for row in range(rows)]
    
        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():
        # 화면에 버튼을 그리는 데 하얀색이고 두께가 5인 반지름 60짜리의 원으로 그림.
        # 중심점은 이전에 설정한 시작버튼의 중심점 좌표를 사용함.
        pygame.draw.circle(screen, WHITE, start_button.center, 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) # rect에는 center_x와 center_y의 정보가 있음. button_size의 정보도 있음.
            else:
                # 실제 숫자 텍스트
                cell_text = game_font.render(str(idx), True, WHITE)
                # 앞에서 가져온 버튼 rect의 center 값을 우리가 그릴 숫자 텍스트의 center 값으로 활용
                text_rect = cell_text.get_rect(center=rect.center)
                screen.blit(cell_text, text_rect)
    
    # 포지션에 대응하는 버튼 확인
    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) # 가로 세로가 120인 사각형 버튼을 생성
    # 해당 버튼의 중심점 좌표를 (120, 120)으로 설정.
    # x좌표는 0에서 120으로 해주면되고 y좌표는 전체 높이에서 120 뺀 값.
    start_button.center = (120, screen_height - 120)
    
    # 색깔
    BLACK = (0, 0, 0) # RGB
    WHITE = (255, 255, 255) #RGB
    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()
    
  • 前のコードでは正解してもゲームは終了しない.この段階では,正解であれば次の段階を実行し,正しくなければゲーム終了後にゲームのメインサイクルから離れることができる.また、開始時には、スタートボタンで現在のゲームステージが表示され、game_over関数でゲーム終了後にどのステージで終了するかが表示されます.
  • ウェルカムスクリーン
  • ゲーム移行画面