-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.py
194 lines (148 loc) · 6.02 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
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
import pygame
import random
import os
pygame.init()
#sound
pygame.mixer.init()
# define colors
white = (255, 255, 255)
red = (255, 0, 0)
black = (0,0,0)
screen_width = 1024
screen_height = 720
#creating window
gameWindow = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Fierce Snake") #game title
pygame.display.update() # Update portions of the screen for software displays
#images
background_image = pygame.image.load("background_image.jpg") #background image
background_image = pygame.transform.scale(background_image,(screen_width,screen_height)).convert_alpha()
start_image = pygame.image.load("start.jpg") #welcome screen
start_image = pygame.transform.scale(start_image,(screen_width,screen_height)).convert_alpha()
game_over_img = pygame.image.load("gameover.jpg") #gameover image
game_over_img = pygame.transform.scale(game_over_img,(screen_width,screen_height)).convert_alpha()
#for fps
clock = pygame.time.Clock()
#font for score etc
font = pygame.font.SysFont(None, 32)
#defining methods
def score_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gameWindow.blit(screen_text, [x, y])
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])
#welcome method for welcome screen
def welcome():
exit_game = False
while not exit_game:
gameWindow.fill(black)
gameWindow.blit(start_image,(0,0))
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:
pygame.mixer.music.load('backgroundmusic.ogg')
pygame.mixer.music.play(-1)
gameLoop()
pygame.display.update()
clock.tick(60)
#Game loop
def gameLoop():
# %%%%%%%%%%%%%%%%%%%%%% Game specific variables %%%%%%%%%%%%%%%%%%%%%%
exit_game = False
game_over = False
snake_x = 40
snake_y = 50
velocity_x = 0
velocity_y = 0
velocity_for_correction = 0
init_velocity = 5
fps = 60
snake_size = 30
#score
score = 0
# for increasing snakes height
snake_list = []
snake_length = 1
# for creating food for our snake
food_x = random.randint(0, screen_width / 2)
food_y = random.randint(0, screen_height / 2)
#check if highscore.txt exists
if(not os.path.exists("highscore.txt")):
with open("highscore.txt","w") as f:
f.write("0")
# opening file
with open("highscore.txt", "r") as f:
hiscore = f.read()
while not exit_game:
if game_over:
with open("highscore.txt", "w") as f:
f.write(str(hiscore))
gameWindow.fill(black)
gameWindow.blit(game_over_img, (0,0))
score_screen(str(score), white, 210, 662)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
welcome()
else :
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
velocity_x = init_velocity
velocity_y = velocity_for_correction
if event.key == pygame.K_LEFT:
velocity_x = - init_velocity
velocity_y = velocity_for_correction
if event.key == pygame.K_UP:
velocity_y = - init_velocity
velocity_x = velocity_for_correction
if event.key == pygame.K_DOWN:
velocity_y = init_velocity
velocity_x = velocity_for_correction
#%%%%%%%%%%%%%%%Cheat codes%%%%%%%%%%%%%%%%
if event.key == pygame.K_k:
score += 5
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
snake_x = snake_x + velocity_x
snake_y = snake_y + velocity_y
if abs(snake_x - food_x)<25 and abs(snake_y - food_y)<25:#abs() gives absolute values
score += 10
food_x = random.randint(0, screen_width / 2) # for putting food at random location
food_y = random.randint(0, screen_height / 2)
snake_length += 5
pygame.mixer.Channel(0).play(pygame.mixer.Sound('Beep.ogg'))
if score > int(hiscore):
hiscore = score
gameWindow.fill(black) #colors window white
gameWindow.blit(background_image, (0, 0) ) #set background image
pygame.draw.rect(gameWindow, white, [food_x, food_y, snake_size, snake_size]) #food
score_screen("Score :" + str(score), white, 5, 5)
score_screen("High Score :" + str(hiscore), white, 800, 5)
head = [] #snake's head
head.append(snake_x)
head.append(snake_y)
snake_list.append(head)
if len(snake_list)>snake_length:
del snake_list[0]
if head in snake_list[:-1]:
game_over = True
pygame.mixer.music.load('gameover.ogg')
pygame.mixer.music.play()
if snake_x < 0 or snake_x > screen_width or snake_y <0 or snake_y > screen_height:
game_over = True
pygame.mixer.music.load('gameover.ogg')
pygame.mixer.music.play()
plot_snake(gameWindow, red, snake_list, snake_size)
pygame.display.update()
clock.tick(fps) #frame per seconds
pygame.quit()
quit()
#call game loop
welcome() #Welcome Screen