macOS   ChatGPT   ゲーム

ホーム

恐る恐る ChatGPT を使ってプログラミングを始めてみます。

私の環境は、MacBook Air 2012 メモリ 4GB に OpenCore Legacy Patcher を使って未サポートのmacOS Big Sur 11.7.6をインストールしています。

もしかすると今後この環境がプログラミング上で不具合を起こすかもしれません。

ゲーム

今回は、お題を決めずにゲームを作ってみました。3ステップでできました。

質問

Tkinterでゲームを作ってください。

答え

はい、Tkinterを使って簡単なゲームを作ってみましょう。今回は簡単なクイズゲームを作成します。以下のように進めていきます。

まずは、必要なライブラリをインポートします。


import tkinter as tk
from tkinter import messagebox
	

次に、問題や答えなどのデータを定義します。


questions = [
    {
        "question": "太陽系の第四惑星は?",
        "answers": ["火星", "木星", "水星", "金星"],
        "correct_answer": "火星"
    },
    {
        "question": "日本の首都は?",
        "answers": ["東京", "京都", "大阪", "名古屋"],
        "correct_answer": "東京"
    },
    {
        "question": "プログラミング言語Pythonの作者は?",
        "answers": ["ジェームズ・ゴスリング", "ブレンダン・アイク", "ガイ・スティール", "グイド・ヴァン・ロッサム"],
        "correct_answer": "グイド・ヴァン・ロッサム"
    }
]
    

次に、ウィンドウを作成します。


window = tk.Tk()
window.title("クイズゲーム")
    

次に、ウィンドウに表示する要素を作成します。ラベルとボタンを作成します。


question_label = tk.Label(window, text="")
question_label.pack(pady=10)

answer_button1 = tk.Button(window, text="", command=lambda: check_answer(answer_button1["text"]))
answer_button1.pack(pady=5)

answer_button2 = tk.Button(window, text="", command=lambda: check_answer(answer_button2["text"]))
answer_button2.pack(pady=5)

answer_button3 = tk.Button(window, text="", command=lambda: check_answer(answer_button3["text"]))
answer_button3.pack(pady=5)

answer_button4 = tk.Button(window, text="", command=lambda: check_answer(answer_button4["text"]))
answer_button4.pack(pady=5)
    

次に、問題を表示する関数を作成します。


def show_question():
    global current_question_index

    if current_question_index >= len(questions):
        messagebox.showinfo("結果", f"あなたのスコアは{score}です。")
        window.destroy()
        return

    question = questions[current_question_index]["question"]
    answers = questions[current_question_index]["answers"]
    correct_answer = questions[current_question_index]["correct_answer"]

    question_label["text"] = question
    answer_button1["text"] = answers[0]
    answer_button2["text"] = answers[1]
    answer_button3["text"] = answers[2]
    answer_button4["text"] = answers[3]

    current_question_index += 1
    

最後に、答えをチェックする関数を作成します。


def check_answer(answer):
    global score

    if answer == questions[current_question_index - 1]["correct_answer"]:
        score += 1
        messagebox.showinfo("正
    

以上を一つのファイルにまとめて実行しようとするとエラーが出たので、そのエラーメッセージをそのまま質問します。

質問


File "/Users/username/Desktop/game.py", line 65
    messagebox.showinfo("正
                        ^
SyntaxError: unterminated string literal (detected at line 65)
    

答え

申し訳ありませんが、私の前の回答に誤りがあります。一部のテキストが欠落しており、文法エラーが発生しています。正しいコードを以下に示します。


def check_answer(answer):
    global score

    if answer == questions[current_question_index - 1]["correct_answer"]:
        score += 1
        messagebox.showinfo("正解", "正解です!")
    else:
        messagebox.showinfo("不正解", "不正解です。")

    show_question()
    

これで正しいコードになります。申し訳ありませんが、前回の回答に誤りがあったことをお詫び申し上げます。

しかし、以上のようにコードを修正して、実行すると何も起こらずにすぐに終了してしまいます。

質問

メインループを

答え

メインループは以下のようになります。


# 初期化
current_question_index = 0
score = 0

# ウィンドウを表示する
show_question()
window.mainloop()
    

このコードは、最初に必要な変数を初期化し、show_question()関数を呼び出して最初の問題を表示します。そして、mainloop()関数を呼び出すことで、Tkinterアプリケーションがイベントループに入り、ユーザーの操作を待ち続けます。ウィンドウが閉じられるまで、プログラムはこのループ内で実行を続けます。

以上が、Tkinterを使って簡単なクイズゲームを作成するためのコードです。

ゲームの完成

上記のコードをまとめると次のコードになります。

game.py


import tkinter as tk
from tkinter import messagebox

questions = [
    {
        "question": "太陽系の第四惑星は?",
        "answers": ["火星", "木星", "水星", "金星"],
        "correct_answer": "火星"
    },
    {
        "question": "日本の首都は?",
        "answers": ["東京", "京都", "大阪", "名古屋"],
        "correct_answer": "東京"
    },
    {
        "question": "プログラミング言語Pythonの作者は?",
        "answers": ["ジェームズ・ゴスリング", "ブレンダン・アイク", "ガイ・スティール", "グイド・ヴァン・ロッサム"],
        "correct_answer": "グイド・ヴァン・ロッサム"
    }
]

window = tk.Tk()
window.title("クイズゲーム")

question_label = tk.Label(window, text="")
question_label.pack(pady=10)

answer_button1 = tk.Button(window, text="", command=lambda: check_answer(answer_button1["text"]))
answer_button1.pack(pady=5)

answer_button2 = tk.Button(window, text="", command=lambda: check_answer(answer_button2["text"]))
answer_button2.pack(pady=5)

answer_button3 = tk.Button(window, text="", command=lambda: check_answer(answer_button3["text"]))
answer_button3.pack(pady=5)

answer_button4 = tk.Button(window, text="", command=lambda: check_answer(answer_button4["text"]))
answer_button4.pack(pady=5)

def show_question():
    global current_question_index

    if current_question_index >= len(questions):
        messagebox.showinfo("結果", f"あなたのスコアは{score}です。")
        window.destroy()
        return

    question = questions[current_question_index]["question"]
    answers = questions[current_question_index]["answers"]
    correct_answer = questions[current_question_index]["correct_answer"]

    question_label["text"] = question
    answer_button1["text"] = answers[0]
    answer_button2["text"] = answers[1]
    answer_button3["text"] = answers[2]
    answer_button4["text"] = answers[3]

    current_question_index += 1

def check_answer(answer):
    global score

    if answer == questions[current_question_index - 1]["correct_answer"]:
        score += 1
        messagebox.showinfo("正解", "正解です!")
    else:
        messagebox.showinfo("不正解", "不正解です。")

    show_question()

# 初期化
current_question_index = 0
score = 0

# ウィンドウを表示する
show_question()
window.mainloop()
    




2216 visits
Posted: May. 07, 2023
Update: May. 07, 2023

ホーム   目次