-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
140 lines (101 loc) · 3.77 KB
/
snake.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()
white=(255,255,255)
black=(0,0,0)
red=(255,0,0)
window_width=800
window_height=600
gameDisplay=pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption("snake game")
font=pygame.font.SysFont(None,25,bold=True)
def myquit():
pygame.quit()
sys.exit(0)
clock=pygame.time.Clock()
FPS=5
blockSize=20
noPixel=0
def snake(blockSize,snakelist):
for size in snakelist:
pygame.draw.rect(gameDisplay,black,[size[0]+5,size[1],blockSize,blockSize])
def message_to_screen(msg,color):
screen_text=font.render(msg,True,color)
gameDisplay.blit(screen_text,[window_width//2,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=round(random.randrange(0,window_width-blockSize))
randomAppleY=round(random.randrange(0,window_height-blockSize))
while not gameExit:
while gameOver==True:
gameDisplay.fill(white)
message_to_screen("Game over, press c to play again",red)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.QUIT:
gameExit=True
gameOver=False
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(white)
AppleThickness=20
print([int(randomAppleX),int(randomAppleY),AppleThickness,AppleThickness])
pygame.draw.rect(gameDisplay,red,[randomAppleX,randomAppleY,AppleThickness,AppleThickness])
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=round(random.randrange(0,window_width-blockSize))
randomAppleY=round(random.randrange(0,window_height-blockSize))
snakeLength+=1
clock.tick(FPS)
pygame.quit()
quit()
gameloop()