patternpythonMinor
Python colour game
Viewed 0 times
gamepythoncolour
Problem
This is a Python program that requires the user to type the colour of the text and not the name of the colour. They have a set time limit and a scoring system. If you have any improvements or suggestions I would greatly appreciate them.
```
#import the modules we need, for creating a GUI...
import tkinter
#...and for creating random numbers.
import random
#the list of possible colour.
colours = ['Red','Blue','Green','Pink','Black','Yellow','Orange','White','Purple','Brown']
#the player's score, initially 0.
score=0
#the game time left, initially 30 seconds.
timeleft=30
#a function that will start the game.
def startGame(event):
#if there's still time left...
if timeleft == 30:
#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 'play' variables above.
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 colour typed is equal to the colour of the text...
if e.get().lower() == colours[1].lower():
#...add one to the score.
score += 1
#clear the text entry box.
e.delete(0, tkinter.END)
#shuffle the list of colours.
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))
#a countdown timer function.
def countdown():
#use the globally declared 'play' variable above.
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: "
```
#import the modules we need, for creating a GUI...
import tkinter
#...and for creating random numbers.
import random
#the list of possible colour.
colours = ['Red','Blue','Green','Pink','Black','Yellow','Orange','White','Purple','Brown']
#the player's score, initially 0.
score=0
#the game time left, initially 30 seconds.
timeleft=30
#a function that will start the game.
def startGame(event):
#if there's still time left...
if timeleft == 30:
#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 'play' variables above.
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 colour typed is equal to the colour of the text...
if e.get().lower() == colours[1].lower():
#...add one to the score.
score += 1
#clear the text entry box.
e.delete(0, tkinter.END)
#shuffle the list of colours.
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))
#a countdown timer function.
def countdown():
#use the globally declared 'play' variable above.
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: "
Solution
Now, I can't code in python or very well read it. But you have a bunch of comments, like these
Even being someone that can't read the code, telling me that you're adding an instructions label isn't needed - I see you adding
My point being that a lot of your code speaks for itself (yay!) and you don't need those comments. Comments should explain why you do something, rather than what you do or how you do it. It will become easier to read and feel less cluttered.
#the list of possible colour.
colours = ['Red','Blue','Green','Pink','Black','Yellow','Orange','White','Purple','Brown']
#a function that will start the game.
def startGame(event):
#add an instructions label.
instructions = tkinter.Label(root, text="Type in the colour of the words, and not the word text!", font=('Helvetica', 12))
#add a score label.
scoreLabel = tkinter.Label(root, text="Press enter to start", font= ('Helvetica', 12))Even being someone that can't read the code, telling me that you're adding an instructions label isn't needed - I see you adding
instructions.My point being that a lot of your code speaks for itself (yay!) and you don't need those comments. Comments should explain why you do something, rather than what you do or how you do it. It will become easier to read and feel less cluttered.
Code Snippets
#the list of possible colour.
colours = ['Red','Blue','Green','Pink','Black','Yellow','Orange','White','Purple','Brown']
#a function that will start the game.
def startGame(event):
#add an instructions label.
instructions = tkinter.Label(root, text="Type in the colour of the words, and not the word text!", font=('Helvetica', 12))
#add a score label.
scoreLabel = tkinter.Label(root, text="Press enter to start", font= ('Helvetica', 12))Context
StackExchange Code Review Q#120535, answer score: 8
Revisions (0)
No revisions yet.