Skip to main content
added 166 characters in body
Source Link
toolic
  • 15.9k
  • 6
  • 29
  • 217

UX

It is not obvious what the user should do when the GUI opens up. You should display some simple instructions in the GUI, such as:

Use arrow keys on keyboard to move right, left, etc.

When I exit out of the GUI window, I see several errors in the shell in which I ran the game. Perhaps this is specific to the version of Python I am using.

Efficiency

In the snake_move function, the 4 if statements should be combined into a single if/elif because the 4 conditions are mutually exclusive:

def snake_move():
    if snake.direction == "up":
        y = snake.ycor()
        snake.sety(y + 20)
    elif snake.direction == "down":
        y = snake.ycor()
        snake.sety(y - 20)
    elif snake.direction == "left":
        x = snake.xcor()
        snake.setx(x - 20)
    elif snake.direction == "right":
        x = snake.xcor()
        snake.setx(x + 20)

You should also be able to eliminate the intermediate variables. For example:

    if snake.direction == "up":
        snake.sety(snake.ycor() + 20)

Magic number

The 20 in the snake_move function should be a declared constant. You should also add a comment describing why you chose this value.

DRY

The 4 functions:

def snake_go_up():
def snake_go_down():
etc.

could be combined into a single function with an argument passed in corresponding to the direction.

This group of lines is repeated twice:

time.sleep(1)
screen.clear()
screen.bgcolor('turquoise')
scoring.goto(0,0)
scoring.write("   GAME OVER \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))

They can be placed into a function.

Layout

Move the functions to the top after the import lines. Having them in the middle of the code interrupts the natural flow of the code (from a human readability standpoint).

There is inconsistent whitespace around operators, such as

            a= snake.xcor()
            b = snake.ycor()

The black program can be used to automatically format the code to add consistency. This will also help with the inconsistent indentation and excessive blank lines.

UX

It is not obvious what the user should do when the GUI opens up. You should display some simple instructions in the GUI, such as:

Use arrow keys on keyboard to move right, left, etc.

When I exit out of the GUI window, I see several errors in the shell in which I ran the game. Perhaps this is specific to the version of Python I am using.

Efficiency

In the snake_move function, the 4 if statements should be combined into a single if/elif because the 4 conditions are mutually exclusive:

def snake_move():
    if snake.direction == "up":
        y = snake.ycor()
        snake.sety(y + 20)
    elif snake.direction == "down":
        y = snake.ycor()
        snake.sety(y - 20)
    elif snake.direction == "left":
        x = snake.xcor()
        snake.setx(x - 20)
    elif snake.direction == "right":
        x = snake.xcor()
        snake.setx(x + 20)

Magic number

The 20 in the snake_move function should be a declared constant. You should also add a comment describing why you chose this value.

DRY

The 4 functions:

def snake_go_up():
def snake_go_down():
etc.

could be combined into a single function with an argument passed in corresponding to the direction.

This group of lines is repeated twice:

time.sleep(1)
screen.clear()
screen.bgcolor('turquoise')
scoring.goto(0,0)
scoring.write("   GAME OVER \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))

They can be placed into a function.

Layout

Move the functions to the top after the import lines. Having them in the middle of the code interrupts the natural flow of the code (from a human readability standpoint).

There is inconsistent whitespace around operators, such as

            a= snake.xcor()
            b = snake.ycor()

The black program can be used to automatically format the code to add consistency. This will also help with the inconsistent indentation and excessive blank lines.

UX

It is not obvious what the user should do when the GUI opens up. You should display some simple instructions in the GUI, such as:

Use arrow keys on keyboard to move right, left, etc.

When I exit out of the GUI window, I see several errors in the shell in which I ran the game. Perhaps this is specific to the version of Python I am using.

Efficiency

In the snake_move function, the 4 if statements should be combined into a single if/elif because the 4 conditions are mutually exclusive:

def snake_move():
    if snake.direction == "up":
        y = snake.ycor()
        snake.sety(y + 20)
    elif snake.direction == "down":
        y = snake.ycor()
        snake.sety(y - 20)
    elif snake.direction == "left":
        x = snake.xcor()
        snake.setx(x - 20)
    elif snake.direction == "right":
        x = snake.xcor()
        snake.setx(x + 20)

You should also be able to eliminate the intermediate variables. For example:

    if snake.direction == "up":
        snake.sety(snake.ycor() + 20)

Magic number

The 20 in the snake_move function should be a declared constant. You should also add a comment describing why you chose this value.

DRY

The 4 functions:

def snake_go_up():
def snake_go_down():
etc.

could be combined into a single function with an argument passed in corresponding to the direction.

This group of lines is repeated twice:

time.sleep(1)
screen.clear()
screen.bgcolor('turquoise')
scoring.goto(0,0)
scoring.write("   GAME OVER \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))

They can be placed into a function.

Layout

Move the functions to the top after the import lines. Having them in the middle of the code interrupts the natural flow of the code (from a human readability standpoint).

There is inconsistent whitespace around operators, such as

            a= snake.xcor()
            b = snake.ycor()

The black program can be used to automatically format the code to add consistency. This will also help with the inconsistent indentation and excessive blank lines.

Source Link
toolic
  • 15.9k
  • 6
  • 29
  • 217

UX

It is not obvious what the user should do when the GUI opens up. You should display some simple instructions in the GUI, such as:

Use arrow keys on keyboard to move right, left, etc.

When I exit out of the GUI window, I see several errors in the shell in which I ran the game. Perhaps this is specific to the version of Python I am using.

Efficiency

In the snake_move function, the 4 if statements should be combined into a single if/elif because the 4 conditions are mutually exclusive:

def snake_move():
    if snake.direction == "up":
        y = snake.ycor()
        snake.sety(y + 20)
    elif snake.direction == "down":
        y = snake.ycor()
        snake.sety(y - 20)
    elif snake.direction == "left":
        x = snake.xcor()
        snake.setx(x - 20)
    elif snake.direction == "right":
        x = snake.xcor()
        snake.setx(x + 20)

Magic number

The 20 in the snake_move function should be a declared constant. You should also add a comment describing why you chose this value.

DRY

The 4 functions:

def snake_go_up():
def snake_go_down():
etc.

could be combined into a single function with an argument passed in corresponding to the direction.

This group of lines is repeated twice:

time.sleep(1)
screen.clear()
screen.bgcolor('turquoise')
scoring.goto(0,0)
scoring.write("   GAME OVER \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))

They can be placed into a function.

Layout

Move the functions to the top after the import lines. Having them in the middle of the code interrupts the natural flow of the code (from a human readability standpoint).

There is inconsistent whitespace around operators, such as

            a= snake.xcor()
            b = snake.ycor()

The black program can be used to automatically format the code to add consistency. This will also help with the inconsistent indentation and excessive blank lines.