forked from pi3d/pi3d_demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.py
217 lines (181 loc) · 6.48 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals
""" based on code Written by Simpliplant, this just happened to be the
first result for a google search for 'python snake code'
"""
from random import randrange
from math import sin, cos, radians
import demo
import pi3d
NORTH, SOUTH = (0.0, 0.0, 1.0), (0.0, 0.0, -1.0) # (dx, dy, dz)
EAST, WEST = (1.0, 0.0, 0.0), (-1.0, 0.0, 0.0) # (dx, dy, dz)
UP, DOWN = (0.0, 1.0, 0.0), (0.0, -1.0, 0.0) # (dx, dy, dz)
TURN_LEFT = {NORTH:WEST, WEST:SOUTH, SOUTH:EAST, EAST:NORTH}
TURN_RIGHT = {NORTH:EAST, EAST:SOUTH, SOUTH:WEST, WEST:NORTH}
SIZE = 100.0
BLOCK_SIZE = 4.0
# Setup display and initialise pi3d
DISPLAY = pi3d.Display.create(x=100, y=100, frames_per_second=10)
DISPLAY.set_background(0.4,0.8,0.8,1) # r,g,b,alpha
#========================================
CAMERA = pi3d.Camera()
LIGHT = pi3d.Light(lightamb=(0.6, 0.6, 0.8))
# load shader
shader = pi3d.Shader("uv_light")
flatsh = pi3d.Shader("uv_flat")
foodTex = pi3d.Texture("textures/Raspi256x256.png")
partTex = pi3d.Texture("textures/world_map256x256.jpg")
ectex = pi3d.loadECfiles("textures/ecubes","skybox_hall")
myecube = pi3d.EnvironmentCube(size=2.0 * SIZE, maptype="FACES", name="cube")
myecube.set_draw_details(shader, ectex)
class Food:
def __init__(self, snake):
self.snake = snake
plane = pi3d.Plane(w=BLOCK_SIZE * 2.0, h=BLOCK_SIZE * 2.0)
self.shape = pi3d.MergeShape()
self.shape.add(plane, 0,0,0)
self.shape.add(plane, 0,0,0, 0,90,0)
self.shape.set_draw_details(shader, [foodTex])
self.spawn()
def spawn(self):
collision = True
while collision:
random_x = randrange(-SIZE, SIZE, BLOCK_SIZE) + BLOCK_SIZE / 2.0
random_y = randrange(-SIZE, SIZE, BLOCK_SIZE) + BLOCK_SIZE / 2.0
random_z = randrange(-SIZE, SIZE, BLOCK_SIZE) + BLOCK_SIZE / 2.0
collision = False
for each in snake.parts:
if each.shape.x() == random_x and each.shape.y() == random_y and each.shape.z() == random_z:
collision = True
break
self.shape.position(random_x, random_y, random_z)
def draw(self):
self.shape.draw()
class Part:
def __init__(self, x=0, y=0, z=0, direction=NORTH):
self.direction = direction
#self.shape = pi3d.Cuboid(w=BLOCK_SIZE, h=BLOCK_SIZE, d=BLOCK_SIZE)
self.shape = pi3d.Sphere(radius=BLOCK_SIZE / 2.0)
self.shape.set_draw_details(shader, [partTex])
self.shape.position(x, y, z)
self.speed = BLOCK_SIZE
def change_direction(self, direction):
d_sum = 0
for i in (0,1,2):
d_sum += abs(self.direction[i] + direction[i])
if d_sum == 0: #going back on self
return
self.direction = direction
def check(self):
if self.shape.x() >= SIZE - BLOCK_SIZE / 2.0 and self.direction == EAST:
return False
if self.shape.x() <= -SIZE + BLOCK_SIZE / 2.0 and self.direction == WEST:
return False
if self.shape.y() >= SIZE - BLOCK_SIZE / 2.0 and self.direction == UP:
return False
if self.shape.y() <= -SIZE + BLOCK_SIZE / 2.0 and self.direction == DOWN:
return False
if self.shape.z() >= SIZE - BLOCK_SIZE / 2.0 and self.direction == NORTH:
return False
if self.shape.z() <= -SIZE + BLOCK_SIZE / 2.0 and self.direction == SOUTH:
return False
return True
def move(self):
self.shape.translate(self.direction[0] * self.speed,
self.direction[1] * self.speed,
self.direction[2] * self.speed)
class Snake:
def __init__(self, x=0, y=0, z=0):
self.head = Part(x, y, z)
self.direction = NORTH
self.length = 1
self.parts = []
self.parts.append(self.head)
self.extend_flag = 0
def change_direction(self, direction):
self.direction = direction
def move(self, food):
new_direction = self.direction
old_direction = None
new_part = None
if self.extend_flag > 0:
last_part = self.parts[-1]
new_part = Part(last_part.shape.x(), last_part.shape.y(),
last_part.shape.z(), last_part.direction)
for each in self.parts:
old_direction = each.direction
each.change_direction(new_direction)
each.move()
new_direction = old_direction
if not self.head.check():
return False
if self.extend_flag > 0:
self.extend(new_part)
for each in self.parts[1:]:
if (each.shape.x() == self.head.shape.x() and
each.shape.y() == self.head.shape.y() and
each.shape.z() == self.head.shape.z()):
return False
if pi3d.Utility.distance([food.shape.x(), food.shape.y(), food.shape.z()],
[self.head.shape.x(), self.head.shape.y(), self.head.shape.z()]) < BLOCK_SIZE:
food.spawn()
self.extend_flag = 2
return True
def extend(self, part):
self.parts.append(part)
self.length += 1
self.extend_flag -= 1
def draw(self):
for part in self.parts:
part.shape.rotateIncY(5.31)
part.shape.rotateIncZ(0.47)
part.shape.draw()
# Fetch key presses
mykeys = pi3d.Keyboard()
snake = Snake()
food = Food(snake)
CAMERA.direction = snake.direction
tilt = -10.0
rot = 0.0
rot_target = 0.0
rad = 45.0
while DISPLAY.loop_running():
rot = 0.75 * rot + 0.25 * rot_target
CAMERA.reset()
CAMERA.rotate(tilt, rot, 0)
CAMERA.position((snake.head.shape.x() + sin(radians(rot - 10)) * rad,
snake.head.shape.y() - sin(radians(tilt)) * rad,
snake.head.shape.z() - cos(radians(rot - 10))* rad))
snake.draw()
food.draw()
myecube.draw()
#Press ESCAPE to terminate
k = mykeys.read()
if k >-1:
if k == 27: #esc
break
elif k == 261 or k == 137: # rgt
CAMERA.direction = TURN_RIGHT[CAMERA.direction]
rot_target = rot_target - 90
snake.change_direction(CAMERA.direction)
elif k == 260 or k == 136: # lft
CAMERA.direction = TURN_LEFT[CAMERA.direction]
rot_target = rot_target + 90
snake.change_direction(CAMERA.direction)
elif k == 259 or k == 134: # up
if snake.direction == DOWN or snake.direction == UP:
snake.change_direction(CAMERA.direction)
else:
snake.change_direction(UP)
elif k == 258 or k == 135: # dwn
if snake.direction == DOWN or snake.direction == UP:
snake.change_direction(CAMERA.direction)
else:
snake.change_direction(DOWN)
elif (k == 32): # sp
snake.extend_flag = True
if not snake.move(food):
break
print("you grew to a length of {} Earths".format(snake.length))
mykeys.close()
DISPLAY.stop()