-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame code.py
More file actions
238 lines (186 loc) · 6.45 KB
/
game code.py
File metadata and controls
238 lines (186 loc) · 6.45 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
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
237
238
import pygame
import random
import os
pygame.init()
#For Adding music
#pygame.mixer.init()
# Colors
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,1)
orange = (255,255,0)
marroon = (115,0,0)
lime = (220,255,250)
pink = (150,250,240)
purple = (240,0,255)
# Creating window
screen_width = 800
screen_height = 600
gameWindow = pygame.display.set_mode((screen_width, screen_height))
# For background image
#bg = pygame.image.load("image name.jpg")
#bg = pygame.transform.scale(bg, (screen_width, screen_height)).convert_alpha
# Game Title
pygame.display.set_caption("THE SNAKE GAME")
pygame.display.update()
clock = pygame.time.Clock()
#font size
font = pygame.font.SysFont(None, 55)
def text_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gameWindow.blit(screen_text, [x,y])
size = pygame.font.SysFont(None, 200)
def textscreen(text, color, x, y):
screentext = size.render(text, True, color)
gameWindow.blit(screentext, [x,y])
textsize = pygame.font.SysFont(None, 35)
def txtscreen(text, color, x, y):
scrtext = textsize.render(text, True, color)
gameWindow.blit(scrtext, [x,y])
# For increasing snake length
def plot_snake(gameWindow, color, snake_list, snake_size):
for x,y in snake_list:
pygame.draw.rect(gameWindow, color, [x, y, snake_size, snake_size])
# For starting screen
def welcome():
exit_game = False
while not exit_game:
gameWindow.fill(orange)
text_screen("THE", blue, 80, 150)
textscreen("SNAKE", marroon, 100, 200)
text_screen("GAME", blue, 550, 330)
txtscreen("PRESS SPACE TO CONTINUE...", purple, 200,510)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
gameloop()
pygame.display.update()
clock.tick(30)
# Main Game Loop
def gameloop():
# Game specific variables
exit_game = False
game_over = False
snake_x = 40
snake_y = 40
snk_x = 50
snk_y = 50
velocity_x = 0
velocity_y = 0
radius = 10
apple = random.randint(100, 700)
berry = random.randint(100, 500)
score = 0
move_velocity = 3
fps = 30
snake_size = 20
snake_list = []
snake_length = 1
#check highscore file is exit or not
if (not os.path.exists("highscore.txt")):
with open("highscore.txt", "w") as f:
f.write("0")
# open file for read
file = open ("highscore.txt", "r")
high_score = file.read()
file.close()
#For game window
while not exit_game:
if game_over:
# Add new high score
with open("highscore.txt", "w") as f:
f.write(str(high_score))
# Game over window
gameWindow.fill(lime)
textscreen("GAME", marroon, 80, 90)
textscreen("OVER", marroon, 330, 240)
txtscreen("PRESS ENTER TO RESTART", blue, 200, 470)
for event in pygame.event.get():
# For quit game
if event.type == pygame.QUIT:
exit_game = True
# for restart game
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
welcome()
else:
for event in pygame.event.get():
# For quit game
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
#For control the game
if event.key == pygame.K_RIGHT:
velocity_x = move_velocity
velocity_y = 0
if event.key == pygame.K_LEFT:
velocity_x = - move_velocity
velocity_y = 0
if event.key == pygame.K_UP:
velocity_y = - move_velocity
velocity_x = 0
if event.key == pygame.K_DOWN:
velocity_y = move_velocity
velocity_x = 0
# For moving the snake
snake_x = snake_x + velocity_x
snake_y = snake_y + velocity_y
snk_x = snk_x + velocity_x
snk_y = snk_y + velocity_y
# scoring stage
if score % 20 == 19:
if abs(snk_x - apple) < 8 and abs(snk_y - berry) < 8:
score += 20
apple = random.randint(20, 500)
berry = random.randint(20, 400)
fps += 5
snake_length += 10
if score>int(high_score):
high_score = score
else:
if abs(snk_x - apple) < 8 and abs(snk_y - berry) < 8:
score +=1
apple = random.randint(20, 500)
berry = random.randint(20, 400)
fps += 1
snake_length +=10
if score>int(high_score):
high_score = score
# For snake background screen
gameWindow.fill(green)
# show background image on screen
# gameWindow.blit(bg, (0,0))
text_screen("Score: " + str(score), red, 5, 5)
text_screen("High Score: " + str(high_score), red, 480, 5)
# food type
if score % 20 ==19:
pygame.draw.circle(gameWindow, red, [apple, berry], 9)
else:
pygame.draw.circle(gameWindow, blue, [apple, berry], 7)
# Snake body
head = []
head.append(snake_x)
head.append(snake_y)
snake_list.append(head)
if len(snake_list) > snake_length:
del snake_list[0]
# when snake touch itself
if head in snake_list[:-1]:
game_over = True
# when snake touch boundary
if snake_x<0 or snake_x>screen_width or snake_y<0 or snake_y>screen_height \
or snk_x<0 or snk_x>screen_width or snk_y<0 or snk_y>screen_height:
game_over = True
# snake formed here
plot_snake(gameWindow, black, snake_list, snake_size)
pygame.draw.circle(gameWindow, yellow, [snk_x, snk_y], radius)
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()
welcome()