HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Snake game made in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gamemadepythonsnake

Problem

I made a snake game with /some/ code from thenewboston's tutorials when I had just started Python. I think it wasn't a good idea to start game development right away since I didn't even know if what I did was correctly coded.

I didn't use classes and objects since I didn't know how to use them. I'm sure this needs a lot of improving.

Game.py

```
import pygame
import random

pygame.init()

pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 155, 0)
RED = (255, 0, 0)
CYAN = (0, 255, 255)
DARK_CYAN = (123, 104, 238)
YELLOW = (247, 255, 0)

game_title = 'Retro Worm'
display_width = 800
display_height = 600
background_color = BLACK
snake_color = DARK_CYAN
energy_color = DARK_CYAN
polygon_powerup_color = (random.randrange(255), random.randrange(255), random.randrange(255))

game_display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption(game_title)

global block_size
block_size = 10

clock = pygame.time.Clock()

font = pygame.font.SysFont(None, 25)

def get_collision(p_x, p_y, target_x, target_y, width, height):
if p_x >= target_x and p_x = target_y and p_y = display_height or item_y = display_width or lead_x = display_height or lead_y = 300:
pygame.draw.rect(game_display, YELLOW, [down_bombs_x[0], down_bombs_y[0], block_size, block_size])
pygame.draw.rect(game_display, YELLOW, [down_bombs_x[1], down_bombs_y[1], block_size, block_size])
pygame.draw.rect(game_display, YELLOW, [down_bombs_x[2], down_bombs_y[2], block_size, block_size])
pygame.draw.rect(game_display, YELLOW, [down_bombs_x[3], down_bombs_y[3], block_size, block_size])
if score >= 500:
pygame.draw.rect(game_display, YELLOW, [right_bombs_x[0], right_bombs_y[0], block_size, block_size])
pygame.draw.rect(game_display, YELLOW, [right_bombs_x[1], right_bombs_y[1], block_size, block_size])

Solution

First off, for a beginner, this code is actually quite good! I do have some suggestions for improvement though, specifically regarding documentation and such.

  • First off, you did great with PEP8. The only errors I'm getting back are that there are too many lines greater than 79 characters long. You do have an unnecessary semicolon on line 233 though.



  • You have a lot of code in your game_loop function. I would recommend separating this function into smaller functions, each with their own individual purpose. This will help improve readability as well.



  • You're missing documentation in your code. The only comments that I see are useless ones like # DOWN BOMBS, or # Event handling. Comments should be descriptive, and describe a block of code well. For functions, you should use docstrings to describe what they do. Here's an example.



def my_function(args):
    """
    Flesh this docstring out with useful
    information about the function.
    """
    # Code goes here


  • Another thing that really sticks out here is your naming. The variables you have at the top of the file are mostly all constants. If you have a constant variable (value doesn't change), it's name should be in all uppercase. For example, if a variable has the name BLOCK_SIZE, we can tell that it's a constant describing the block size.



  • With the issue of naming continued, a few of your variable names are a little hard to understand, for example, from looking at the function argument b_x, I don't know what it does. If it's an abbreviation of something, and it's this short, don't abbreviate it.



  • My final tip is to format lists, dictionaries, and tuples like the below example. Not in the current form you're doing it. It mostly just improves readability.



my_list = [
    an_item
    ...
]


In short, your code is pretty good! I hope that this answer helped you improve your code!

Code Snippets

def my_function(args):
    """
    Flesh this docstring out with useful
    information about the function.
    """
    # Code goes here
my_list = [
    an_item
    ...
]

Context

StackExchange Code Review Q#88917, answer score: 6

Revisions (0)

No revisions yet.