Skip to content

Commit bfaa972

Browse files
Merge pull request #314 from shashank0412/patch-2
Create snakegame.py
2 parents b7b6bf5 + 6e71b0b commit bfaa972

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

snake game/snakegame.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import pygame
2+
import random
3+
4+
5+
6+
7+
#colors
8+
white=(255,255,255)
9+
red=(255,0,0)
10+
black=(0,0,0)
11+
x = pygame.init()
12+
width_of_screen = 900
13+
height_of_screen = 600
14+
gameWindow = pygame.display.set_mode((width_of_screen,height_of_screen))
15+
pygame.display.set_caption("snake game-PythonGeeks")
16+
pygame.display.update()
17+
clock = pygame.time.Clock()
18+
font = pygame.font.SysFont(None,55)
19+
#display score on the screen
20+
def score_on_screen(text,color,x,y):
21+
screen_text = font.render(text, True , color)
22+
gameWindow.blit(screen_text,[x,y])
23+
24+
25+
26+
27+
# plotting the snake
28+
def plot_snake(gameWindow, color ,snake_list,snake_size):
29+
for x,y in snake_list:
30+
pygame.draw.rect(gameWindow,color,[x,y,snake_size,snake_size])
31+
32+
33+
34+
35+
# making the welcome screen
36+
def welcome():
37+
game_exit = False
38+
while not game_exit:
39+
gameWindow.fill((255,182,193))
40+
score_on_screen("Welcome to snakes game by PythonGeeks",black,90,250)
41+
score_on_screen("Press spacebar to play",black,232,290)
42+
for event in pygame.event.get():
43+
if event.type==pygame.QUIT:
44+
game_exit = True
45+
if event.type==pygame.KEYDOWN:
46+
if event.key==pygame.K_SPACE:
47+
game()
48+
pygame.display.update()
49+
clock.tick(60)
50+
51+
52+
53+
54+
55+
#snake game
56+
def game():
57+
game_exit= False
58+
game_over= False
59+
snake_x=45
60+
snake_y=55
61+
velocity_x= 0
62+
velocity_y= 0
63+
init_velocity = 5
64+
score= 0
65+
apple_x = random.randint(20,width_of_screen/2)
66+
apple_y = random.randint(20,height_of_screen/2)
67+
snake_size=30
68+
snake_list = []
69+
snake_length = 1
70+
fps=40
71+
with open ("highscore.txt","r") as f:
72+
highscore = f.read()
73+
while not game_exit:
74+
if game_over:
75+
with open ("highscore.txt","w") as f:
76+
f.write(str(highscore))
77+
gameWindow.fill(white)
78+
score_on_screen("Game Over! Press Enter to continue",red,100,250)
79+
for event in pygame.event.get():
80+
if event.type == pygame.QUIT:
81+
game_exit = True
82+
if event.type== pygame.KEYDOWN:
83+
if event.key == pygame.K_RETURN:
84+
welcome()
85+
else:
86+
for event in pygame.event.get():
87+
if event.type == pygame.QUIT:
88+
game_exit = True
89+
if event.type==pygame.KEYDOWN:
90+
if event.key == pygame.K_RIGHT:
91+
velocity_x = init_velocity
92+
velocity_y = 0
93+
if event.key == pygame.K_LEFT:
94+
velocity_x = -init_velocity
95+
velocity_y = 0
96+
if event.key == pygame.K_UP:
97+
velocity_y = -init_velocity
98+
velocity_x = 0
99+
if event.key == pygame.K_DOWN:
100+
velocity_y = init_velocity
101+
velocity_x = 0
102+
snake_x+=velocity_x
103+
snake_y+=velocity_y
104+
if abs(snake_x - apple_x)<20 and abs(snake_y - apple_y)<20:
105+
score+=10
106+
apple_x = random.randint(20,width_of_screen/2)
107+
apple_y = random.randint(20,height_of_screen/2)
108+
snake_length+=5
109+
if score>int(highscore):
110+
highscore = score
111+
gameWindow.fill(white)
112+
score_on_screen("Score: "+str(score) + " highscore: "+str(highscore),red,5,5)
113+
pygame.draw.rect(gameWindow,red,[apple_x,apple_y,snake_size,snake_size])
114+
head=[]
115+
head.append(snake_x)
116+
head.append(snake_y)
117+
snake_list.append(head)
118+
if len(snake_list)>snake_length:
119+
del snake_list[0]
120+
if head in snake_list[:-1]:
121+
game_over=True
122+
if snake_x<0 or snake_x>width_of_screen or snake_y<0 or snake_y>height_of_screen:
123+
game_over = True
124+
plot_snake(gameWindow,black,snake_list,snake_size)
125+
pygame.draw.rect(gameWindow,black,[snake_x,snake_y,snake_size,snake_size])
126+
pygame.display.update()
127+
clock.tick(fps)
128+
pygame.quit()
129+
quit()
130+
welcome()
131+
132+
133+

0 commit comments

Comments
 (0)