Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

6章 ヒット&ブローゲームをよりゲームらしくする

Last updated at Posted at 2020-05-29

ウィンドウを表示する


# coding:utf-8
import tkinter as tk

root = tk.Tk()
root.geometry("400x150")editboxl
root.title("数当てゲーム")

label1 = tk.Label(root, text="数を入力してください")
label1.place(x = 20, y = 20)

editbox1 = tk.Entry(width = 4)
editbox1.place(x = 120, y = 20)

root.mainloop()

root = tk.Tk()
root.mainloop()
この2行は、tkinterでウィンドウを表示するときの決まり文句である
##結果
実行結果

ボタンが押されたときのメッセージ表示

ボタンを配置する


buttonl = tk.Button(root, text = "チェック", font=("Helvetica", 14))
buttonl.place(x = 220, y = 60)

括弧のなかの引数「root」はボタンを配置する対象となるウィンドウを示し
text=""」はボタンに表示する文字を示している

メッセージを表示する

import tkinter.messagebox as tmsg

# ボタンがクリックされたときの処理
def ButtonClick():
    tmsg.showinfo("テスト", "クリックされました")

, command=ButtonClick

メッセージを表示するには「tkinter」の「messagebox」というパッケージに含まれる関数を使う.今回は「showinfo関数」を使いメッセージを表示する
※showinfo, showwarning, showerrorの違いは、画面に表示されるときのアイコンの違いである

結果

実行結果

#ヒット&ブローの当たり判定を組み込む

## 入力されたテキストの値を得る

 # テキスト入力欄に入力された文字を取得
    b = editboxl.get()
    # メッセージとして表示する
    tmsg.showinfo("入力されたテキスト", )

入力されたテキストはgetメソッドを使うことで取得できる
今回のプログラミングではテキスト入力欄を「editbox1」という変数に代入している
そのため1行目のようにしてgetメソッドを実行すると入力欄に入力されたテキストを取得できる

ヒット&ブローの値判定を作る

# coding:utf-8
import random
import tkinter as tk
import tkinter.messagebox as tmsg

# ボタンがクリックされたときの処理
def ButtonClick():
    # テキスト入力欄に入力された文字を取得
    b = editboxl.get()

    # Lesson 5-4のプログラムから判定部分を拝借
    # 4桁の数字かどうかを判定する
    isok = False
    if len(b) != 4:
        tmsg.showerror("エラー", "4桁の数字を入力してください")
    else:
         kazuok = True
         for i in range(4):
             if (b[i] <"0") or (b[i] > "9") :
                 tmsg.showerror("エラー", "数字ではありません")
                 kazuok = False
                 break
         if kazuok :
             isok = True

    if isok :
    # 4桁の数字であったとき
    # ヒットを判定
    hit = 0
    for i in range(4):
      if a[i] == int(b[i]):
        hit = hit + 1

    # ブローを判定
    blow = 0
    for j in range(4):
      for i in range(4):
        if (int(b[j]) == a[i]) and (a[i] != int(b[i])) and (a[j] != int(b[j])):
          blow = blow + 1
          break

    # ヒットが4なら当たりで終了(ウィンドウ向けに新たに調整)
    if hit == 4:
        tmsg.showinfo("当たり","おめでとうございます!")
        # 終了
        root.destroy()
    else:
        # ヒット数とブロー数を表示  
        tmsg.showinfo("ヒント","ヒット" + str(hit) + "/" + "ブロー" + str(blow))
    
# メインのプログラム
# 最初にランダムな4つの数字を作成しておく
a = [random.randint(0, 9),
     random.randint(0, 9),
     random.randint(0, 9),
     random.randint(0, 9),]

# ウィンドウ表示
root = tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")

# ラベルを作る
labell = tk.Label(root, text="数を入力してください", font=("Helvetica", 14))
labell.place(x = 20, y = 20)

# テキストボックスを作る
editboxl = tk.Entry(width = 4, font=("Helvetica", 28))
editboxl.place(x = 120, y = 60)

# ボタンを作る
buttonl = tk.Button(root, text = "チェック", font=("Helvetica", 14), command=ButtonClick)
buttonl.place(x = 220, y = 60)

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

前回作成したヒット&ブローゲームを組み込む(一部調整)
root.destroy()
これはdestroyメソッドといい、これを実行するとウィンドウが破棄されプログラムが終了する

#履歴を表示する

履歴を表示するテキストボックスを付ける

rirekibox = tk.Text(root, font=("Helvetica", 14))
rirekibox.place(x=400, y=0, width=200, height=400)
 

テキストボックスを作るには、Textメソッドを実行する
作ったテキストボックスを「rirekibox」という変数に代入する

履歴を表示する

rirekibox.insert(tk.END, b + " /H:" + str(hit) + " B:" + str(blow) + "\n")

テキストボックスに文字を追加するにはinsertメソッドを使用する
最初の引数に「tk.END」を指定すると「末尾」に挿入できる
※今回はtkinterを「import tkinter as tk」のように「tk」という名前でインポートしてるので「tk.END」で表す。ほかの名前でする場合は「その名前.END」というふうに表す

完成

ついに完成
スクリーンショット (22).png

  最後に:先生の記事

1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?