Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Simple FLAMES game using Tkinter
Next article icon

TKinteris widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games. Let's try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds. Colors used in this game are Red, Blue, Green, Pink, Black, Yellow, Orange, White, Purple and Brown. Interface will display name of different colors in different colors. Player has to identify the color and enter the correct color name to win the game.

Prerequisite:Python GUI Tkinter

Below is the implementation of the above game: 

Python
# import the modulesimporttkinterimportrandom# list of possible colour.colours=['Red','Blue','Green','Pink','Black','Yellow','Orange','White','Purple','Brown']score=0# the game time left, initially 30 seconds.timeleft=30# function that will start the game.defstartGame(event):iftimeleft==30:# start the countdown timer.countdown()# run the function to# choose the next colour.nextColour()# Function to choose and# display the next colour.defnextColour():# use the globally declared 'score'# and 'play' variables above.globalscoreglobaltimeleft# if a game is currently in playiftimeleft>0:# make the text entry box active.e.focus_set()# if the colour typed is equal# to the colour of the textife.get().lower()==colours[1].lower():score+=1# clear the text entry box.e.delete(0,tkinter.END)random.shuffle(colours)# change the colour to type, by changing the# text _and_ the colour to a random colour valuelabel.config(fg=str(colours[1]),text=str(colours[0]))# update the score.scoreLabel.config(text="Score: "+str(score))# Countdown timer functiondefcountdown():globaltimeleft# if a game is in playiftimeleft>0:# decrement the timer.timeleft-=1# update the time left labeltimeLabel.config(text="Time left: "+str(timeleft))# run the function again after 1 second.timeLabel.after(1000,countdown)# Driver Code# create a GUI windowroot=tkinter.Tk()# set the titleroot.title("COLORGAME")# set the sizeroot.geometry("375x200")# add an instructions labelinstructions=tkinter.Label(root,text="Type in the colour""of the words, and not the word text!",font=('Helvetica',12))instructions.pack()# add a score labelscoreLabel=tkinter.Label(root,text="Press enter to start",font=('Helvetica',12))scoreLabel.pack()# add a time left labeltimeLabel=tkinter.Label(root,text="Time left: "+str(timeleft),font=('Helvetica',12))timeLabel.pack()# add a label for displaying the colourslabel=tkinter.Label(root,font=('Helvetica',60))label.pack()# add a text entry box for# typing in colourse=tkinter.Entry(root)# run the 'startGame' function# when the enter key is pressedroot.bind('<Return>',startGame)e.pack()# set focus on the entry boxe.focus_set()# start the GUIroot.mainloop()

Output :

Note: Above code may not run on online IDE because of TKinter module.

Code Explanation:

  1. The code starts by importing the necessary modules.
  2. The tkinter module provides the basic interface for creating graphical user interfaces in Python.
  3. Next, the colors variable stores a list of six colours, as well as a score variable.
  4. The startGame() function is called when the game starts.
  5. This function initializes some variables and sets up the game play.
  6. First, it checks to see if time has expired (in which case the game is over).
  7. If time hasn't expired, then it runs the countdown() function to keep track of how much time is left in the game.
  8. Finally, it sets up two labels—one to display the current time left and another to display the player's score.
  9. Next, we create a driver code block that creates and displays a window onscreen.
  10. We set its title and size before adding an instructions label to it.
  11. The instructions label contains text that tells players what they need to do in order to play this game: type in one of six colour values into the text entry box below it, not just word text!
  12. After you type in your colour value, hit enter key on your keyboard to submit it and begin playing!
  13. The code creates a window that has the title "COLORGAME" and a label for instructions.
  14. The window is 375x200 pixels in size, and the label is added to the window at (0, 0).
  15. The next section of code sets up some variables.
  16. The first variable, 'colours', stores an array of six colours: Red, Blue, Green, Pink, Black, and Yellow.
  17. The second variable, 'score', stores the current score of the game.
  18. The third variable, 'timeleft', keeps track of how much time is left in the game.
  19. The next section of code creates a function called 'startGame'.
  20. This function will be used to start the game timer when it is clicked on by the user.

How to Create a Color Game in Python using Tkinter
Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp