STAY INFORMED
following content serves as a personal note and may lack complete accuracy or certainty.

Minimal-Mistakes instruction
Useful vscode Shortcut Keys
Unix Commands
npm Commands
Vim Commands
Git Note
Useful Figma Shortcut Keys

1 minute read

main function

All set up now. I need to modify the main function.

running = True

    # create snake object
    snake = Snake(width / 2, height / 2)

    # create food object
    food = Food()

    # show food at random location
    food.new_location()

     while running:

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                if event.key == pygame.K_UP:
                    snake.x_dir = 0
                    snake.y_dir = -1
                if event.key == pygame.K_DOWN:
                    snake.x_dir = 0
                    snake.y_dir = 1
                if event.key == pygame.K_LEFT:
                    snake.x_dir = -1
                    snake.y_dir = 0
                if event.key == pygame.K_RIGHT:
                    snake.x_dir = 1
                    snake.y_dir = 0
        # clear the surface
        display.fill(background)

        # show score
        show_score()

        # show objects
        snake.show()
        food.show()

        snake.update()

        # collision detection
        if snake.check_eaten():
            food.new_location()
            score += 1
            snake.grow()

        # update screen
        pygame.display.update()

        # 10frames per a second
        clock.tick(10)
Learned New

I struggled with collision detection since it didn’t work. I realized that it was working, but there is a problem with food position. My food_x,y variables were in the variables file, and it was not changed even though new_location() function was called.

I deleted the collision detection in snake class and added it to main function.

# main.py

if abs(snake.body[0][0] - food_x) < scale and abs(snake.body[0][1] - food_y) < scale:
    food.new_location()
    snake.grow()

I modified food class. I no longer need global food position variables.

# food_class.py

class Food:
    def __init__(self, food_x, food_y):
        self.x = food_x
        self.y = food_y

    def new_location(self):

        self.x = random.randrange(1, width - scale)
        self.y = random.randrange(1, height - scale)

    def show(self):
        pygame.draw.rect(display, food_color, (self.x, self.y, scale, scale))

Those work well, and need to fix score part. When snake eats food, we get +1 score, so I added score variable in food class.

class Food:
    def __init__(self, food_x, food_y, score):
        self.x = food_x
        self.y = food_y
        self.score = score
    def new_location(self):

        self.x = random.randrange(1, width - scale)
        self.y = random.randrange(1, height - scale)

    def show(self):
        pygame.draw.rect(display, food_color, (self.x, self.y, scale, scale))

So I can modify the collision detection like this

if abs(snake.body[0][0] - food.x) < scale and abs(snake.body[0][1] - food.y) < scale:
    food.new_location()
    snake.grow()
    food.score +=  1