Skip to content

Commit f552962

Browse files
authored
Add files via upload
1 parent 56cac9e commit f552962

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed

Diff for: Snake/DATA/highscore.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
740

Diff for: Snake/Music/bgm.mp3

4.1 MB
Binary file not shown.

Diff for: Snake/Music/bgm1.mp3

3.31 MB
Binary file not shown.

Diff for: Snake/Music/bgm2.mp3

4.49 MB
Binary file not shown.

Diff for: Snake/Music/wc.mp3

5.85 MB
Binary file not shown.

Diff for: Snake/Snake.py

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
2+
3+
#Importing The Modules
4+
import pygame
5+
import random
6+
import os
7+
import sys
8+
#Initialization
9+
pygame.mixer.init()
10+
pygame.init()
11+
12+
13+
#Colors
14+
white = (255, 255, 255)
15+
red = (255, 0, 0)
16+
black = (0, 0, 0)
17+
american_rose= (255,3,62)
18+
amber = (255,191,0)
19+
azure = (0,127,255)
20+
snakegreen = (35, 45, 40)
21+
22+
#Game Backgrounds
23+
bg1 = pygame.image.load("Screen/gamebag/bg.jpg")
24+
bg2 = pygame.image.load("Screen/gamebag/bg2.jpg")
25+
bg3 = pygame.image.load("Screen/gamebag/bg3.jpg")
26+
intro = pygame.image.load("Screen/intro1S.png")
27+
outro = pygame.image.load("Screen/outro.png")
28+
outro1 = outro = pygame.image.load("Screen/outro.png")
29+
#Creating The window
30+
screen_width = 900
31+
screen_height = 600
32+
gameWindow = pygame.display.set_mode((screen_width, screen_height))
33+
34+
#Game Title
35+
pygame.display.set_caption("Snake By Siddhant")
36+
pygame.display.update()
37+
38+
#Music
39+
pygame.mixer.music.load('music/wc.mp3')
40+
pygame.mixer.music.play(100)
41+
pygame.mixer.music.set_volume(.6)
42+
43+
#Variables For The Game
44+
clock = pygame.time.Clock()
45+
font = pygame.font.SysFont('Harrington', 35)
46+
47+
def text_screen(text, color, x, y):
48+
screen_text = font.render(text, True, color)
49+
gameWindow.blit(screen_text, [x,y])
50+
51+
def plot_snake(gameWindow, color, snk_list, snake_size):
52+
for x,y in snk_list:
53+
pygame.draw.rect(gameWindow, color, [x, y, snake_size, snake_size])
54+
55+
56+
#Welcome Screen
57+
58+
def welcome():
59+
exit_game = False
60+
while not exit_game:
61+
initv = random.choice([3,4,5])
62+
mbg = random.choice([bg1,bg2,bg3])
63+
gameWindow.blit(intro, (0,0))
64+
text_screen("Theme - Technology & Toys",american_rose,300,150)
65+
for event in pygame.event.get():
66+
if event.type == pygame.QUIT:
67+
exit_game = True
68+
if event.type == pygame.KEYDOWN:
69+
if event.key == pygame.K_RETURN:
70+
pygame.mixer.music.fadeout(200)
71+
pygame.mixer.music.load('music/bgm.mp3')
72+
pygame.mixer.music.play(100)
73+
pygame.mixer.music.set_volume(.6)
74+
gameloop(initv,mbg)
75+
pygame.display.update()
76+
clock.tick(60)
77+
78+
# Game Loop
79+
def gameloop(init_velocity,mbg):
80+
81+
# Game specific variables
82+
exit_game = False
83+
game_over = False
84+
snake_x = 45
85+
snake_y = 55
86+
velocity_x = 0
87+
velocity_y = 0
88+
snk_list = []
89+
snk_length = 1
90+
91+
#Highscore Build
92+
if(not os.path.exists("DATA/highscore.txt")):
93+
with open("DATA/highscore.txt", "w") as f:
94+
f.write("0")
95+
with open("DATA/highscore.txt", "r") as f:
96+
highscore = f.read()
97+
98+
#Food
99+
food_x = random.randint(20, screen_width / 2)
100+
food_y = random.randint(20, screen_height / 2)
101+
102+
#Game Variables
103+
score = 0
104+
# print(init_velocity)
105+
snake_size = 30
106+
fps = 60
107+
while not exit_game:
108+
if game_over:
109+
with open("DATA/highscore.txt", "w") as f:
110+
f.write(str(highscore))
111+
112+
#GameOverScreen
113+
114+
gameWindow.blit(outro, (0, 0))
115+
text_screen("Score: " + str(score ), snakegreen, 385, 350)
116+
for event in pygame.event.get():
117+
if event.type == pygame.QUIT:
118+
exit_game = True
119+
if event.type == pygame.KEYDOWN:
120+
if event.key == pygame.K_RETURN:
121+
welcome()
122+
else:
123+
for event in pygame.event.get():
124+
if event.type == pygame.QUIT:
125+
exit_game = True
126+
if event.type == pygame.KEYDOWN:
127+
if event.key == pygame.K_RIGHT:
128+
velocity_x = init_velocity
129+
velocity_y = 0
130+
if event.key == pygame.K_LEFT:
131+
velocity_x = - init_velocity
132+
velocity_y = 0
133+
if event.key == pygame.K_UP:
134+
velocity_y = - init_velocity
135+
velocity_x = 0
136+
if event.key == pygame.K_DOWN:
137+
velocity_y = init_velocity
138+
velocity_x = 0
139+
if event.key == pygame.K_q:
140+
score +=10
141+
snake_x = snake_x + velocity_x
142+
snake_y = snake_y + velocity_y
143+
if abs(snake_x - food_x)<12 and abs(snake_y - food_y)<12:
144+
score +=10
145+
food_x = random.randint(20, screen_width / 2)
146+
food_y = random.randint(20, screen_height / 2)
147+
snk_length +=5
148+
if score>int(highscore):
149+
highscore = score
150+
gameWindow.blit(mbg, (0, 0))
151+
text_screen("Score: " + str(score) + " Highscore: "+str(highscore), snakegreen, 5, 5)
152+
pygame.draw.rect(gameWindow, red, [food_x, food_y, snake_size, snake_size])
153+
head = []
154+
head.append(snake_x)
155+
head.append(snake_y)
156+
snk_list.append(head)
157+
158+
159+
if len(snk_list)>snk_length:
160+
del snk_list[0]
161+
if head in snk_list[:-1]:
162+
game_over = True
163+
pygame.mixer.music.load('music/bgm1.mp3')
164+
pygame.mixer.music.play(100)
165+
pygame.mixer.music.set_volume(.6)
166+
if snake_x<0 or snake_x>screen_width or snake_y<0 or snake_y>screen_height:
167+
game_over = True
168+
pygame.mixer.music.load('music/bgm2.mp3')
169+
pygame.mixer.music.play(100)
170+
pygame.mixer.music.set_volume(.6)
171+
plot_snake(gameWindow, black, snk_list, snake_size)
172+
pygame.display.update()
173+
clock.tick(fps)
174+
pygame.quit()
175+
sys.exit()
176+
welcome()

0 commit comments

Comments
 (0)