Tkinter là thư viện GUI phổ biến, dễ dùng để phát triển ứng dụng desktop và game đơn giản. Game này yêu cầu người chơi nhập tên màu sắc của từ hiển thị (không phải đọc từ), trong 30 giây.
Cách chơi
- Màn hình hiển thị tên một màu (ví dụ: "RED") nhưng được vẽ bằng màu khác (ví dụ: xanh dương).
- Người chơi phải gõ tên màu vẽ (xanh dương), không phải từ hiển thị.
- Đúng một lần được 1 điểm. Thời gian: 30 giây.
- Màu sử dụng: Red, Blue, Green, Pink, Black, Yellow, Orange, White, Purple, Brown.
Code hoàn chỉnh
import tkinter
import random
my_colours = ['Red','Blue','Green','Pink','Black','Yellow','Orange','White','Purple','Brown']
my_score = 0
my_timeleft = 30
def my_startGame(event):
if my_timeleft == 30:
my_countdown()
my_nextColour()
def my_nextColour():
global my_score, my_timeleft
if my_timeleft > 0:
e.focus_set()
if e.get().lower() == my_colours[1].lower():
my_score += 1
e.delete(0, tkinter.END)
random.shuffle(my_colours)
label.config(fg=str(my_colours[1]), text=str(my_colours[0]))
my_scoreLabel.config(text="Score: " + str(my_score))
def my_countdown():
global my_timeleft
if my_timeleft > 0:
my_timeleft -= 1
timeLabel.config(text="Time left: " + str(my_timeleft))
timeLabel.after(1000, my_countdown)
# Giao diện
root = tkinter.Tk()
root.title("COLOR GAME")
root.geometry("375x200")
tkinter.Label(root, text="Type in the color of the words, and not the word text!", font=('Helvetica', 12)).pack()
my_scoreLabel = tkinter.Label(root, text="Press enter to start", font=('Helvetica', 12))
my_scoreLabel.pack()
timeLabel = tkinter.Label(root, text="Time left: " + str(my_timeleft), font=('Helvetica', 12))
timeLabel.pack()
label = tkinter.Label(root, font=('Helvetica', 60))
label.pack()
e = tkinter.Entry(root)
root.bind('', my_startGame)
e.pack()
e.focus_set()
root.mainloop()
Giao diện game