forked from DeepNinja07x/Python_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThe_Snake_Game.py
136 lines (107 loc) · 4.24 KB
/
The_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pygame,sys
import time
import random
pygame.init() #Initializing PyGame Module
#Setting colors
white=(255,255,255)
black=(0,0,0)
red=(255,0,0)
#Setting boreders in px
window_width=800
window_height=600
gameDisplay=pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption('Slither.io - The Snake Game')
clock=pygame.time.Clock()#varible for getting time within the program
FPS=5 #Frame_per_second
blockSize=20
noPixel=0
def myquit():
'''Self explanatory'''
pygame.quit()
sys.exit(0)
font=pygame.font.SysFont(None,35,bold=True)
def drawGrid():
sizeGrd=window_width//blockSize
def snake(blockSize,snakelist):
#x=250-(segment_width+segment_margin)*i
for size in snakelist:
pygame.draw.rect(gameDisplay,white,[size[0]+5,size[1],blockSize,blockSize],2)
def message_to_screen(msg,color):
screen_text=font.render(msg,True,color)
gameDisplay.blit(screen_text,[100,window_height/2])
def gameLoop():
gameExit=False
gameOver=False
lead_x=window_width/2
lead_y=window_height/2
change_pixels_of_x=0
change_pixels_of_y=0
snakelist = []
snakeLength = 1
randomAppleX = int(random.randrange(0, window_width-blockSize)/10)*10
randomAppleY = int(random.randrange(0, window_height-blockSize)/10)*10
while not gameExit:
while gameOver == True:
gameDisplay.fill(black)
message_to_screen("Game over, press 'c' to play again or 'q' to quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameOver = False
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
myquit()
leftArrow = event.key == pygame.K_LEFT
rightArrow = event.key == pygame.K_RIGHT
upArrow = event.key == pygame.K_UP
downArrow = event.key == pygame.K_DOWN
if leftArrow:
change_pixels_of_x = -blockSize
change_pixels_of_y = noPixel
elif rightArrow:
change_pixels_of_x = blockSize
change_pixels_of_y = noPixel
elif upArrow:
change_pixels_of_y = -blockSize
change_pixels_of_x = noPixel
elif downArrow:
change_pixels_of_y = blockSize
change_pixels_of_x = noPixel
if lead_x >= window_width or lead_x < 0 or lead_y >= window_height or lead_y < 0:
gameOver = True
lead_x += change_pixels_of_x
lead_y += change_pixels_of_y
gameDisplay.fill(black)
AppleThickness = 20
print([int(randomAppleX),int(randomAppleY),AppleThickness,AppleThickness])
pygame.draw.circle(gameDisplay, red, [randomAppleX,randomAppleY],10)
allspriteslist = []
allspriteslist.append(lead_x)
allspriteslist.append(lead_y)
snakelist.append(allspriteslist)
if len(snakelist) > snakeLength:
del snakelist[0]
for eachSegment in snakelist [:-1]:
if eachSegment == allspriteslist:
gameOver = True
snake(blockSize, snakelist)
pygame.display.update()
if lead_x >= randomAppleX and lead_x <= randomAppleX + AppleThickness:
if lead_y >= randomAppleY and lead_y <= randomAppleY + AppleThickness:
randomAppleX = int(random.randrange(0, window_width-blockSize)/10)*10
randomAppleY = int(random.randrange(0, window_height-blockSize)/10)*10
snakeLength += 1
clock.tick(FPS)
pygame.quit()
quit()
gameLoop()