import pygame
import random
import time
from Apple import Apple
from Snake import Snake
from direction import Direction

pygame.init()

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 608

background = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))

for i in range(25):
    for j in range(19):
        Image = pygame.image.load("images/background.png")
        mask = (random.randrange(0,20), random.randrange(0,20), random.randrange(0,20))

        Image.fill(mask, special_flags=pygame.BLEND_ADD)
        background.blit(Image,(i*32, j*32))

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()

apple = Apple()
snake  = Snake()
MOVE_SNAKE = pygame.USEREVENT + 1
pygame.time.set_timer(MOVE_SNAKE,500)


game_works = True
while game_works:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                game_works = False
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                snake.change_direction(Direction.UP)
            if event.key == pygame.K_d:
                snake.change_direction(Direction.RIGHT)
            if event.key == pygame.K_s:
                snake.change_direction(Direction.DOWN)
            if event.key == pygame.K_a:
                snake.change_direction(Direction.LEFT)
        
        if event.type == MOVE_SNAKE:
            snake.UPdate()

    screen.blit(background,(0,0))
    screen.blit(apple.Image, apple.rect)
    screen.blit(snake.Image, snake.rect)
    pygame.display.flip()
    clock.tick(30)

pygame.quit()