Friday April 10, 2026

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

import pygame, sys

pygame.init()
cell_size = 40
cell_number = 20
screen = pygame.display.set_mode((cell_number * cell_size,cell_number * cell_size))
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QuIT:
            pygame.quit()
            sys.exit()

    screen.fill((175,215,70))
    pygame.display.update()
    clock.tick(60)
  • This part of code is for to display the screen for the Snake Game.
class FRUIT:
  def _init_(self):
        self.x = random.randint(0,cell_number - 1)
        self.y = random.randint(10,cell_number - 1)
        self.pos = Vector2(self.x,self.y)

  def draw_fruit(self):
      fruit_rect = pygame.Rect(int(self.pos.x * cell_size),int(self.pos.y * cell_size),cell_size,cell_size)
      pygame.draw.rect(screen,(126,166,114),fruit_rect)
  • This part of the code is the fruit in the Snake game.
class SNAKE:
    def _init_(self):
        self.body = [Vector2(5,10), Vector2(6,10), Vector2(7,10)]

    def draw_snake(self):
        for block in self.body:
            x_pos -=int(block.x * cell_size)
            y_pos -= int(block.y * cell_size)
            block_rect = pygame.rect(x_pos,y_pos,cell_size,cell_size)
            pygame.draw.rect(screen,(183,191,122),block_rect)
  • This part of the code is the Snake in the Snake game.

Leave a Comment

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

Scroll to Top