import unicornhat as unicorn
  from random import randint
  import time, pygame
  
  SPEED = 5
  SCREEN_WIDTH = 600
  SCREEN_HEIGHT = 450
  BLACK =  (    0,     0,     0)
  WHITE = ( 255, 255, 255)
  GREEN = (     0, 255,     0)
  RED =     ( 255,     0,      0)
  BLUE = (     0,       0,  255)
  YELLOW =(255, 255,    0)
  PURPLE = (255,    0, 255)
  CYAN =   (     0, 255, 255)
  COLOURS = (GREEN, RED, BLUE, YELLOW, PURPLE, CYAN)
  
  class Snake():
      body_list = None 
      change_x = None 
      change_y = None 
      eaten = None 
      g_over = False
      def __init__(self):
          self.body_list = [[2,1],[2,2]] 
          self.change_x = 1
          self.change_y = 0
          self.eaten = False
      def update(self, food):
          old_segmant=self.body_list.pop()
          self.eaten = False
          x = self.body_list[0][0] + self.change_x
          y = self.body_list[0][1] + self.change_y
          segmant = [x,y]
          self.body_list.insert(0, segmant)
          if segmant[0] == food.x_pos and segmant[1] == food.y_pos:
              unicorn.set_pixel(old_segmant[0],old_segmant[1],food.r,food.g,food.b)
              self.body_list.append(old_segmant)
              self.eaten = True
              unicorn.show() 
          else:
              unicorn.set_pixel(old_segmant[0],old_segmant[1],BLACK[0],BLACK[1],
              BLACK[2])
          for segmant in self.body_list:
              try:
                  unicorn.set_pixel(segmant[0],segmant[1],WHITE[0],WHITE[1],WHITE[2])
              except:
                  self.g_over = True
      def go_left(self):
          self.change_x = 1
          self.change_y = 0
      def go_right(self):
          self.change_x = -1
          self.change_y = 0
      def go_up(self):
          self.change_x = 0
          self.change_y = 1
      def go_down(self):
          self.change_x = 0
          self.change_y = -1
      def game_over(self):
          if self.body_list[0] in self.body_list[1::] or self.g_over == True:
              return True
          return False
  
  class Food():
      eaten = None
      x_pos = None
      y_pos = None
      r = None
      g = None
      b = None
      def __init__(self):
          self.eaten = True 
  
      def update(self, snake):
          if self.eaten:
              inside = True
              while inside:
                  self.x_pos = randint(0,7)
                  self.y_pos = randint(0,7)
                  if [self.x_pos, self.y_pos] in snake.body_list:
                      inside = True
                  else:
                      inside = False
              colour = randint(0,len(COLOURS)-1)
              self.r = COLOURS[colour][0]
              self.g = COLOURS[colour][1]
              self.b = COLOURS[colour][2]
          unicorn.set_pixel(self.x_pos,self.y_pos,self.r,self.g,self.b)
          self.eaten = False
  
  class Game(object):
      snake = None
      food = None
      game_over = None
      start = True 
      def __init__(self):
          self.snake = Snake()
          self.food = Food()
          
      def process_events(self):
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  return True 
              if event.type == pygame.KEYDOWN and (self.game_over or self.start):
                  self.__init__()
                  self.start = False
              if event.type == pygame.KEYDOWN:
                  if event.key == pygame.K_LEFT and self.snake.change_x != -1:
                      self.snake.go_left()
                  if event.key == pygame.K_RIGHT and self.snake.change_x != 1:
                      self.snake.go_right()
                  if event.key == pygame.K_UP and self.snake.change_y != -1:
                      self.snake.go_up()
                  if event.key == pygame.K_DOWN and self.snake.change_y != 1:
                      self.snake.go_down()
          return False
  
      def run_logic(self):
          self.game_over = self.snake.game_over()
          if not self.game_over and not self.start:
              self.food.update(self.snake)
              self.snake.update(self.food)
              self.food.eaten = self.snake.eaten
  
      def display_frame(self, screen):
          if self.game_over or self.start:
              screen.fill(WHITE)
              font = pygame.font.SysFont('serif', 25)
              title = font.render('Unicorn Snake.', True, BLACK)
              text = font.render('Press any key to play.', True, BLACK)
              instructions = font.render('Use the arrow keys to control your 
              snake on the unicorn hat', True, BLACK)
              title_center_x = (SCREEN_WIDTH // 2) - (title.get_width() // 2)
              text_center_x = (SCREEN_WIDTH // 2) - (text.get_width() // 2)
              instructions_center_x = (SCREEN_WIDTH // 2) - (instructions.get_width(
              ) // 2)
              top_y = title.get_height() * 2
              center_y = (SCREEN_HEIGHT // 2) - (text.get_height() // 2)
              bottom_y = SCREEN_HEIGHT - (instructions.get_height() * 2)
              screen.blit( title, [title_center_x, top_y] )
              screen.blit( text, [text_center_x, center_y] )
              screen.blit( instructions, [instructions_center_x, bottom_y] )
              unicorn.clear()
          else:
              screen.fill(BLACK)
              font = pygame.font.SysFont('serif', 25)
              title = font.render('Unicorn Snake.', True, WHITE)
              text = font.render('Game on', True, WHITE)
              title_center_x = (SCREEN_WIDTH // 2) - (title.get_width() // 2)
              text_center_x = (SCREEN_WIDTH // 2) - (text.get_width() // 2)
              top_y = title.get_height() * 2
              center_y = (SCREEN_HEIGHT // 2) - (text.get_height() // 2)
              screen.blit( title, [title_center_x, top_y] )
              screen.blit( text, [text_center_x, center_y] )
              unicorn.show() 
  
          pygame.display.flip()
  
  def main():
      pygame.init()
  
      size = (SCREEN_WIDTH, SCREEN_HEIGHT)
      screen = pygame.display.set_mode(size)
  
      pygame.display.set_caption('Unicorn Snake')
  
      done = False
      clock = pygame.time.Clock()
  
      game = Game()
      
      while not done:
          done = game.process_events()
  
          game.run_logic()
  
          game.display_frame(screen)
  
          clock.tick(SPEED)
  
      pygame.quit()
  
  if __name__ == '__main__':
      main()