import pygame
import random
BG_COLOR = (255, 255, 255)
SNAKE_COLOR = (0, 117, 128)
CANDY_COLOR = (254, 208, 73)
SQUARE_SIZE = 30
HEIGHT = 600
WIDTH = 1000
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
vx = SQUARE_SIZE
vy = 0
x = WIDTH // 2
y = HEIGHT // 2
my_font = pygame.font.SysFont("Consolas", 30)
you_lost = my_font.render("You lost", True, (255, 0, 0))
def create_candy():
candy_x = random.randint(70, WIDTH - 70)
candy_y = random.randint(70, HEIGHT - 70)
return candy_x, candy_y
candy_x, candy_y = create_candy()
def change_direction(vx, vy, key):
if key == pygame.K_LEFT:
if vx == SQUARE_SIZE:
vx = 0
vy = -SQUARE_SIZE
elif vx == -SQUARE_SIZE:
vx = 0
vy = SQUARE_SIZE
elif vy == SQUARE_SIZE:
vy = 0
vx = SQUARE_SIZE
else:
vy = 0
vx = -SQUARE_SIZE
if key == pygame.K_RIGHT:
if vx == SQUARE_SIZE:
vx = 0
vy = SQUARE_SIZE
elif vx == -SQUARE_SIZE:
vx = 0
vy = -SQUARE_SIZE
elif vy == SQUARE_SIZE:
vy = 0
vx = -SQUARE_SIZE
else:
vy = 0
vx = SQUARE_SIZE
return vx, vy
game_over = False
snake = [(x, y)]
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
exit()
if e.type == pygame.KEYDOWN:
vx, vy = change_direction(vx, vy, e.key)
screen.fill(BG_COLOR)
if not game_over:
for x, y in snake:
pygame.draw.rect(screen, SNAKE_COLOR, (x, y, SQUARE_SIZE - 1, SQUARE_SIZE - 1))
pygame.draw.circle(screen, CANDY_COLOR, (candy_x + SQUARE_SIZE // 2, candy_y + SQUARE_SIZE // 2),
SQUARE_SIZE // 2)
snake.insert(0, (snake[0][0] + vx, snake[0][1] + vy))
x, y = snake[0]
if x < 0 or y < 0 or x + SQUARE_SIZE > WIDTH or y + SQUARE_SIZE > HEIGHT:import random
BG_COLOR=(255,255,255)
SNAKE_COLOR=(0,117,128)
CANDY_COLOR=(254,208,73)
SQUARE_SIZE=30
HEIGHT=600
WIDTH=1000
pygame.init()
screen=pygame.display.set_mode((WIDTH,HEIGHT))
vx=SQUARE_SIZE
vy=0
x=WIDTH//2
y=HEIGHT//2
my_font=pygame.font.SysFont("Consolas",30)
you_lost=my_font.render("You lost",True,(255,0,0))
def create_candy():
candy_x=random.randint(70,WIDTH-70)
candy_y=random.randint(70,HEIGHT-70)
return candy_x, candy_y
candy_x,candy_y=create_candy()
def change_direction(vx,vy,key):
if key==pygame.K_LEFT:
if vx==SQUARE_SIZE:
vx=0
vy=-SQUARE_SIZE
elif vx== -SQUARE_SIZE:
vx=0
vy=SQUARE_SIZE
elif vy==SQUARE_SIZE:
vy=0
vx=SQUARE_SIZE
else:
vy=0
vx=-SQUARE_SIZE
if key==pygame.K_RIGHT:
if vx==SQUARE_SIZE:
vx=0
vy=SQUARE_SIZE
elif vx==-SQUARE_SIZE:
vx=0
vy=-SQUARE_SIZE
elif vy==SQUARE_SIZE:
vy=0
vx=-SQUARE_SIZE
else:
vy=0
vx=SQUARE_SIZE
return vx,vy
game_over=False
snake=[(x,y)]
while True:
for e in pygame.event.get():
if e.type==pygame.QUIT:
exit()
if e.type==pygame.KEYDOWN:
vx,vy=change_direction(vx,vy,e.key)
screen.fill(BG_COLOR)
if not game_over:
for x,y in snake:
pygame.draw.rect(screen,SNAKE_COLOR,(x,y,SQUARE_SIZE-1,SQUARE_SIZE-1))
pygame.draw.circle(screen, CANDY_COLOR,(candy_x+SQUARE_SIZE//2,candy_y+SQUARE_SIZE//2),SQUARE_SIZE//2)
snake.insert(0,(snake[0][0]+vx,snake[0][1]+vy))
x,y=snake[0]
if x<0 or y<0 or x + SQUARE_SIZE>WIDTH or y+SQUARE_SIZE>HEIGHT:
game_over=True
head_rect=pygame.Rect(x,y,SQUARE_SIZE,SQUARE_SIZE)
candy_rect=pygame.Rect(candy_x,candy_y,SQUARE_SIZE,SQUARE_SIZE)
if head_rect.colliderect(candy_rect):
candy_x,candy_y=create_candy()
else:
snake.pop()
for x1,y1 in snake[1:]:
snake_rect=pygame.Rect(x1,y1,SQUARE_SIZE,SQUARE_SIZE)
if snake_rect.colliderect(head_rect):
game_over=True
else:
screen.blit(you_lost,(WIDTH//2-100,HEIGHT//2-30))
pygame.display.flip()
pygame.display.update ()
pygame.time.delay(250)
True
head_rect = pygame.Rect(x, y, SQUARE_SIZE, SQUARE_SIZE)
candy_rect = pygame.Rect(candy_x, candy_y, SQUARE_SIZE, SQUARE_SIZE)
if head_rect.colliderect(candy_rect):
candy_x, candy_y = create_candy()
else:
snake.pop()
for x1, y1 in snake[1:]:
snake_rect = pygame.Rect(x1, y1, SQUARE_SIZE, SQUARE_SIZE)
if snake_rect.colliderect(head_rect):
game_over = True
else:
screen.blit(you_lost, (WIDTH // 2 - 100, HEIGHT // 2 - 30))
pygame.display.flip()
pygame.display.update()
pygame.time.delay(250)