import pygame,sys, random, time from pygame.math import Vector2 class SNAKE: def __init__(self): self.body = [Vector2(5,10), Vector2(4,10), Vector2(3,10)] self.direction = Vector2(1,0) self.new_block = False self.head_up = pygame.image.load("images\snake_head_up.png").convert_alpha() self.head_up = pygame.transform.scale(self.head_up, (40, 40)) self.head_down = pygame.image.load("images\snake_head_down.png").convert_alpha() self.head_down = pygame.transform.scale(self.head_down, (40, 40)) self.head_left = pygame.image.load("images\snake_head_left.png").convert_alpha() self.head_left = pygame.transform.scale(self.head_left, (40, 40)) self.head_right = pygame.image.load("images\snake_head_right.png").convert_alpha() self.head_right = pygame.transform.scale(self.head_right, (40, 40)) self.tail = pygame.image.load("images\snake_tail.png").convert_alpha() self.tail = pygame.transform.scale(self.tail, (40, 40)) self.body_image = pygame.image.load("images\snake_body.png").convert_alpha() self.body_image = pygame.transform.scale(self.body_image, (40, 40)) def draw_snake(self): self.update_head_graphics() # Draw tail tail_rect = pygame.Rect(int(self.body[-1].x * cell_size), int(self.body[-1].y * cell_size), cell_size, cell_size) screen.blit(self.tail, tail_rect) # Draw head head_rect = pygame.Rect(int(self.body[0].x * cell_size), int(self.body[0].y * cell_size), cell_size, cell_size) screen.blit(self.head, head_rect) # Draw body for block in self.body[1:-1]: 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) screen.blit(self.body_image, block_rect) def update_head_graphics(self): if self.direction == Vector2(1, 0): self.head = self.head_right elif self.direction == Vector2(-1, 0): self.head = self.head_left elif self.direction == Vector2(0, 1): self.head = self.head_down elif self.direction == Vector2(0, -1): self.head = self.head_up def move_snake(self): if self.new_block == True: body_copy = self.body[:] body_copy.insert(0,body_copy[0] + self.direction) self.body = body_copy[:] self.new_block = False else: body_copy = self.body[:-1] body_copy.insert(0,body_copy[0] + self.direction) self.body = body_copy[:] def add_block(self): self.new_block = True class FRUIT: def __init__(self): self.randomize() def draw_fruit(self): fruit_rect = pygame.Rect(int(self.pos.x * cell_size),int(self.pos.y * cell_size),cell_size,cell_size) screen.blit(orange,fruit_rect) def randomize(self): self.x = random.randint(0,cell_number -1) self.y = random.randint(0,cell_number -1) self.pos = Vector2(self.x,self.y) class MAIN: def __init__(self): self.snake = SNAKE() self.fruit = FRUIT() def update(self): self.snake.move_snake() self.check_collision() self.check_fail() def draw_elements(self): self.draw_grass() self.fruit.draw_fruit() self.snake.draw_snake() self.draw_score() def check_collision(self): if self.fruit.pos == self.snake.body[0]: self.fruit.randomize() self.snake.add_block() for block in self.snake.body[1:]: if block == self.fruit.pos: self.fruit.randomize() 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() def game_over(self): time.sleep(0.7) pygame.quit() sys.exit() def draw_grass(self): grass_color = "#03C04A" for row in range(cell_number): if row % 2 == 0: for col in range(cell_number): if col % 2 == 0: grass_rect = pygame.Rect(col * cell_size,row*cell_size,cell_size,cell_size) pygame.draw.rect(screen,grass_color,grass_rect) else: for col in range(cell_number): if col % 2 != 0: grass_rect = pygame.Rect(col * cell_size,row*cell_size,cell_size,cell_size) pygame.draw.rect(screen,grass_color,grass_rect) def draw_score(self): score_text = str(len(self.snake.body) - 3) score_surface = game_font.render(score_text, True, "#000000") score_x = int(cell_size * cell_number - 60) score_y = int(cell_size * cell_number - 40) score_rect = score_surface.get_rect(center = (score_x, score_y)) orange_rect = orange.get_rect(midright = (score_rect.left, score_rect.centery)) bg_rect = pygame.Rect(orange_rect.left, orange_rect.top, orange_rect.width + score_rect.width + 5, orange_rect.height + 2) pygame.draw.rect(screen, pygame.Color("white"), bg_rect) screen.blit(score_surface, score_rect) screen.blit(orange, orange_rect) pygame.draw.rect(screen, pygame.Color("black"), bg_rect, 3) 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() orange = pygame.image.load("images\orange.png").convert_alpha() orange = pygame.transform.scale(orange, (40, 40)) game_font = pygame.font.Font("#44v2.ttf", 25) main_game = MAIN() SCREEN_UPDATE = pygame.USEREVENT pygame.time.set_timer(SCREEN_UPDATE,150) 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.snake.direction = Vector2(0,-1) 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) if event.key == pygame.K_RIGHT: if main_game.snake.direction.x != -1: main_game.snake.direction = Vector2(1,0) screen.fill("#03AC13") main_game.draw_elements() pygame.display.update() clock.tick(60)