GUI অ্যাপ্লিকেশন বিকাশের জন্য tkinter খুবই জনপ্রিয় এবং সহজ। tkinter ব্যবহার করে সহজেই GUI গেমস ডেভেলপ করুন।
এখানেও আমরা কালার গেম ডেভেলপ করার চেষ্টা করছি। এই গেমটিতে খেলোয়াড়কে স্ক্রিনে প্রদর্শিত শব্দের রঙ লিখতে হয় এবং তাই স্কোর এক দ্বারা বৃদ্ধি পায়, এই গেমটি খেলার মোট সময় 30 সেকেন্ড এবং এই গেমটিতে ব্যবহৃত রঙগুলি হল লাল, নীল, সবুজ, গোলাপী, কালো, হলুদ, কমলা, সাদা, বেগুনি এবং বাদামী। ইন্টারফেস বিভিন্ন রঙের বিভিন্ন রঙের নাম প্রদর্শন করবে। গেম জিততে ব্যবহারকারীকে রঙ সনাক্ত করতে হবে এবং সঠিক রঙের নাম লিখতে হবে।
উদাহরণ কোড
import tkinter
import random
# list of colour.
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:
# start the countdown timer.
my_countdown()
my_nextColour()
def my_nextColour():
global my_score
global my_timeleft
# if a game is currently in play
if my_timeleft > 0:
e.focus_set()
if e.get().lower() == my_colours[1].lower():
my_score += 1
# clear the text entry box.
e.delete(0, tkinter.END)
random.shuffle(my_colours)
label.config(fg = str(my_colours[1]), text = str(my_colours[0]))
# update the score.
my_scoreLabel.config(text = "Score: " + str(my_score))
# Countdown timer function
def my_countdown():
global my_timeleft
# if a game is in play
if my_timeleft > 0:
# decrement the timer.
my_timeleft -= 1
# update the time left label
timeLabel.config(text = "Time left: "+ str(my_timeleft))
# run the function again after 1 second.
timeLabel.after(1000, my_countdown)
# Driver Code
root = tkinter.Tk()
root.title("COLORGAME")
root.geometry("375x200")
my_instructions = tkinter.Label(root, text = "Type in the color" "of the words, and not the word text!",
font = ('Helvetica', 12))
my_instructions.pack()
my_scoreLabel = tkinter.Label(root, text = "Press enter to start",
font = ('Helvetica', 12))
my_scoreLabel.pack()
my_timeLabel = tkinter.Label(root, text = "Time left: " +
str(my_timeleft), font = ('Helvetica', 12))
my_timeLabel.pack()
label = tkinter.Label(root, font = ('Helvetica', 60))
label.pack()
e = tkinter.Entry(root)
root.bind('<Return>', my_startGame)
e.pack()
e.focus_set()
# start the GUI
root.mainloop()
আউটপুট
<কেন্দ্র>
<কেন্দ্র>