-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_game.py
122 lines (110 loc) · 3.47 KB
/
snake_game.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
import pygame
import time
import random
pygame.init()
speed=15
#size of window
x=1200
y=800
#Colors
black=pygame.Color(0,0,0)
white=pygame.Color(255,255,255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
#Window Design
pygame.display.set_caption("Snack Game By Biswajit")
gamewindow=pygame.display.set_mode((x,y))#check agaiin
fps=pygame.time.Clock()
snakepos=[100,50]#change if needed
#Initial length of snake in blocks
body=[[100,50],[90,50],[80,50],[70,50]]
fruitpos=[random.randrange(1,(x//10))*10, random.randrange(1,(y//10))*10 ]
spawn=True
direction="RIGHT"
change=direction
score=0
def showscore(choice, color, font, size):
sfont=pygame.font.SysFont(font, size)
ssurf=sfont.render("Score : " + str(score), True, color)
srect=ssurf.get_rect()
gamewindow.blit(ssurf,srect)
def gameover():
myfont=pygame.font.SysFont("tw cen mt", 50)
gosurf=myfont.render("Wowwww! Your Score is : " + str(score), True, red)
gorect=gosurf.get_rect()
gorect.midtop=(x/2,y/2)#change if needed
gamewindow.blit(gosurf,gorect)
pygame.display.flip()
'''with open(f"{name}.txt","r") as f:
if int(f.read())<score:
with open(f"{name}.txt","w") as f:
f.write(f"{score}")'''
try:
f=open(f"{name.lower()}.txt","r")
k=open(f"{name.lower()}.txt","w")
if int(f.read())<score:
k.write(f"{score}")
except:
with open(f"{name.lower()}.txt","w") as f:
f.write(f"{score}")
time.sleep(2)
pygame.quit()
quit()
#MAIN CODE
name =input("Enter Your Name: ")
while True:
for i in pygame.event.get():
if i.type == pygame.KEYDOWN:
if i.key == pygame.K_UP:
change="UP"
if i.key == pygame.K_DOWN:
change="DOWN"
if i.key == pygame.K_RIGHT:
change="RIGHT"
if i.key == pygame.K_LEFT:
change="LEFT"
if i.key == pygame.K_ESCAPE:
pygame.quit()
quit()
#If 2 keys are presssed
if change == 'UP' and direction != 'DOWN':
direction = 'UP'
if change == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
if direction == "UP":
snakepos[1]-=10
elif direction =='DOWN':
snakepos[1]+=10
elif direction=="RIGHT":
snakepos[0]+=10
elif direction=="LEFT":
snakepos[0]-=10
#SNake Growing
body.insert(0,list(snakepos))
if snakepos[0]==fruitpos[0] and snakepos[1]==fruitpos[1]:
score+=10
spawn=False
else:
body.pop()
if not spawn:
fruitpos=[random.randrange(1,(x//10))*10,random.randrange(1,(y//10))*10]
speed+=2
spawn=True
gamewindow.fill(black)
for i in body:
pygame.draw.rect(gamewindow,green, pygame.Rect(i[0],i[1],10,10))
pygame.draw.rect(gamewindow,white,pygame.Rect(fruitpos[0],fruitpos[1], 10, 10))
#Game Over
if snakepos[0]<0 or snakepos[0]>x-10 or snakepos[1]<0 or snakepos[1]>y-10:
gameover()
for i in body[1:]:
if snakepos[0]==i[0] and snakepos[1]== i[1]:
gameover()
showscore(1, white, 'tw cen mt', 40)
pygame.display.update()
fps.tick(speed)