Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
Impig, Sipocot, Camarines Sur 4408
Website: www.cbsua.edu.ph
Email Address:
[email protected] Trunkline: (054) 881-6681
COLLEGE OF INFORMATION TECHNOLOGY
CC 104 – Data Structures and Algorithms
2nd semester, AY 2023 – 2024
GUESS THE COLOR GAME
by: MAGADIA, ARJAY R.
Guess the Color Game Algorithm
1. Start
2. Display guess the color window, instructions and the timer
3. If the player wants to continue
4. Then proceed to STEP #6
5. Else end program
6. Start Timer
7. Display the filled colored text to guess
8. If the player has time left to guess
9. Then proceed to STEP #11
10. Else display game is over, the total correct score and proceed to STEP #5
11. Input guess word
12. If the player press enters and input valid guess word
13. Then proceed to STEP #15
14. Else display invalid text and proceed to STEP #7
15. If player’s guesses word is equal to the index of array
16. Then proceed to STEP #
17. Else display wrong and proceed to STEP #7
18. If the player has a list of arrays to guess
19. Then display correct and increment the score and proceed to STEP #7
20. Else disable enter button and proceed to STEP #10
Guess the Color Game Flowchart
Guess the Color Game Data Structures Used
1. Hash Map
2. Array list
3. Stacks
4. Queue
Guess the Color Game Source Code
import tkinter
import random
# initialize the game variables
score = 0
timeleft = 60
# create a list of colors
colours = ["red", "blue", "green", "yellow", "orange", "purple","black",
"brown","gray"]
# function that will start the game.
def startGame(event):
global timeleft
if timeleft == 60:
# start the countdown timer.
countdown()
# run the function to
# choose the next colour.
nextColour()
# Function to choose and
# display the next colour.
def nextColour():
global score
global timeleft
# if a game is currently in play
if timeleft > 0:
# make the text entry box active.
e.focus_set()
# if the user has entered a text
if len(e.get()) > 0:
# if the colour typed is equal to the colour of the text
if e.get().lower() == colours[1].lower():
correct_answer()
score += 1
else:
wrong_answer()
# 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.
scoreLabel.config(text="Score: " + str(score))
# if the user has not entered a text
else:
# reset the result label
resultLabel.config(text="")
# 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]))
def correct_answer():
# display a message indicating a correct answer
resultLabel.config(text="Correct!")
def wrong_answer():
# display a message indicating a wrong answer
resultLabel.config(text="Wrong!")
def invalid():
resultLabel.config(text="Invalid Text!")
# function that will start the countdown.
def countdown():
global timeleft
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.config(fg="red")
timeLabel.after(1000, countdown)
else:
# display a game over message
resultLabel.config(text="Game Over! Your score is " + str(score))
timeLabel.config(text="Time's up!")
# stop the countdown
return
# Driver Code
# create a GUI window
root = tkinter.Tk()
# set the title
root.title("COLOR GAME")
# set the size
root.geometry("375x200")
# add an instructions label
instructions = tkinter.Label(root, text="Identify the color"
" of the words, and not the word on the text!",
font=('Helvitica', 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)
# add a label for displaying the result
resultLabel = tkinter.Label(root, text="", font=('Arial', 12))
resultLabel.config(fg= "green")
resultLabel.pack()
# 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()
Word Guessing Game Screenshot (Output per process)
Figure#1: Displays the window for the guess the color window, instructions and timer
Figure#2: In this figure when the user presses start the guess colored filled text start to
displayed and at the same time the timer starts to countdown
Figure#3: In this figure when the player inputs the guess it displays the message
correct, increment the score above and displays the next color filled text
Figure#4: In this figure when the player inputs the guess it displays the message wrong
and displays the next color filled text
Figure#5: In this figure when the player runs out of time it displays the total score, game
over message and time is up message.