Friday April 24, 2026

This Free Friday I am working on a project on YouTube a Snake Game part 3.

def check_fail(self):
        if not 0 <= self.snake.body[0].x < cell_number or not 0 <= self.snake.body[0].y < cell_number:
            self.game_over()

        for block in self.snake.body[1:]:
            if block == self.snake.body[0]:
                self.game_over()
  • This is the basic check fail logic.
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            if event.type == SCREEN_UPDATE:
                main_game.update()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                     if main_game.snake.direction.y != 1:
                      main_game.direction = Vector2(0,-1)
                if event.key == pygame.K_RIGHT:
                    if main_game.snake.direction.x != -1:
                     main_game.snake.direction = Vector2(1,0)
                if event.key == pygame.K_DOWN:
                    if main_game.snake.direction.y != -1:
                      main_game.snake.direction = Vector2(0,1)
                if event.key == pygame.K_LEFT:
                    if main_game.snake.direction.x != 1:
                     main_game.snake.direction = Vector2(-1,0)
  • This part of the code is a more updated for the snake to move up, down and right, to left.
apple = pygame.image.load('Graphics/apple.png').convert_alpha()
  • This part of the code is the graphics for the apple in the snake.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top