-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake_Game.py
More file actions
142 lines (121 loc) · 4.21 KB
/
Snake_Game.py
File metadata and controls
142 lines (121 loc) · 4.21 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pygame
import random
pygame.init()
# Color specification
white = (255,255,255)
red = (255,0,0)
black = (0,0,0)
def plot_snake(gameWindow, color, snake_list):
snake_size = 30
for list in snake_list:
for x, y in list:
pygame.draw.rect(gameWindow, color, [x,y,snake_size,snake_size])
# define font module
font = pygame.font.SysFont(None, 60)
def text_screen(text, color, x , y):
screen_text = font.render(text,True, color)
gameWindow.blit(screen_text, [x,y])
# defining clock
clock = pygame.time.Clock()
# setting display size
display_width = 700
display_height = 600
gameWindow = pygame.display.set_mode((display_width,display_height))
# setting Title of window
pygame.display.set_caption("Snake Game")
def apple_position(snake_list):
apple_x = random.randint(15, display_width - 15)
apple_y = random.randint(15, display_height - 15)
for list in snake_list:
if abs(apple_x-list[0][0]) < 16 and abs(apple_y-list[0][1]) < 16:
apple_position(snake_list)
else:
return [apple_x,apple_y]
# catching every event on/over window
def game_loop():
# Game specific variables
game_exit = False
game_over = False
velocity_x = 0
velocity_y = 0
snake_x = random.randint(0,display_width)
snake_y = random.randint(0,display_height)
score = 0
speed = 5
apple_size = 30
snake_list = []
snake_length = 1
head = []
head.append([snake_x, snake_y])
snake_list.append(head)
apple = apple_position(snake_list)
fps = 60
while not game_exit:
# everything that needs to be updated is in this loop
for event in pygame.event.get():
# if event is an pressing of quit button
if event.type == pygame.QUIT:
game_exit = True
# if event is a pressing of any key
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if velocity_x != -speed:
velocity_x = speed
velocity_y = 0
elif event.key == pygame.K_LEFT:
if velocity_x != speed:
velocity_x = -speed
velocity_y = 0
elif event.key == pygame.K_DOWN:
if velocity_y != -speed:
velocity_y = speed
velocity_x = 0
elif event.key == pygame.K_UP:
if velocity_y != speed:
velocity_y = -speed
velocity_x = 0
# Filling background
gameWindow.fill(white)
head = []
snake_x += velocity_x
snake_y += velocity_y
head.append([snake_x, snake_y])
snake_list.append(head)
if len(snake_list) > snake_length:
del snake_list[0]
plot_snake(gameWindow, black, snake_list)
# if snake eats apple
if abs(apple[0] - snake_x) < 20 and abs(apple[1] - snake_y) <20:
score += 1
apple = apple_position(snake_list)
snake_length += speed
# drawing apple
pygame.draw.rect(gameWindow, red, [apple[0], apple[1], apple_size, apple_size])
# if snakes bites itself then Game Over
for list in snake_list[:-1]:
if head[0][0] == list[0][0] and head[0][1] == list[0][1]:
game_exit = True
# if head[0] in snake_list:
# game_exit = True
# text_screen("Game over", [0,255,0], 5, 5)
# keeping the snake in the screen
if snake_x < 0:
snake_x = display_width
elif snake_x >= display_width:
snake_x = 0
if snake_y < 0:
snake_y = display_height
elif snake_y >= display_height:
snake_y = 0
text_screen("Score: " + str(score), [0,255,0], 5, 5)
pygame.display.update()
clock.tick(fps)
# after the game over
text_x = display_width/2-90
text_y = display_height/2-40
text_screen("Game over", [255,10,10], text_x, text_y)
text_screen("Score: " + str(score), [255,10,10], text_x+25, text_y+50)
pygame.display.update()
game_loop()
pygame.quit()
quit()