-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
157 lines (116 loc) · 2.93 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
#!/usr/bin/env python2
import pygame
import random
import sys
from knopgame.vector2d import Vector2D
##############################
# CLASSES
##############################
class GridObject(object):
def __init__(self, pos):
self.pos = pos
self.color = COLOR_WHITE
def draw(self):
pygame.draw.rect(screen, self.color,
(self.pos.x * 10,
self.pos.y * 10,
10, 10), 0)
class Food(GridObject):
def __init__(self, pos):
self.pos = pos
self.color = COLOR_RED
def update(self):
pass
def alive(self):
return True
def new_position(self):
self.pos = Vector2D(random.randint(0, width/grid_size),
random.randint(0, height/grid_size))
class Head(GridObject):
def __init__(self, pos):
self.pos = pos
self.vel = Vector2D(0, -1)
self.color = COLOR_WHITE
def update(self):
self.pos.add(self.vel)
def alive(self):
return True
def left(self):
self.vel = Vector2D(-1, 0)
def right(self):
self.vel = Vector2D(1, 0)
def up(self):
self.vel = Vector2D(0,-1)
def down(self):
self.vel = Vector2D(0, 1)
def eat(self):
print "eating"
##############################
# GLOBALS
##############################
COLOR_BLACK = (0,0,0)
COLOR_WHITE = (255,255,255)
COLOR_RED = (220, 0, 0)
width = 400
height = 400
grid_size = 10
screen = None
clock = None
elapsed = 0
gravity = Vector2D(0, 0.2)
head = None
food = None
objects = list()
##############################
# FUNCTIONS
##############################
def setup():
pygame.init()
global clock
clock = pygame.time.Clock()
global screen
screen = pygame.display.set_mode((width, height))
global objects
global head
global food
head = Head(Vector2D(20, 20))
food = Food(Vector2D(0,0))
food.new_position()
objects.append(head)
objects.append(food)
def loop():
global objects
global elapsed
keep_running = True
elapsed = clock.tick(2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
head.left()
if event.key == pygame.K_RIGHT:
head.right()
if event.key == pygame.K_UP:
head.up()
if event.key == pygame.K_DOWN:
head.down()
screen.fill(COLOR_BLACK)
if head.pos.equals(food.pos):
head.eat()
food.new_position()
for o in objects:
o.update()
o.draw()
# remove deleteable particles
objects = filter(lambda x: x.alive(), objects)
print len(objects)
pygame.display.update()
return keep_running
def main():
setup()
while(loop()):
pass
print "exiting cleanly"
if __name__ == "__main__":
main()