5
\$\begingroup\$

This is the bare bones of a SNAKE game that I'm working on, the code runs as is, I've taken great care to keep my code clean and readable. I would appreciate feedback on my code. I plan on creating some graphics to greatly improve the presentation and a menu and high scores.

import sys
import random
import pygame

pygame.init()


class Snake:

    RED = (200, 0, 0)
    BLUE = (0, 0, 190)
    BLACK = (0, 0, 0)
    GREEN = (0, 230, 0)

    score = 0
    xpos = 80
    ypos = 16
    direction = (1, 0)
    snake_tail = [[16, 16], [32, 16], [48, 16], [64, 16], [80, 16]]

    keys = {"GO": pygame.KEYDOWN, "UP": pygame.K_UP, "DOWN": pygame.K_DOWN,
            "LEFT": pygame.K_LEFT, "RIGHT": pygame.K_RIGHT
            }

    fps = pygame.time.Clock()

    def __init__(self, width, height):

        self.width = width
        self.height = height
        self.food_x = random.randint(1, width - 1) * 16
        self.food_y = random.randint(1, height - 1) * 16
        self.screen = pygame.display.set_mode((width * 16, height * 16))

    def main_loop(self):

        while True:

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

                if event.type == self.keys["GO"] and event.key == self.keys["RIGHT"]:
                    self.direction = (1, 0)
                if event.type == self.keys["GO"] and event.key == self.keys["LEFT"]:
                    self.direction = (-1, 0)
                if event.type == self.keys["GO"] and event.key == self.keys["UP"]:
                    self.direction = (0, -1)
                if event.type == self.keys["GO"] and event.key == self.keys["DOWN"]:
                    self.direction = (0, 1)

            self.move_snake()
            self.fps.tick(12)

    def move_snake(self):

        self.xpos += self.direction[0] * 16
        self.ypos += self.direction[1] * 16
        self.snake_tail.append([self.xpos, self.ypos])
        segment = self.snake_tail.pop(0)

        pygame.draw.circle(self.screen, self.BLACK, (segment[0], segment[1]), 8)

        for segments in self.snake_tail:
            pygame.draw.circle(self.screen, self.BLUE, (segments[0], segments[1]), 8)

        pygame.draw.circle(self.screen, self.GREEN, (self.food_x, self.food_y), 8)
        pygame.draw.circle(self.screen, self.RED, (segments[0], segments[1]), 8)
        pygame.display.update()

        self.kill_snake()

    def kill_snake(self):

        if self.snake_tail[-1] in self.snake_tail[:-1]:
            pygame.quit()
            sys.exit()
        if self.xpos <= 0 or self.xpos >= self.width * 16 or self.ypos <= 0 or self.ypos >= self.height * 16:
            pygame.quit()
            sys.exit()

        self.feed_snake()

    def feed_snake(self):

        if self.xpos == self.food_x and self.ypos == self.food_y:
            self.score += 1
            pygame.display.set_caption(str(self.score))
            self.snake_tail.insert(0, [self.xpos, self.ypos])
            self.food_x = (random.randint(1, self.width - 1)) * 16
            self.food_y = (random.randint(1, self.height - 1)) * 16

Snake(25, 25).main_loop()
\$\endgroup\$
1
\$\begingroup\$

There are two main issues with your code.

Comments

Explain your code and what you are trying to do. If you keep developing this game, after some time you will have problems to understand what you are doing.

Especially explain the move_snake(), kill_snake() and feed_snake() functions, but also the usage of the variables.

Constants

Rename all constants, like keys to KEYS.

Access all constants with e.g. Snake.KEYS because they are constants, no class variables that are different for every instance.

To improve readability, you can change the indention for KEYS like this:

KEYS = {"GO": pygame.KEYDOWN,
        "UP": pygame.K_UP, 
        "DOWN": pygame.K_DOWN, 
        "LEFT": pygame.K_LEFT, 
        "RIGHT": pygame.K_RIGHT }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.