-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
145 lines (116 loc) · 4.05 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
137
138
139
140
141
142
143
144
145
# Snake Game
# By Mateusz Grochala
# github.com/Mat-Gr/snake
import random, pygame, sys
from pygame.locals import *
WINWIDTH = 640
WINHEIGHT = 480
PIXSIZE = 10
# R G B
GRAY = (45, 45, 45)
WHITE = (255, 255, 255)
BGCOLOR = GRAY
def main():
global FPSCLOCK, DISPLAYSURF
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
pygame.display.set_caption('Snake')
cords = [WINWIDTH/2, WINHEIGHT/2]
prevPos = (cords[0], cords[1])
snakeBody = [prevPos]
direction = 'up'
foodX = 0
foodY = 0
food = False
FPS = 15
while True:
scoreFontObj = pygame.font.Font('freesansbold.ttf', 16)
scoreTextSurfaceObj = scoreFontObj.render(score(snakeBody), True, WHITE)
scoreTextRectObj = scoreTextSurfaceObj.get_rect()
scoreTextRectObj.topleft = (10, 10)
GOfontObj = pygame.font.Font('freesansbold.ttf', 35)
GOtextSurfaceObj = GOfontObj.render('GAME OVER', True, WHITE)
GOtextRectObj = GOtextSurfaceObj.get_rect()
GOtextRectObj.center = (WINWIDTH/2, WINHEIGHT/2)
DISPLAYSURF.fill(BGCOLOR)
DISPLAYSURF.blit(scoreTextSurfaceObj, scoreTextRectObj)
# food
if food == False:
food_cords = generateFoodCords()
foodX = food_cords[0]
foodY = food_cords[1]
food = True
elif cords[0] == foodX and cords[1] == foodY:
food = False
snakeBody = eat(prevPos, snakeBody)
FPS += 0.5
# movement
if direction == 'up':
prevPos = (cords[0], cords[1])
cords[1] -= PIXSIZE
if cords[1] < 0:
cords[1] = WINHEIGHT-PIXSIZE
elif direction == 'right':
prevPos = (cords[0], cords[1])
cords[0] += PIXSIZE
if cords[0] > WINWIDTH-PIXSIZE:
cords[0] = 0
elif direction == 'down':
prevPos = (cords[0], cords[1])
cords[1] += PIXSIZE
if cords[1] > WINHEIGHT-PIXSIZE:
cords[1] = 0
elif direction == 'left':
prevPos = (cords[0], cords[1])
cords[0] -= PIXSIZE
if cords[0] < 0:
cords[0] = WINWIDTH-PIXSIZE
# death
if (cords[0], cords[1]) in snakeBody:
DISPLAYSURF.blit(GOtextSurfaceObj, GOtextRectObj)
pygame.display.update()
pygame.time.wait(2000)
snakeBody = [prevPos]
FPS = 15
# draw snake
snakeBody = updateSnake(prevPos, snakeBody)
drawSnake(cords, snakeBody)
# draw food
pygame.draw.rect(DISPLAYSURF, WHITE, (foodX, foodY, PIXSIZE, PIXSIZE), 1)
for event in pygame.event.get(): # event handling loop
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# change direction
elif event.type == KEYDOWN:
if event.key == K_UP:
direction = 'up'
elif event.key == K_RIGHT:
direction = 'right'
elif event.key == K_DOWN:
direction = 'down'
elif event.key == K_LEFT:
direction = 'left'
pygame.display.update()
FPSCLOCK.tick(FPS)
def score(snakeBody):
return 'Score: ' + str(len(snakeBody)-1)
def generateFoodCords():
foodX = random.randrange(0, WINWIDTH, PIXSIZE)
foodY = random.randrange(0, WINHEIGHT, PIXSIZE)
return [foodX, foodY]
def updateSnake(prevPos, snakeBody):
snakeBody.pop()
snakeBody.insert(0, prevPos)
return snakeBody
def eat(prevPos, snakeBody):
snakeBody.insert(0, prevPos)
return snakeBody
def drawSnake(cords, snakeBody):
snakeHead = Rect(cords[0], cords[1], PIXSIZE, PIXSIZE)
pygame.draw.rect(DISPLAYSURF, WHITE, snakeHead)
for val in snakeBody:
pygame.draw.rect(DISPLAYSURF, WHITE, (val, (PIXSIZE, PIXSIZE)))
if __name__ == '__main__':
main()