#100日チャレンジ　本読んで感化されたので，Make10作ってみた

#### 大塚あみさんの『＃100日チャレンジ　毎日連続100本アプリ作ったら人生が変わった』を読んだら，面白すぎたので興味あったゲームを作ってみた

車に乗っている時によく遊んだことのある，Make10というゲームを作ってみた</br>
.pyに変換する方法，誰か知ってたら教えて！
![Make10.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/668168/ac96732c-0bc9-4207-8f9a-641691dc159d.gif)


```java:jupyter notobook
import pygame
import random
import sys
import re
```

```java:jupyter notobook
screen = pygame.display.set_mode((600, 500))  # ウィンドウサイズを少し大きく
pygame.display.set_caption("Make 10 Game")
font = pygame.font.SysFont(None, 48)
small_font = pygame.font.SysFont(None, 36)
clock = pygame.time.Clock()

# ランダムに1〜9の数字を4つ生成
def generate_numbers():
    return [random.randint(1, 9) for _ in range(4)]

# 数式が正しいかチェック
def check_expression(expr, numbers):
    try:
        used = [int(s) for s in expr if s.isdigit()]
        if sorted(used) != sorted(numbers):
            return False
        return eval(expr) == 10
    except:
        return False

# 初期状態
numbers = generate_numbers()
input_text = ''
result_msg = ''

last_input_text = ""
last_result_msg = ""
last_numbers = []

# 初期描画テキストの準備
input_surface = font.render("Input: " + input_text, True, (0, 0, 255))
result_surface = small_font.render("", True, (0, 128, 0))
nums_surface = font.render("Numbers: " + ', '.join(map(str, numbers)), True, (0, 0, 0))

# 入力ボタンの定義（数字と記号）
button_chars = ['1','2','3','4','5','6','7','8','9','0','+','-','*','/','(',')']
char_buttons = []
for i, char in enumerate(button_chars):
    x = 50 + (i % 4) * 60
    y = 300 + (i // 4) * 50
    rect = pygame.Rect(x, y, 50, 40)
    char_buttons.append((char, rect))

# ボタン：CheckとClear
check_button_rect = pygame.Rect(400, 120, 120, 50)
clear_button_rect = pygame.Rect(400, 180, 120, 50)

# メインループ
while True:
    screen.fill((255, 255, 255))

    # テキストの再レンダリング
    if input_text != last_input_text:
        input_surface = font.render("Input: " + input_text, True, (0, 0, 255))
        last_input_text = input_text

    if result_msg != last_result_msg:
        result_surface = small_font.render(
            result_msg,
            True,
            (0, 128, 0) if result_msg == "Correct!" else (255, 0, 0)
        )
        last_result_msg = result_msg

    if numbers != last_numbers:
        nums_surface = font.render("Numbers: " + ', '.join(map(str, numbers)), True, (0, 0, 0))
        last_numbers = numbers[:]

    # 入力・結果・数字の描画
    screen.blit(nums_surface, (50, 50))
    screen.blit(input_surface, (50, 120))
    screen.blit(result_surface, (50, 180))

    # 数字＆記号ボタン描画
    for char, rect in char_buttons:
        pygame.draw.rect(screen, (220, 220, 220), rect)
        text = small_font.render(char, True, (0, 0, 0))
        screen.blit(text, (rect.x + 15, rect.y + 10))

    # Checkボタン
    mouse_pos = pygame.mouse.get_pos()
    pygame.draw.rect(screen, (180, 180, 255) if check_button_rect.collidepoint(mouse_pos) else (200, 200, 200), check_button_rect)
    check_text = small_font.render("Check", True, (0, 0, 0))
    screen.blit(check_text, (check_button_rect.x + 20, check_button_rect.y + 10))

    # Clearボタン
    pygame.draw.rect(screen, (255, 200, 200) if clear_button_rect.collidepoint(mouse_pos) else (230, 230, 230), clear_button_rect)
    clear_text = small_font.render("Clear", True, (0, 0, 0))
    screen.blit(clear_text, (clear_button_rect.x + 25, clear_button_rect.y + 10))

    # イベント処理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            if check_button_rect.collidepoint(event.pos):
                if check_expression(input_text, numbers):
                    result_msg = "Correct!"
                else:
                    result_msg = "Wrong!"
                numbers = generate_numbers()
                input_text = ''

            elif clear_button_rect.collidepoint(event.pos):
                input_text = ''
                result_msg = ''

            else:
                for char, rect in char_buttons:
                    if rect.collidepoint(event.pos):
                        input_text += char

    pygame.display.flip()
    clock.tick(30)
```

気になった方はいいね！をしてね:thumbsup: