-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
181 lines (151 loc) · 5.03 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#import pywin32 #Painting pixels
#import pyPNG #Painting bitmaps
import pygame, time
from random import randint
class GridPoint:
def __init__(self, x, y):
self.x = x
self.y = y
AREA = GridPoint(640,480)
SPEED_DELAY = 0.1
STEP_SIZE = 20 #Needs to be the same as sprite size
BG_COLOR = (0,0,0)
UP = "U"
DOWN = "D"
LEFT = "L"
RIGHT = "R"
pygame.init()
game_screen = pygame.display.set_mode((AREA.x, AREA.y))
continue_game = True
snake_head = pygame.sprite.Sprite()
snake_head.tail = []
snake_head.image = pygame.image.load("snake_seg.gif") ##TODO get a snake image
snake_head.rect = snake_head.image.get_rect()
snake_group = pygame.sprite.GroupSingle(snake_head)
apple = pygame.sprite.Sprite()
apple.live = False
apple.image = pygame.image.load("apple.gif")
apple.rect = apple.image.get_rect()
apple_group = pygame.sprite.GroupSingle(apple)
#TODO make sure game screen is divisable by the STEP_SIZE
if snake_head.rect.width != apple.rect.width and snake_head.rect.height != apple.rect.height:
print "ERROR: Sprites are not a common size"
elif snake_head.rect.width != snake_head.rect.height:
print "ERROR: Snake sprite is not square"
elif apple.rect.width != apple.rect.height:
print "ERROR: Apple sprite is not square"
else:
STEP_SIZE = snake_head.rect.width
def isExitGameEvent(event):
return event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE)
def moveSprite(sprite):
if sprite.direction == UP:
sprite.rect.top -= STEP_SIZE
elif sprite.direction == DOWN:
sprite.rect.top += STEP_SIZE
elif sprite.direction == LEFT:
sprite.rect.left -= STEP_SIZE
elif sprite.direction == RIGHT:
sprite.rect.left += STEP_SIZE
if sprite.rect.left < 0:
sprite.rect.left = 0
if sprite.rect.top < 0:
sprite.rect.top = 0
if sprite.rect.bottom > AREA.y:
sprite.rect.bottom = AREA.y
if sprite.rect.right > AREA.x:
sprite.rect.right = AREA.x
def handleEvents():
"""Handle any events and return False if the game should end, True otherwise"""
for event in pygame.event.get():
if isExitGameEvent(event):
return False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake_head.direction = UP
if event.key == pygame.K_DOWN:
snake_head.direction = DOWN
if event.key == pygame.K_LEFT:
snake_head.direction = LEFT
if event.key == pygame.K_RIGHT:
snake_head.direction = RIGHT
if event.key == pygame.K_p:
print "P Pressed"
elif event.type == pygame.KEYUP:
pass
return True
def waitForKeyPress():
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
return
def createApples():
"""If there are no apples, places one at a random location"""
if not apple.live:
apple.rect.top = randint(0, AREA.y - STEP_SIZE)
apple.rect.top -= apple.rect.top % STEP_SIZE
apple.rect.left = randint(0, AREA.x - STEP_SIZE)
apple.rect.left -= apple.rect.left % STEP_SIZE
apple.live = True
def clearApples():
apple.live = False
def eatAvailiableApples(eater):
"""Check if eater is one an apple, if so, eat it"""
if (eater.rect.left == apple.rect.left) & (eater.rect.top == apple.rect.top):
return True
else:
return False
def snakeIsTangled(snake):
y = snake.rect.top
x = snake.rect.left
for tailSeg in snake.tail:
if y == tailSeg.y and x == tailSeg.x:
return True
return False
def updateSnake():
"""Update snake and tail positions, return False if snake is tangled"""
snake_head.tail.append(GridPoint(snake_head.rect.left, snake_head.rect.top))
moveSprite(snake_head)
if eatAvailiableApples(snake_head):
clearApples()
#dont clip tail which -in effect- grows the tail by one
elif snakeIsTangled(snake_head):
return False
else:
snake_head.tail.pop(0)
return True
def drawSnake():
halfStep = STEP_SIZE/2
snake_group.draw(game_screen)
for tailSeg in snake_head.tail:
pygame.draw.circle(game_screen, (10,200,10), (tailSeg.x+halfStep,tailSeg.y+halfStep), STEP_SIZE/3, 2)
def printText(game_screen, text, yLoc, size, color = (255,255,255)):
gameFont = pygame.font.Font(None, size)
label = gameFont.render(text, 1, color)
lblHeight = label.get_rect().bottom - label.get_rect().top
lblWidth = label.get_rect().right - label.get_rect().left
game_screen.blit(label, (AREA.x/2 - lblWidth/2, yLoc)) #Centered
def printEndOfGameSummary(game_screen, score):
game_screen.fill(BG_COLOR)
time.sleep(1)
printText(game_screen, "Game Over", 150, 35)
printText(game_screen, "Score: {}".format(score), 200, 30)
pygame.display.update()
waitForKeyPress()
waitForKeyPress()
snake_head.direction = DOWN
game_speed_mofifier = SPEED_DELAY
while continue_game:
game_speed_mofifier = SPEED_DELAY - (0.0005 * len(snake_head.tail))
time.sleep(game_speed_mofifier)
continue_game = handleEvents()
continue_game = updateSnake()
game_screen.fill(BG_COLOR)
printText(game_screen, "{}, {}".format(len(snake_head.tail), game_speed_mofifier), 10, 30, (100,100,100))
drawSnake()
createApples()
apple_group.draw(game_screen)
pygame.display.update()
print "Score: {}".format(len(snake_head.tail)-1)
printEndOfGameSummary(game_screen, len(snake_head.tail)-1)
pygame.quit()