import pygame
from pygame.locals import *
import random

WINDOW_SIZE = (600, 600)
PIXEL_SIZE = 10

# Collision
def collision(pos1, pos2):
    return pos1 == pos2

def off_limits(pos):
    if 0 <= pos[0] < WINDOW_SIZE[0] and 0 <= pos[1] < WINDOW_SIZE[1]:
        return False
    else:
        return True

# Position aleatory
def random_on_grid():
    x = random.randint(0, WINDOW_SIZE[0])
    y = random.randint(0, WINDOW_SIZE[1])
    return x // PIXEL_SIZE * PIXEL_SIZE, y // PIXEL_SIZE * PIXEL_SIZE

pygame.init()

# Created the window the game
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('Snake')

# Snake, created the snake with position, direction and blue collor
snake_pos = [(250, 50), (260, 50), (270, 50)]
snake_surface = pygame.Surface((PIXEL_SIZE, PIXEL_SIZE))
snake_surface.fill((0, 0, 255))
snake_direction = K_LEFT

# Apple, created the apple with position and red collor
apple_surface = pygame.Surface((PIXEL_SIZE, PIXEL_SIZE))
apple_surface.fill((255, 0, 0))
apple_pos = random_on_grid()

def restart_game():
    global snake_pos
    global apple_pos
    global snake_direction
    snake_pos = [(250, 50), (260, 50), (270, 50)]
    snake_direction = K_LEFT
    apple_pos = random_on_grid()

# Punctuation the game
pontos = 0
font = pygame.font.Font('freesansbold.ttf', 36)
text = font.render('Pontuação: ' + str(pontos), True, (255, 255, 255), (0, 0, 0))
textRect = text.get_rect()
textRect.center = (330, 20)

while True:
    pygame.time.Clock().tick(15)
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            quit()
        elif event.type == KEYDOWN:
            if event.key in [K_UP, K_DOWN, K_LEFT, K_RIGHT]:
                snake_direction = event.key

    screen.blit(apple_surface, apple_pos)

    screen.blit(text, textRect)

    if collision(apple_pos, snake_pos[0]):
        snake_pos.append((-10, -10))
        apple_pos = random_on_grid()
        pontos = pontos + 1
        text = font.render('Pontuação: ' + str(pontos), True, (255, 255, 255), (0, 0, 0))

    for pos in snake_pos:
        screen.blit(snake_surface, pos)

    for i in range(len(snake_pos) - 1, 0, -1):
        if collision(snake_pos[0], snake_pos[i]):
            restart_game()
            break
        snake_pos[i] = snake_pos[i - 1]

    if off_limits(snake_pos[0]):
        restart_game()

    if snake_direction == K_UP:
        snake_pos[0] = (snake_pos[0][0], snake_pos[0][1] - PIXEL_SIZE)
    elif snake_direction == K_DOWN:
        snake_pos[0] = (snake_pos[0][0], snake_pos[0][1] + PIXEL_SIZE)
    elif snake_direction == K_LEFT:
        snake_pos[0] = (snake_pos[0][0] - PIXEL_SIZE, snake_pos[0][1])
    elif snake_direction == K_RIGHT:
        snake_pos[0] = (snake_pos[0][0] + PIXEL_SIZE, snake_pos[0][1])

    pygame.display.update()
664b2722798f7e0f4543de993548cc5430cbd441
<mask token>


def restart_game():
    global snake_pos
    global apple_pos
    global snake_direction
    snake_pos = [(250, 50), (260, 50), (270, 50)]
    snake_direction = K_LEFT
    apple_pos = random_on_grid()


<mask token>

<mask token>


def collision(pos1, pos2):
    return pos1 == pos2


def off_limits(pos):
    if 0 <= pos[0] < WINDOW_SIZE[0] and 0 <= pos[1] < WINDOW_SIZE[1]:
        return False
    else:
        return True


def random_on_grid():
    x = random.randint(0, WINDOW_SIZE[0])
    y = random.randint(0, WINDOW_SIZE[1])
    return x // PIXEL_SIZE * PIXEL_SIZE, y // PIXEL_SIZE * PIXEL_SIZE


<mask token>


def restart_game():
    global snake_pos
    global apple_pos
    global snake_direction
    snake_pos = [(250, 50), (260, 50), (270, 50)]
    snake_direction = K_LEFT
    apple_pos = random_on_grid()


<mask token>

<mask token>


def collision(pos1, pos2):
    return pos1 == pos2


def off_limits(pos):
    if 0 <= pos[0] < WINDOW_SIZE[0] and 0 <= pos[1] < WINDOW_SIZE[1]:
        return False
    else:
        return True


def random_on_grid():
    x = random.randint(0, WINDOW_SIZE[0])
    y = random.randint(0, WINDOW_SIZE[1])
    return x // PIXEL_SIZE * PIXEL_SIZE, y // PIXEL_SIZE * PIXEL_SIZE


pygame.init()
<mask token>
pygame.display.set_caption('Snake')
<mask token>
snake_surface.fill((0, 0, 255))
<mask token>
apple_surface.fill((255, 0, 0))
<mask token>


def restart_game():
    global snake_pos
    global apple_pos
    global snake_direction
    snake_pos = [(250, 50), (260, 50), (270, 50)]
    snake_direction = K_LEFT
    apple_pos = random_on_grid()


<mask token>
while True:
    pygame.time.Clock().tick(15)
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            quit()
        elif event.type == KEYDOWN:
            if event.key in [K_UP, K_DOWN, K_LEFT, K_RIGHT]:
                snake_direction = event.key
    screen.blit(apple_surface, apple_pos)
    screen.blit(text, textRect)
    if collision(apple_pos, snake_pos[0]):
        snake_pos.append((-10, -10))
        apple_pos = random_on_grid()
        pontos = pontos + 1
        text = font.render('Pontuação: ' + str(pontos), True, (255, 255, 
            255), (0, 0, 0))
    for pos in snake_pos:
        screen.blit(snake_surface, pos)
    for i in range(len(snake_pos) - 1, 0, -1):
        if collision(snake_pos[0], snake_pos[i]):
            restart_game()
            break
        snake_pos[i] = snake_pos[i - 1]
    if off_limits(snake_pos[0]):
        restart_game()
    if snake_direction == K_UP:
        snake_pos[0] = snake_pos[0][0], snake_pos[0][1] - PIXEL_SIZE
    elif snake_direction == K_DOWN:
        snake_pos[0] = snake_pos[0][0], snake_pos[0][1] + PIXEL_SIZE
    elif snake_direction == K_LEFT:
        snake_pos[0] = snake_pos[0][0] - PIXEL_SIZE, snake_pos[0][1]
    elif snake_direction == K_RIGHT:
        snake_pos[0] = snake_pos[0][0] + PIXEL_SIZE, snake_pos[0][1]
    pygame.display.update()

import pygame
from pygame.locals import *
import random
WINDOW_SIZE = 600, 600
PIXEL_SIZE = 10


def collision(pos1, pos2):
    return pos1 == pos2


def off_limits(pos):
    if 0 <= pos[0] < WINDOW_SIZE[0] and 0 <= pos[1] < WINDOW_SIZE[1]:
        return False
    else:
        return True


def random_on_grid():
    x = random.randint(0, WINDOW_SIZE[0])
    y = random.randint(0, WINDOW_SIZE[1])
    return x // PIXEL_SIZE * PIXEL_SIZE, y // PIXEL_SIZE * PIXEL_SIZE


pygame.init()
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('Snake')
snake_pos = [(250, 50), (260, 50), (270, 50)]
snake_surface = pygame.Surface((PIXEL_SIZE, PIXEL_SIZE))
snake_surface.fill((0, 0, 255))
snake_direction = K_LEFT
apple_surface = pygame.Surface((PIXEL_SIZE, PIXEL_SIZE))
apple_surface.fill((255, 0, 0))
apple_pos = random_on_grid()


def restart_game():
    global snake_pos
    global apple_pos
    global snake_direction
    snake_pos = [(250, 50), (260, 50), (270, 50)]
    snake_direction = K_LEFT
    apple_pos = random_on_grid()


pontos = 0
font = pygame.font.Font('freesansbold.ttf', 36)
text = font.render('Pontuação: ' + str(pontos), True, (255, 255, 255), (0, 
    0, 0))
textRect = text.get_rect()
textRect.center = 330, 20
while True:
    pygame.time.Clock().tick(15)
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            quit()
        elif event.type == KEYDOWN:
            if event.key in [K_UP, K_DOWN, K_LEFT, K_RIGHT]:
                snake_direction = event.key
    screen.blit(apple_surface, apple_pos)
    screen.blit(text, textRect)
    if collision(apple_pos, snake_pos[0]):
        snake_pos.append((-10, -10))
        apple_pos = random_on_grid()
        pontos = pontos + 1
        text = font.render('Pontuação: ' + str(pontos), True, (255, 255, 
            255), (0, 0, 0))
    for pos in snake_pos:
        screen.blit(snake_surface, pos)
    for i in range(len(snake_pos) - 1, 0, -1):
        if collision(snake_pos[0], snake_pos[i]):
            restart_game()
            break
        snake_pos[i] = snake_pos[i - 1]
    if off_limits(snake_pos[0]):
        restart_game()
    if snake_direction == K_UP:
        snake_pos[0] = snake_pos[0][0], snake_pos[0][1] - PIXEL_SIZE
    elif snake_direction == K_DOWN:
        snake_pos[0] = snake_pos[0][0], snake_pos[0][1] + PIXEL_SIZE
    elif snake_direction == K_LEFT:
        snake_pos[0] = snake_pos[0][0] - PIXEL_SIZE, snake_pos[0][1]
    elif snake_direction == K_RIGHT:
        snake_pos[0] = snake_pos[0][0] + PIXEL_SIZE, snake_pos[0][1]
    pygame.display.update()

import pygame
from pygame.locals import *
import random

WINDOW_SIZE = (600, 600)
PIXEL_SIZE = 10

# Collision
def collision(pos1, pos2):
    return pos1 == pos2

def off_limits(pos):
    if 0 <= pos[0] < WINDOW_SIZE[0] and 0 <= pos[1] < WINDOW_SIZE[1]:
        return False
    else:
        return True

# Position aleatory
def random_on_grid():
    x = random.randint(0, WINDOW_SIZE[0])
    y = random.randint(0, WINDOW_SIZE[1])
    return x // PIXEL_SIZE * PIXEL_SIZE, y // PIXEL_SIZE * PIXEL_SIZE

pygame.init()

# Created the window the game
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('Snake')

# Snake, created the snake with position, direction and blue collor
snake_pos = [(250, 50), (260, 50), (270, 50)]
snake_surface = pygame.Surface((PIXEL_SIZE, PIXEL_SIZE))
snake_surface.fill((0, 0, 255))
snake_direction = K_LEFT

# Apple, created the apple with position and red collor
apple_surface = pygame.Surface((PIXEL_SIZE, PIXEL_SIZE))
apple_surface.fill((255, 0, 0))
apple_pos = random_on_grid()

def restart_game():
    global snake_pos
    global apple_pos
    global snake_direction
    snake_pos = [(250, 50), (260, 50), (270, 50)]
    snake_direction = K_LEFT
    apple_pos = random_on_grid()

# Punctuation the game
pontos = 0
font = pygame.font.Font('freesansbold.ttf', 36)
text = font.render('Pontuação: ' + str(pontos), True, (255, 255, 255), (0, 0, 0))
textRect = text.get_rect()
textRect.center = (330, 20)

while True:
    pygame.time.Clock().tick(15)
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            quit()
        elif event.type == KEYDOWN:
            if event.key in [K_UP, K_DOWN, K_LEFT, K_RIGHT]:
                snake_direction = event.key

    screen.blit(apple_surface, apple_pos)

    screen.blit(text, textRect)

    if collision(apple_pos, snake_pos[0]):
        snake_pos.append((-10, -10))
        apple_pos = random_on_grid()
        pontos = pontos + 1
        text = font.render('Pontuação: ' + str(pontos), True, (255, 255, 255), (0, 0, 0))

    for pos in snake_pos:
        screen.blit(snake_surface, pos)

    for i in range(len(snake_pos) - 1, 0, -1):
        if collision(snake_pos[0], snake_pos[i]):
            restart_game()
            break
        snake_pos[i] = snake_pos[i - 1]

    if off_limits(snake_pos[0]):
        restart_game()

    if snake_direction == K_UP:
        snake_pos[0] = (snake_pos[0][0], snake_pos[0][1] - PIXEL_SIZE)
    elif snake_direction == K_DOWN:
        snake_pos[0] = (snake_pos[0][0], snake_pos[0][1] + PIXEL_SIZE)
    elif snake_direction == K_LEFT:
        snake_pos[0] = (snake_pos[0][0] - PIXEL_SIZE, snake_pos[0][1])
    elif snake_direction == K_RIGHT:
        snake_pos[0] = (snake_pos[0][0] + PIXEL_SIZE, snake_pos[0][1])

    pygame.display.update()