color guessing game with GUI using python

     Welcome to color guessing game with GUI



This is the simple color guessing game you can make more complex or hard this game and other is you can add also level to take more interest to user.





import tkinter
import random
import tkinter.simpledialog

# List of possible colours.
colours = ['Red', 'Blue', 'Green', 'Pink', 'Black',
'Yellow', 'Orange', 'White', 'Purple', 'Brown']
score = 0
incorrect_guesses = 0


# Function to prompt the user for game duration.
def getGameDuration():
duration = tkinter.simpledialog.askinteger("Game Duration", "Enter game duration in seconds:")
return duration


# Set the game duration by prompting the user.
GAME_DURATION = getGameDuration()

# The game time left, initially GAME_DURATION seconds.
timeleft = GAME_DURATION


# Function that will start the game.
def startGame(event):
if timeleft == GAME_DURATION:
# Start the countdown timer.
countdown()

# Run the function to
# choose the next colour.
nextColour()


# Function to choose and display the next colour.
def nextColour():
# Use the globally declared 'score'
# and 'incorrect_guesses' variables above.
global score
global timeleft
global incorrect_guesses

# If a game is currently in play
if timeleft > 0:

# Make the text entry box active.
e.focus_set()

# If the colour typed is equal
# to the colour of the text
if e.get().lower() == colours[1].lower():

score += 1
else:
incorrect_guesses += 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 value
label.config(fg=str(colours[1]), text=str(colours[0]))

# Update the score.
updateScoreLabel()


# Countdown timer function
def countdown():
global timeleft

# If a game is in play
if timeleft > 0:

# Decrement the timer.
timeleft -= 1

# Update the time left label
timeLabel.config(text="Time left: " + str(timeleft))

# Run the function again after 1 second.
timeLabel.after(1000, countdown)
else:
# Game over, display final score.
gameOver()


# Function to display the final score.
def gameOver():
scoreLabel.config(text=f"Game Over! Final Score: {score}. Incorrect guesses: {incorrect_guesses}")


# Update the score label.
def updateScoreLabel():
scoreLabel.config(text="Score: " + str(score))


# Driver Code

# Create a GUI window
root = tkinter.Tk()

# Set the title
root.title("COLORGAME")

# Set the size
root.geometry("375x200")

# Add an instructions label
instructions = tkinter.Label(root, text="Type in the colour"
"of the words, and not the word text!",
font=('Helvetica', 12))
instructions.pack()

# Add a score label
scoreLabel = tkinter.Label(root, text="Press enter to start",
font=('Helvetica', 12))
scoreLabel.pack()

# Add a time left label
timeLabel = tkinter.Label(root, text="Time left: " +
str(timeleft), font=('Helvetica', 12))

timeLabel.pack()

# Add a label for displaying the colours
label = tkinter.Label(root, font=('Helvetica', 60))
label.pack()

# Add a text entry box for
# typing in colours
e = tkinter.Entry(root)

# Run the 'startGame' function
# when the enter key is pressed
root.bind('<Return>', startGame)
e.pack()

# Set focus on the entry box
e.focus_set()

# Start the GUI
root.mainloop() 

Comments

Popular posts from this blog

Screen Recording using python.

wallpaper changer.

Find any color from image using python