-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
236 lines (132 loc) · 4.46 KB
/
game.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from math import trunc
import pygame
import time
import random
from pygame import key
from pygame.constants import K_LEFT, K_RIGHT
snake_speed = 15
# STORING WINDOW SIZE IN IN VAR
window_x = 720
window_y = 480
# storing colours in variables
black = pygame.Color(0,0,0)
white = pygame.Color(255,255,255)
red = pygame.Color(255,0,0)
green = pygame.Color(0,255,0)
blue = pygame.Color(0,0,255)
# INITIALIZING PYGAME
pygame.init()
# INITIALIZING GAME WINDOW
pygame.display.set_caption('Snake game (Aryan yadav)')
game_window = pygame.display.set_mode((window_x,window_y))
# SETTING FPS CONTROLLER
fps = pygame.time.Clock()
# DEFINING SNAKES DEFAULT POSITION
snake_position = [100,50]
# DEFINING FIRST FOUR BLOCKS
# BODY
snake_body = [
[100,50],
[90,50],
[80,50],
[70,50]
]
# FOOD POSITION
food_position = [random.randrange(1,(window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]
food_spawn = True
# DEFAULT DIRECTION OF SNAKE
# LEFT - RIGHT
direction = 'RIGHT'
change = direction
# STORING SCORE
score = 0
# SCORE DISPLAY FUNCTION
def display_score(choice, color, font, size):
# CREATING FONT OBJECT
score_font = pygame.font.SysFont(font, size)
# SCORE SURFACE
score_surface = score_font.render('Score : ' + str(score), True, color)
# TEXT SURFACE AREA
score_rect = score_surface.get_rect()
# DISPLAY TEXT
game_window.blit(score_surface, score_rect)
def game_over():
# FONT OBJECT
my_font = pygame.font.SysFont('times new roman', 50)
# DISPLAY TEXT SURFACE
game_over_surface = my_font.render('Your Score is : ' + str(score), True, red)
# RECTANGULAR TEXT SURFACE OBJECT
game_over_rect = game_over_surface.get_rect()
# POSITION OF THE TEXT
game_over_rect.midtop = (window_x/2, window_y/4)
# DRAWING TEXT ON SCREEN
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
# QUIT GAME JUST AFTER TWO SECONDS
time.sleep(2)
# DEACTIVATING PYGAME LIBRARY
pygame.quit()
# QUIT THE PROGRAM
quit()
#MAIN FUNCTION
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change = 'UP'
if event.key == pygame.K_DOWN:
change = 'DOWN'
if event.key == pygame.K_LEFT:
change = 'LEFT'
if event.key == pygame.K_RIGHT:
change = 'RIGHT'
# IF TWO KEY PRESSED SIMULTANEOUSLY
if change == 'UP' and direction != 'DOWN':
direction = 'UP'
if change == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# MOVING THE SNAKE
if direction == 'UP':
snake_position[1] -= 10
if direction == 'DOWN':
snake_position[1] +=10
if direction == 'LEFT':
snake_position[0] -=10
if direction == 'RIGHT':
snake_position[0] +=10
# SNAKE BODY GROWING LOGIC
snake_body.insert(0,list(snake_position))
if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
score += 10
food_spawn = False
else:
snake_body.pop()
if not food_spawn:
food_position = [random.randrange(1,(window_x//10))*10,
random.randrange(1, (window_y//10)) * 10]
food_spawn = True
game_window.fill(black)
for pops in snake_body:
pygame.draw.rect(game_window, green, pygame.Rect(pops[0], pops[1], 10, 10))
pygame.draw.rect(game_window, white, pygame.Rect(food_position
[0], food_position[1], 10, 10))
# GAME OVER
if snake_position[0] < 0 or snake_position[0] > window_x-10:
game_over()
if snake_position[1] < 0 or snake_position[1] > window_y-10:
game_over()
# TOUCHING SNAKE
for block in snake_body[1:]:
if snake_position[0]== block[0] and snake_position[1] == block[1]:
game_over()
# DISPLAYING SCORE CONTINOUSLY
display_score(1, white, 'times new roman', 20)
# REFRESH GAME SCREEN
pygame.display.update()
# FRAME RATE
fps.tick(snake_speed)