-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
65 lines (51 loc) · 1.69 KB
/
snake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN, pygame.KEYUP])
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
comicsans = pygame.font.SysFont('comicsans', 50)
snake = [(400,400)]
v = (0,0)
size = 40
eat = True
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if eat:
while True:
food = (random.randrange(800//size)*size, random.randrange(800//size)*size)
if food not in snake:
break
eat = False
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] or pressed[pygame.K_w]:
v=(0,-size)
elif pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
v=(0,size)
elif pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
v=(-size,0)
elif pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
v=(size,0)
snake.append((snake[-1][0]+v[0], snake[-1][1]+v[1]))
if not (0<=snake[-1][0]<=800 and 0<=snake[-1][1]<=800) or snake[-1] in snake[:-2]:
done = True
if snake[-1]==food:
eat = True
elif v:
snake.pop(0)
screen.fill(black)
pygame.draw.rect(screen, red, pygame.Rect(food[0]+size//4,food[1]+size//4,size//2,size//2))
for x,y in snake:
pygame.draw.rect(screen, white, pygame.Rect(x,y,size,size))
text = comicsans.render(str(len(snake)), True, white)
textbox = text.get_rect()
textbox.center = (400,100)
screen.blit(text, textbox)
clock.tick(10)
pygame.display.flip()