-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
172 lines (147 loc) · 5.13 KB
/
Copy pathsnake.py
File metadata and controls
172 lines (147 loc) · 5.13 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
import pygame
import random
import os
pygame.init()
pygame.mixer.init()
#colors
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)
green = (0, 255, 0)
yellow = (255, 255, 0)
bg_light = (245, 245, 245)
light_green = (144, 238, 144)
screen_w = 900
screen_h = 600
border_w = 800
border_h = 500
#creating window
gm_window = pygame.display.set_mode((screen_w, screen_h))
wel_img = pygame.image.load("wel_img.jpg")
wel_img = pygame.transform.scale(wel_img, (screen_w, screen_h)).convert_alpha()
pygame.display.set_caption("Snake Game")
pygame.display.update()
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 30)
def text_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gm_window.blit(screen_text, [x, y])
def plot_snake(snk_list, snake_size):
snake_yellow = (255, 200, 0)
snk_color = (102, 52, 0)
for x, y in snk_list:
snake_body = (0, 80, 0)
pygame.draw.circle(gm_window, snake_yellow, (x, y), snake_size)
pygame.draw.circle(gm_window, snk_color, (x, y), snake_size-0.01)
def welcome():
gm_window.blit(wel_img, (0,0))
pygame.mixer.music.load("bg_music.mpeg")
pygame.mixer.music.play()
exit_gm = False
while not exit_gm:
text_screen("Welcome to Snake Game!", black, 300, 250)
text_screen("Press Space Bar To Play!", black, 300, 300)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
gm_loop()
pygame.display.update()
clock.tick(30)
#Game Loop
def gm_loop():
pygame.mixer.music.unload()
#Game specific variables
exit_gm = False
gm_over = False
snake_x = 100
snake_y = 200
snake_size= 10
v_x = 0 #velocity
v_y = 0
fps = 30
score = 0
init_v = 5
border = 5
snk_list = []
snk_length = 1
food_x = random.randint(80, border_w-100)
food_y = random.randint(60, border_h-100)
food_size = 10
# check if hiscore file exists
if (not os.path.exists("hiscore.txt")):
with open("hiscore.txt", "w") as f:
f.write("0")
with open("hiscore.txt", "r") as f:
hiscore = f.read()
while not exit_gm:
if gm_over:
with open("hiscore.txt", "w") as f:
f.write(str(hiscore))
text_screen("Game Over! Press Enter to Continue", red, screen_w/3-50, screen_h/3)
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit_gm = 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_gm = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
v_x = init_v
v_y = 0
if event.key == pygame.K_LEFT:
v_x = -init_v
v_y = 0
if event.key == pygame.K_UP:
v_y = -init_v
v_x = 0
if event.key == pygame.K_DOWN:
v_y = init_v
v_x = 0
snake_x += v_x
snake_y += v_y
if abs(snake_x - food_x) < 20 and abs(snake_y - food_y) < 20:
pygame.mixer.music.load("bip.mpeg")
pygame.mixer.music.play()
if score>0 and score%50==0:
score = score+55
init_v += 2
else:
score += 5
food_x = random.randint(80, border_w/2)
food_y = random.randint(60, border_h/2)
snk_length += 5
if score>int(hiscore):
hiscore = score
gm_window.fill(light_green)
pygame.draw.rect(gm_window, green, (screen_w/15,screen_h/15, border_w, border_h), 3)
text_screen("Score: "+str(score) +" Hiscore: "+str(hiscore), red, 5, 5)
if score>0 and score%50==0:
pygame.draw.circle(gm_window, red, (food_x, food_y), food_size*2)
else:
pygame.draw.circle(gm_window, red, (food_x, food_y), food_size)
head = []
head.append(snake_x)
head.append(snake_y)
snk_list.append(head)
if len(snk_list)>snk_length:
del snk_list[0]
if head in snk_list[:-1]:
gm_over = True
pygame.mixer.music.load("blast.mpeg")
pygame.mixer.music.play()
if snake_x<70 or snake_x>border_w+45 or snake_y<55 or snake_y>border_h+25:
gm_over = True
pygame.mixer.music.load("blast.mpeg")
pygame.mixer.music.play()
plot_snake(snk_list , snake_size)
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()
welcome()