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

death function

Everything works except death function. When snake collides with wall or its body, game should be over. This code is for checking collision with wall. I wrote code like this for test, may need to modify later.

if snake.body[0][0] < -10 or snake.body[0][1] < -10:
    print("dead")
    pygame.time.delay(2000)
if snake.body[0][0] > screen_width or snake.body[0][1] > screen_height:
    print("dead")
    pygame.time.delay(2000)

And this code is for checking collision with its body.

def death(self):
        if self.length > 2:
            # if head collided to body, dead.
            for i in range(1, self.length - 1):
                if abs(self.body[0][0] - self.body[i][0]) < 1 and abs(self.body[0][1] - self.body[i][1]) < 1:
                    return True
            return False

game over function

When snake dies, I just print “dead” and delay 2 seconds. Now I need to create game over scene.

def game_over():
    font = pygame.font.SysFont("Copperplate Gothic Bold", 50)
    text = font.render("Game Over", True, (255, 255, 255))
    text_width, text_height = text.get_size()
    display.blit(text, (screen_width / 2 - (text_width / 2), screen_height / 2 - (text_height / 2)))
    # update the contents of the entire display
    pygame.display.flip()
    pygame.time.delay(3000)
    pygame.quit()
    sys.exit()
Learned New

I realized that snake shouldn’t be turned 180 degrees so I need to modify keydown functions

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:
            if not(snake.x_dir == 0 and snake.y_dir == 1):
                snake.x_dir = 0
                snake.y_dir = -1

        if event.key == pygame.K_DOWN:
            if not(snake.x_dir == 0 and snake.y_dir == -1):
                snake.x_dir = 0
                snake.y_dir = 1

        if event.key == pygame.K_LEFT:
            if not(snake.x_dir == 1 and snake.y_dir == 0):
                snake.x_dir = -1
                snake.y_dir = 0

        if event.key == pygame.K_RIGHT:
            if not(snake.x_dir == -1 and snake.y_dir == 0):
                snake.x_dir = 1
                snake.y_dir = 0