-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
165 lines (96 loc) · 3.42 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pygame
import time
import random
# Initializing pygame
pygame.init()
# Define Colors
white= (255,255,255)
black= (0,0,0)
red = (255,0,0)
orange = (255,165,0)
width = 800
height = 600
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock()
snake_size = 10
snake_speed = 15
message_font = pygame.font.SysFont('ubuntu',30)
score_font = pygame.font.SysFont('ubuntu',25)
def print_score(score):
text = score_font.render("Score: "+str(score),True,orange)
screen.blit(text,[0,0])
def draw_snake(snake_size,snake_pixels):
for pixel in snake_pixels:
pygame.draw.rect(screen,white,[pixel[0],pixel[1],snake_size,snake_size])
def run_game():
game_over = False
game_close = False
x = width/2
y = height/2
x_speed = 0
y_speed = 0
snake_pixels = []
snake_length = 1
food_x = round(random.randrange(0,width-snake_size)/10.0)*10.0
food_y = round(random.randrange(0,height-snake_size)/10.0)*10.0
while not game_over:
while game_close:
screen.fill(black)
game_over_msg = message_font.render("Game Over !",True,red)
quit_msg=message_font.render("Press 1 to Quit",True,red)
restart_msg=message_font.render("Press 2 to Restart",True,red)
screen.blit(game_over_msg,[300,200])
screen.blit(quit_msg,[300,300])
screen.blit(restart_msg,[300,400])
print_score(snake_length-1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
game_over = True
game_close = False
if event.key ==pygame.K_2:
run_game()
if event.type == pygame.QUIT:
game_over = True
game_close = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed = -snake_size
y_speed = 0
if event.key == pygame.K_RIGHT:
x_speed = snake_size
y_speed = 0
if event.key == pygame.K_UP:
x_speed = 0
y_speed = -snake_size
if event.key == pygame.K_DOWN:
x_speed = 0
y_speed = snake_size
if x>= width or x<0 or y>=height or y<0:
game_close = True
x+=x_speed
y+=y_speed
screen.fill(black)
pygame.draw.rect(screen,orange,[food_x,food_y,snake_size,snake_size])
snake_pixels.append([x,y])
if len(snake_pixels)> snake_length:
del snake_pixels[0]
for pixel in snake_pixels[:-1]:
if pixel == [x,y]:
game_close = True
draw_snake(snake_size,snake_pixels)
print_score(snake_length - 1)
pygame.display.update()
if x == food_x and y==food_y:
food_x = round(random.randrange(0,width-snake_size)/10.0)*10.0
food_y = round(random.randrange(0,height-snake_size)/10.0)*10.0
snake_length+=1
clock.tick(snake_speed)
pygame.quit()
quit()
run_game()