ゲームを作って確かめてみました。
Pythonで初めて作ったゲームです。クラス作成に慣れておらず、コード的に稚拙です。コードの訂正・修正案等につき、意見を頂けたら嬉しいです。
概要:
White216)216色の白が対象です。白はRGB値の順に並んでいます。ヒントボタンが2つあり、RGB値を表示します。
White125)125色の白が対象です。Changeボタンにより白がランダム配置となります。ヒントボタンは1つです。
感想:白は200色あるかもしれないが区別するのは難しい。125色でもランダムにすると難易度があがります。
注意事項:
作成PCは、MacBook Air (11-inch, Early 2015)
モニターのピクセルサイズは、(width=1152, height=648)
MacOSでは、buttonのbgが機能しないので、tkmacosxを使用。画面のレイアウトが崩れたり、Windowsでは動作しない可能性あり。
疑問点:
tkinterでframeが重なっている時、初期状態でのframeの順番は、配置ではなく定義順(最初に定義したframeが一番下に位置し、順番に重なっていく)という理解で正しいのかな。
White 216
Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
White216: Pick the same color from white color family.
HzR supported by DMM.make Akiba Startline(Jul. 2022)
"""
import tkinter as tk
from tkmacosx import Button
import random
def count_up():
global count
count = count + 1
def button_func(event):
global comment
event.widget.config(fg="red", relief = tk.SUNKEN)
if event.widget.cget("bg") == cc_q:
comment = "正解です:\n "+str(count)+"回\nかかりました"
text.set(comment)
else:
comment = "不正解です"
text.set(comment)
count = 1
root = tk.Tk()
root.geometry("1040x580")
root.title("White 216")
root.configure(bg='white')
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
fr1 = tk.Frame(root, width=140, height=580, relief= 'flat', bd=1, bg="white")
fr2 = tk.Frame(root, width=900, height=580, relief= 'flat', bd=1, bg="white")
fr3 = tk.Frame(root, width=900, height=580, relief= 'flat', bd=1, bg="white")
fr1.grid(row=0, column=0, sticky="nsew")
fr2.grid(row=0, column=1, sticky="nsew")
fr3.grid(row=0, column=1, sticky="nsew")
def outer(x):
def inner():
#print(x)
count_up()
return inner
rgblist = []
cclist =[]
#color code 216
for i in range(255, 225, -5):
for j in range(255, 225, -5):
for k in range(255, 225, -5):
tuple = (i, j, k)
rgblist.append(tuple)
#rgb2hex
color_code = '#{}{}{}'.format(hex(i), hex(j), hex(k))
color_code = color_code.replace('0x', '')
cclist.append(color_code)
color_dic = dict(zip(cclist, rgblist))
# random colorlist
zip_obj = zip(cclist, rgblist)
zip_lst = list(zip_obj)
zip_rlst=random.sample(zip_lst, len(zip_lst))
# arranged color buttons with text
for i, (cc,rgb) in enumerate(zip(cclist, rgblist)):
button_i= Button(fr2, relief = tk.RAISED, bd =1, width = 100, height =22, text = rgb, font=('arial', 10), bg=cc, command = outer(rgb))
button_i.grid(column=i//24, row=i%24)
button_i.bind("<ButtonPress>", button_func)
# no text
for i, (cc,rgb) in enumerate(zip(cclist, rgblist)):
button_i= Button(fr3, relief = tk.RAISED, bd =1, width = 100, height =22, bg=cc, command=outer(rgb))
button_i.grid(column=i//24, row=i%24)
button_i.bind("<ButtonPress>", button_func)
mes1 = tk.Message(fr1, relief = tk.SOLID, width = 100, bg = "white", bd = 0, text = "白って200色\nあんねん\n by アンミカ")
mes1.pack(pady =20)
mes2 = tk.Message(fr1, relief = tk.SOLID, width = 100, bg = "white", text = "下と同じ色を、右のタイルから選んでクリックしよう")
mes2.pack(pady =20)
# question
cc_q = random.choice(cclist)
rgb_q = color_dic[cc_q]
button_q = Button(fr1, relief = tk.FLAT, bd =1, width = 100, height =30,
bg=cc_q, )
button_q.pack()
print("正解(RGB): ", end = ' ')
print(rgb_q)
labelfr3 = tk.LabelFrame(fr1, text = "RGB hint", bg = "white")
labelfr3.pack(pady=10)
label3 = tk.Label(labelfr3, width=10, height=2, text=rgb_q, bg ="white")
# hint buttons
but6 = Button(fr1, relief = tk.RIDGE, bd =5, width = 50, height =40, text = "HINT2", fg= "white", bg = "#F08000", command = fr2.tkraise )
but6.pack(side = tk.BOTTOM, pady = 20)
but5 = Button(fr1, relief = tk.RIDGE, bd =5, width = 50, height =40, text = "HINT1", fg= "white", bg = "#0095B6", command = label3.pack )
but5.pack(side = tk.BOTTOM, pady = 20)
text = tk.StringVar(fr1)
mes4 = tk.Message(fr1, relief = tk.SOLID, width = 100, bd = 0, bg = "white", textvariable=text)
mes4.pack(side = tk.BOTTOM, pady = 20)
root.mainloop()
White 125
Python
import tkinter as tk
from tkmacosx import Button
import random
def count_up():
global count
count = count + 1
def button_func(event):
global comment
event.widget.config(fg="red", relief = tk.SOLID)
if event.widget.cget("bg") == cc_q:
comment = "正解です:\n "+str(count)+"回\nかかりました"
text.set(comment)
else:
comment = "不正解です"
text.set(comment)
#change frame
def on_button():
global flag
if flag == 1:
flag = 2
fr4.tkraise()
elif flag == 2:
flag = 1
fr3.tkraise()
count = 1
flag = 1
root = tk.Tk()
root.geometry("1040x580")
root.title("White 125")
root.configure(bg='white')
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
#stacking order? bottom to top
fr4 = tk.Frame(root, width=900, height=580, relief= 'flat', bd=1, bg="white")
fr1 = tk.Frame(root, width=140, height=580, relief= 'flat', bd=1, bg="white")
fr3 = tk.Frame(root, width=900, height=580, relief= 'flat', bd=1, bg="white")
fr1.grid(row=0, column=0, sticky="nsew")
fr3.grid(row=0, column=1, sticky="nsew")
fr4.grid(row=0, column=1, sticky="nsew")
def outer(x):
def inner():
#print(x)
count_up()
return inner
rgblist = []
cclist =[]
# color code 125
for i in range(255, 230, -5):
for j in range(255, 230, -5):
for k in range(255, 230, -5):
tuple = (i, j, k)
rgblist.append(tuple)
#rgb2hex
color_code = '#{}{}{}'.format(hex(i), hex(j), hex(k))
color_code = color_code.replace('0x', '')
cclist.append(color_code)
color_dic = dict(zip(cclist, rgblist))
# random colorlist
zip_obj = zip(cclist, rgblist)
zip_lst = list(zip_obj)
zip_rlst=random.sample(zip_lst, len(zip_lst))
# arranged color buttons without text
for i, (cc,rgb) in enumerate(zip(cclist, rgblist)):
button_i= Button(fr3, relief = tk.RAISED, bd =1, width = 100, height =39, bg=cc, command=outer(rgb))
button_i.grid(column=i//14, row=i%14)
button_i.bind("<ButtonPress>", button_func)
# random color buttons
for i, (cc,rgb) in enumerate(zip_rlst):
button_i= Button(fr4, relief = tk.RAISED, bd =1, width = 100, height =39, bg=cc, command=outer(rgb))
button_i.grid(column=i//14, row=i%14)
button_i.bind("<ButtonPress>", button_func)
mes1 = tk.Message(fr1, relief = tk.SOLID, width = 100, bg = "white", bd = 0, text = "白って200色\nあんねん\n by アンミカ")
mes1.pack(pady =20)
mes2 = tk.Message(fr1, relief = tk.SOLID, width = 100, bg = "white", text = "下と同じ色を、右のタイルから選んでクリックしよう")
mes2.pack(pady =20)
# question
cc_q = random.choice(cclist)
rgb_q = color_dic[cc_q]
button_q = Button(fr1, relief = tk.FLAT, bd =1, width = 100, height =40, bg=cc_q, )
button_q.pack()
print("正解(RGB): ", end = ' ')
print(rgb_q)
labelfr3 = tk.LabelFrame(fr1, text = "RGB hint", bg = "white")
labelfr3.pack(pady=20)
label3 = tk.Label(labelfr3, width=10, height=2, text=rgb_q, bg ="white")
# change button
but6 = Button(fr1, relief = tk.RIDGE, bd =10, width = 50, height =40, text = "CHANGE", font=('arial', 10), bg = "yellow", command=on_button)
but6.pack(side = tk.BOTTOM, pady = 10)
but5 = Button(fr1, relief = tk.RIDGE, bd =5, width = 50, height =40, text = "HINT", fg= "white", bg = "indigo", command = label3.pack)
but5.pack(side = tk.BOTTOM, pady = 10)
text = tk.StringVar(fr1)
mes4 = tk.Message(fr1, relief = tk.SOLID, width = 100, bd = 0, bg = "white", textvariable=text)
mes4.pack(side = tk.BOTTOM, pady = 20)
root.mainloop()