-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
113 lines (90 loc) · 2.79 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pygame
import time
import random
pygame.init()
GRAY = (128,128,128)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
WIDTH = 600
HEIGHT = 400
surface = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Snake')
done = False
snake_x = WIDTH / 2
snake_y = HEIGHT / 2
delta_x = 0
delta_y = 0
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
snake_length = 1
snake_list = []
def game_over():
font = pygame.font.SysFont('monospace', 30)
score = font.render(f"Score: {snake_length}", True, (0,0,0))
score_rect = score.get_rect(center=(WIDTH / 2, HEIGHT / 2))
surface.blit(score, score_rect)
pygame.display.flip()
while True:
event = pygame.event.wait()
if event.type == QUIT:
pygame.quit()
sys.exit()
def show_score():
font = pygame.font.SysFont('monospace',20)
score = font.render(f"Score: {snake_length}",True,(0,0,0))
surface.blit(score,(0,0))
def draw_snake(snake_body):
for x in snake_body:
pygame.draw.rect(surface, GRAY, ([x[0], x[1], 10, 10]))
def draw_fruit():
food_x = random.randrange(20, WIDTH - 20)
food_y = random.randrange(20, HEIGHT - 20)
pygame.draw.rect(WIN ,RED, pygame.Rect(food_x,food_y,10,10))
food_x = random.randrange(20, WIDTH - 20)
food_y = random.randrange(20, HEIGHT - 20)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
delta_x = -10
delta_y = 0
elif event.key == pygame.K_RIGHT:
delta_x = 10
delta_y = 0
elif event.key == pygame.K_UP:
delta_y = -10
delta_x = 0
elif event.key == pygame.K_DOWN:
delta_y = 10
delta_x = 0
if snake_x >= WIDTH or snake_x < 0 or snake_y >= HEIGHT or snake_y < 0:
done = True
game_over()
snake_x += delta_x
snake_y += delta_y
surface.fill(WHITE)
pygame.draw.rect(surface, RED, pygame.Rect(food_x, food_y, snake_block, snake_block))
snake_head = []
snake_head.append(snake_x)
snake_head.append(snake_y)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
for x in snake_list[:-1]:
if x == snake_head:
done = True
game_over()
draw_snake(snake_list)
if pygame.Rect(snake_x,snake_y,10,10).colliderect(pygame.Rect(food_x,food_y,10,10)):
food_x = random.randrange(20, WIDTH - 20, 10)
food_y = random.randrange(20, HEIGHT - 20, 10)
snake_length += 1
show_score()
pygame.display.update()
clock.tick(snake_speed)
pygame.quit()
quit()