forked from Manish1Gupta/Coding-Community-Contributions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlither-io.py
More file actions
148 lines (108 loc) · 4.01 KB
/
Slither-io.py
File metadata and controls
148 lines (108 loc) · 4.01 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
import pygame, sys
import time
import random
pygame.init()
window_ht = 600
window_wt = 1000
black = (0, 0 , 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 255, 0)
green = (0, 0 ,255)
window = pygame.display.set_mode([window_wt,window_ht])
pygame.display.set_caption('Slither.eat : The Snake Game')
screen = pygame.display.get_surface()
font = pygame.font.SysFont(None, 25, bold = True)
def myquit():
pygame.quit()
sys.exit(0)
clock = pygame.time.Clock()
FPS = 5 # frame rate for refreshing screen
blockSize = 20
noPixel = 0
def snake(blocksize, snakelist):
for size in snakelist:
pygame.draw.rect(screen, black, [size[0]+5,size[1],blocksize,blocksize])
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
screen.blit(screen_text, [window_wt/2, window_ht/2])
def gameLoop():
gameExit = False
gameOver = False
lead_x = window_wt/2
lead_y = window_ht/2
change_pixels_of_x = 0
change_pixels_of_y = 0
snakelist = []
snakeLength = 1
randomAppleX = round(random.randrange(0, window_wt-blockSize)/10.0)*10.0
randomAppleY = round(random.randrange(0, window_ht-blockSize)/10.0)*10.0
while not gameExit:
while gameOver == True:
screen.fill(blue)
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()
#Logic 1
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_x = noPixel
change_pixels_of_y = -blockSize
else:
change_pixels_of_x = noPixel
change_pixels_of_y = blockSize
#Logic 2
if lead_x >= window_wt or lead_x < 0 or lead_y >= window_ht or lead_y < 0:
gameOver = True
lead_x += change_pixels_of_x
lead_y += change_pixels_of_y
screen.fill(blue)
AppleThickness = 20
#Logic 3
pygame.draw.rect(screen, red, [randomAppleX,randomAppleY,AppleThickness,AppleThickness])
allspritelist = []
allspritelist.append(lead_x)
allspritelist.append(lead_y)
snakelist.append(allspritelist)
if len(snakelist) > snakeLength:
del snakelist[0]
for eachSegment in snakelist [:-1]:
if eachSegment == allspritelist:
gameOver = True
#Logic 4
snake(blockSize, snakelist)
pygame.display.update()
#Logic 5
if lead_x >= randomAppleX and lead_x <= randomAppleX + AppleThickness:
if lead_y >= randomAppleY and lead_y <= randomAppleY + AppleThickness:
randomAppleX = round(random.randrange(0, window_wt-blockSize)/10.0)*10.0
randomAppleY = round(random.randrange(0, window_ht-blockSize)/10.0)*10.0
snakeLength += 1
clock.tick(FPS)
pygame.quit()
quit()
gameLoop()