-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
199 lines (173 loc) · 5.61 KB
/
Copy pathmain.py
File metadata and controls
199 lines (173 loc) · 5.61 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
# snake game with pygame
# By: slime3000fly and a little bit by angater1
import pygame, sys, time, random
from pygame import mixer
pygame.init()
pygame.display.init()
fps_controller = pygame.time.Clock()
screen = pygame.display.set_mode((1080, 720))
# initial score
score = 0
# reading highest score from txt
f = open('highest_score.txt', 'r')
highest_score = int(f.read())
f.close()
#music
mixer.music.set_volume(0.5)
mixer.music.load('snake (by damn so deep).mp3')
mixer.music.play(2)
mixer.music.queue('koks terrarka (by damn so deep).mp3')
mixer.music.play(1)
mixer.music.queue('The Fall (by damn so deep).mp3')
mixer.music.play()
#sfx
lose_sound = mixer.Sound('lose.wav')
apple_sound = mixer.Sound('apple.wav')
#color
black = (0, 0, 0)
def show_score(choice, color, font, size):
score_font = pygame.font.SysFont(font, size)
score_surface = score_font.render('Score : ' + str(score) + ' ' + 'Highest Score : ' + str(highest_score), True,
color)
score_rect = score_surface.get_rect()
screen.blit(score_surface, score_rect)
# variable declartaion
x = [200]
y = [200]
checker = [0]
number_of_apple = 0
size = 1 # variable which define how long is snake
t = 0.5
z1 = 0
z2 = 0
move = 20 # variable which define how big is single step for snake
x_apple = 0
y_apple = 0
# function declaration
def lose():
# function which play after lose game
lose_sound.play()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.QUIT: sys.exit()
# drawing writing 'you lose'
font = pygame.font.Font('freesansbold.ttf', 52)
text = font.render('YOU LOSE', True, red, white)
# text surface object
textRect = text.get_rect()
# set the center of the rectangular object.
textRect.center = (540, 360)
screen.blit(text, textRect)
pygame.display.update()
def touch():
# function to check if snake touch itself or wall
snake_head_x = x[0]
snake_head_y = y[0]
# check if snake is colliding itself
for i in range(1, size):
for k in range(0, 11):
if (snake_head_x == x[i] + k):
for k in range(0, 11):
if (snake_head_y == y[i] + k): lose()
# check if snake is touching wall
if snake_head_x >= 1060 or snake_head_x <= 20 or snake_head_y >= 700 or snake_head_y <= 20:
lose()
# color
red = pygame.Color(139, 0, 0)
blue = pygame.Color(51, 255, 255)
white = pygame.Color(255, 255, 255)
orange = (255, 100, 0)
yellow = (255, 50, 170)
done = False
while not done:
# print(size)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
# adding new element to list x and y
for i in range(0, size):
if (i >= len(x)):
x.append(i)
y.append(i)
touch()
# key to control snake
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
z1 = - move
z2 = 0
if event.key == pygame.K_DOWN:
z1 = move
z2 = 0
if event.key == pygame.K_RIGHT:
z2 = move
z1 = 0
if event.key == pygame.K_LEFT:
z2 = - move
z1 = 0
if event.key == pygame.K_ESCAPE:
done = True
# nubmer to move snake
x[0] = x[0] + z2
y[0] = y[0] + z1
# create x and y for drawing apple
if number_of_apple == 0:
x_apple = random.randint(50, 1000)
y_apple = random.randint(50, 650)
number_of_apple = 1
screen.fill(white)
# drawing snake
i = 0
for i in range(0, size):
# drawing snake head
if (i == 0): snake_head = pygame.draw.rect(screen, yellow, pygame.Rect(x[i], y[i], 20, 20))
# drawing another part of snake
if (i == 1):
temp_x2 = x[i]
temp_y2 = y[i]
x[i] = previos_x
y[i] = previos_y
pygame.draw.rect(screen, blue, pygame.Rect(x[i], y[i], 20, 20))
if (i >= 2):
if (i % 2 == 0):
temp_x1 = x[i]
x[i] = temp_x2
temp_y1 = y[i]
y[i] = temp_y2
pygame.draw.rect(screen, blue, pygame.Rect(x[i], y[i], 20, 20))
else:
temp_x2 = x[i]
x[i] = temp_x1
temp_y2 = y[i]
y[i] = temp_y1
pygame.draw.rect(screen, blue, pygame.Rect(x[i], y[i], 20, 20))
# draw apple
pygame.draw.rect(screen, red, pygame.Rect(x_apple, y_apple, 10, 10))
# drawing wall
pygame.draw.rect(screen, orange, pygame.Rect(0, 0, 20, 720))
pygame.draw.rect(screen, orange, pygame.Rect(1070, 0, 20, 720))
pygame.draw.rect(screen, orange, pygame.Rect(0, 0, 1080, 20))
pygame.draw.rect(screen, orange, pygame.Rect(0, 710, 1080, 20))
# drawing score
show_score(1, black, 'times new roman', 20)
pygame.display.flip()
# check if snake eat apple
for i in range(-17, 16):
if x[0] + i == x_apple:
for i in range(-17, 16):
if y[0] + i == y_apple:
number_of_apple = 0
size += 1
score += 10
apple_sound.play()
if score > highest_score:
highest_score = score
#saving highest score to highest_score.txt
f = open('highest_score.txt', 'w')
f.write(str(highest_score))
f.close()
# FPS !!!!!
fps_controller.tick(15)
previos_x = x[0]
previos_y = y[0]