patternpythonMinor
Simon memory game
Viewed 0 times
gamememorysimon
Problem
I have recently started to learn Python and I have decided to apply what I have learnt so far to creating a Simon Memory Game clone. I am now looking for help with improving the code. Any help with improving this code would be greatly appreciated and criticism is welcome. I'm using Python 3.5.
```
import tkinter
from random import choice
class Simon() :
def __init__(self, master) :
# Configure tkinter.Tk with some basic settings.
self.master = master
self.master.minsize(640, 480)
self.master.resizable(False, False)
self.master.title("Simon Memory Game")
self.master.update() # Complete any outstanding tkinter tasks.
# Create the canvas to be used to draw the rectangles that make up the game. Have it take up the entire window.
self.game_canvas = tkinter.Canvas(self.master, width = self.master.winfo_width(), height = self.master.winfo_height(), highlightthickness = 0)
self.game_canvas.pack()
# Set up the four colors that will be used throughout the game.
self.idle_colors = ("red", "blue", "green", "yellow")
self.tinted_colors = ("#ff4d4d", "#4d4dff", "#4dff4d", "#ffff4d")
self.current_colors = [color for color in self.idle_colors]
self.rectangle_ids = []
# Sequence of the colors for the current game and the position in the sequence for use when showing it to the user.
self.sequence = [choice(self.idle_colors)]
self.sequence_position = 0
self.draw_canvas()
self.show_sequence()
self.master.mainloop()
# Show the sequence to the player, so the player can repeat it.
def show_sequence(self) :
# Pass the current color of the sequence to the flash function.
self.flash(self.sequence[self.sequence_position])
# Check that we have not reached the end of the sequence.
if(self.sequence_position ', lambda e : self.check_choice())
def main() :
root = tkinter.Tk()
```
import tkinter
from random import choice
class Simon() :
def __init__(self, master) :
# Configure tkinter.Tk with some basic settings.
self.master = master
self.master.minsize(640, 480)
self.master.resizable(False, False)
self.master.title("Simon Memory Game")
self.master.update() # Complete any outstanding tkinter tasks.
# Create the canvas to be used to draw the rectangles that make up the game. Have it take up the entire window.
self.game_canvas = tkinter.Canvas(self.master, width = self.master.winfo_width(), height = self.master.winfo_height(), highlightthickness = 0)
self.game_canvas.pack()
# Set up the four colors that will be used throughout the game.
self.idle_colors = ("red", "blue", "green", "yellow")
self.tinted_colors = ("#ff4d4d", "#4d4dff", "#4dff4d", "#ffff4d")
self.current_colors = [color for color in self.idle_colors]
self.rectangle_ids = []
# Sequence of the colors for the current game and the position in the sequence for use when showing it to the user.
self.sequence = [choice(self.idle_colors)]
self.sequence_position = 0
self.draw_canvas()
self.show_sequence()
self.master.mainloop()
# Show the sequence to the player, so the player can repeat it.
def show_sequence(self) :
# Pass the current color of the sequence to the flash function.
self.flash(self.sequence[self.sequence_position])
# Check that we have not reached the end of the sequence.
if(self.sequence_position ', lambda e : self.check_choice())
def main() :
root = tkinter.Tk()
Solution
Most of this will come down to reading PEP8, however I'll state them here anyway.
-
-
Functions at module level should have two new lines between them and other things, rather than one as you have now
-
In your function calls, you have unexpected spaces around keyword/parameter assignments
-
There should also be two blank lines between the
-
You should have at least two spaces before inline comment.
-
Many of your lines are too long, they should only be 79 characters maximum
-
In Whitespace in Expressions and Statements,
Avoid extraneous whitespace in the following situations:
...
Immediately before a comma, semicolon, or colon:
-
You shouldn't have multiple statements on one line (there should be a newline after the colon)
-
There is no newline at end of file as seen here.
-
if statements don't need parentheses, so don't put them around them. You seem to go between doing this and not, remember to also be consistent-
Functions at module level should have two new lines between them and other things, rather than one as you have now
-
In your function calls, you have unexpected spaces around keyword/parameter assignments
-
There should also be two blank lines between the
import statements and other code-
You should have at least two spaces before inline comment.
-
Many of your lines are too long, they should only be 79 characters maximum
-
In Whitespace in Expressions and Statements,
Avoid extraneous whitespace in the following situations:
...
Immediately before a comma, semicolon, or colon:
Yes: if x == 4: print x, y; x, y = y, x
No: if x == 4 : print x , y ; x , y = y , x-
You shouldn't have multiple statements on one line (there should be a newline after the colon)
-
There is no newline at end of file as seen here.
Code Snippets
Yes: if x == 4: print x, y; x, y = y, x
No: if x == 4 : print x , y ; x , y = y , xContext
StackExchange Code Review Q#136064, answer score: 5
Revisions (0)
No revisions yet.