-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
151 lines (118 loc) · 4.02 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
146
147
148
149
150
151
import pygame
import random
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,480)) #sets the window size
pygame.display.set_caption('Snake') #sets the title bar
pygame.mouse.set_visible(0)
clock = pygame.time.Clock() # get a clock for the main loop
running = True # should the game stop?
started = False
up = 0 # directions
right = 1
down = 2
left = 3
snakePos = [(16,12)]
snakeDir = 4
lives = 3
length = 1 # how long the snake is
myfont = pygame.font.SysFont("monospace", 16)
# Add Fruit
fruitPos = [(random.randrange(1,32),random.randrange(1,24))]
gridSize = (32,24) # size of game grid
updateInvFreq = 5 # how many frames it takes for the game to update
snakeSquare = pygame.Surface((20,20)) # define a rectangle
snakeSquare.fill(pygame.Color("red")) # color it in
fruitSquare = pygame.Surface((20,20))
fruitSquare.fill(pygame.Color("blue"))
background = pygame.Surface((640,480))
background.fill(pygame.Color("black"))
def handleEvents():
global snakeDir # need to modify snake direction
global running # same here
global started
for event in pygame.event.get():
if event.type == QUIT: # quit if we press close
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
elif event.type == KEYDOWN and event.key == K_LEFT:
snakeDir = left
started = True
elif event.type == KEYDOWN and event.key == K_RIGHT:
snakeDir = right
started = True
elif event.type == KEYDOWN and event.key == K_UP:
snakeDir = up
started = True
elif event.type == KEYDOWN and event.key == K_DOWN:
snakeDir = down
started = True
def update():
global snakePos
global fruitPos
global running
global length
global lives
(x,y) = snakePos[0] # get first snake position and update it
if snakeDir == up:
y = (y-1) % gridSize[1]
elif snakeDir == down:
y = (y+1) % gridSize[1]
elif snakeDir == left:
x = (x-1) % gridSize[0]
elif snakeDir == right:
x = (x+1) % gridSize[0]
elif snakeDir == 4:
pass
else:
raise Exception(
"The direction of the snake isn't valid: " +
str(snakeDir)
)
if started:
snakePos.insert(0, (x,y)) # add new position to the snake
# longer snakes and add fruit logic
if (snakePos[0] == fruitPos[0]):
length += 1
fruitPos = [(random.randrange(1,32),random.randrange(1,24))]
elif (snakePos[0] in snakePos[1:] and snakePos[1][0]):
if(lives == 1): # then end game
snakePos = [(x,y)]
length = 1
lives = 0
running = False
else:
lives = lives - 1
else:
snakePos.pop() #remove old one so doesn't grow
def draw():
screen.blit(background, (0,0)) # clear the screen with the black rectangle
# draw fruit
screen.blit(fruitSquare, ((fruitPos[0][0] * 20), (fruitPos[0][1] * 20)))
# draw the snake blocks
# remember to multiply positions by the size of a block
for i in range(0,len(snakePos)):
screen.blit(snakeSquare, ((snakePos[i][0] * 20), (snakePos[i][1] * 20)))
# update
scoretext = myfont.render("Score = "+str(length-1), True, (255,255,255))
livestext = myfont.render("Lives = "+str(lives), True, (255,255,255))
screen.blit(scoretext, (100, 10))
screen.blit(livestext, (450, 10))
# and flip the screen
pygame.display.flip()
# how many frames have passed since last update?
updateCounter = 0
# WE NEED A GAME LOOP TO KEEP THE WINDOW OPEN, THIS IS JUST AN EXAMPLE:
while running:
# sync game with display
clock.tick(60)
# get events to update with
handleEvents()
# if enough frames have passed, update
if (updateCounter == 0):
update()
# redraw everything
draw()
updateCounter = (updateCounter + 1) % updateInvFreq
pygame.quit()