-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.py
More file actions
116 lines (103 loc) · 3.3 KB
/
Copy pathlevel.py
File metadata and controls
116 lines (103 loc) · 3.3 KB
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
from __future__ import division
import random
import math
#GLOBAL VARS
PROB_HOLES = .1
OBSTACLE_TYPE = 4
HOLE_TYPE = 3
NORMAL_TYPE = 0
CRACKING_1_TYPE = 1
CRACKING_2_TYPE = 2
BORDER_TYPE = 5
class level(object):
_obstacles = []
_holes = []
_holes_progress = {}
def __init__(self):
for i in range(128):
self._obstacles.append([])
self._holes.append([])
for j in range(128):
self._obstacles[i].append(False)
self._holes[i].append(False)
for i in range(128):
self.placeObstacle(0, i)
self.placeObstacle(127, i)
self.placeObstacle(i, 0)
self.placeObstacle(i, 127)
def update(self):
if random.random() < PROB_HOLES:
x = random.randint(0,127)
y = random.randint(0,127)
try:
self._holes_progress[(x, y)] += 1
except KeyError:
self._holes_progress[(x, y)] = 0
items_to_delete = []
for k, v in self._holes_progress.iteritems():
v += 1
self._holes_progress[k] += 1
if self._holes_progress[k] >= 3:
#print k
self._holes[k[0]][k[1]] = True
items_to_delete.append(k)
for i in items_to_delete:
del self._holes_progress[i]
def placeObstacle(self, x, y):
self._obstacles[x][y] = True
def getState(self):
return (self._obstacles, self._holes, self._holes_progress)
def getContents(self, center, rotation):
result = {}
center_x = center[0]
center_y = center[1]
center_x_min = math.floor(center_x - 8 * 1.414)
if center_x_min < 0:
center_x_min = 0
center_x_max = math.ceil(center_x + 8 * 1.414)
if center_x_max > 127:
center_x_max = 127
center_y_min = math.floor(center_y - 8 * 1.414)
if center_y_min < 0:
center_y_min = 0
center_y_max = math.ceil(center_y + 8 * 1.414)
if center_y_max > 127:
center_y_max = 127
for x in range(int(center_x_min), int(center_x_max) + 1):
for y in range(int(center_y_min), int(center_y_max) + 1):
element_x = (x - center_x)
element_y = (y - center_y)
value = 0
if self._obstacles[x][y]:
if x is 0 or x is 127 or y is 0 or y is 127:
value = BORDER_TYPE
else:
value = OBSTACLE_TYPE
elif (x, y) in self._holes_progress:
value = self._holes_progress[(x, y)]
elif self._holes[x][y]:
value = HOLE_TYPE
xprime = element_x * math.cos(rotation) - element_y * math.sin(rotation)
yprime = element_x * math.sin(rotation) + element_y * math.cos(rotation)
result[(xprime, yprime)] = value
return result
def collision(self, x, y):
start_x = int((x) // 32) - 1
if start_x < 0: start_x = 0
start_y = int((y) // 32) - 1
if start_y < 0: start_y = 0
obj_list = []
for i in range(0, 5):
for j in range(0, 5):
try:
if self._obstacles[start_x + i][start_y + j]:
collision_x = (start_x + i) * 32
collision_y = (start_y + j) * 32
obj_list.append((collision_x, collision_y, OBSTACLE_TYPE))
if self._holes[start_x + i][start_y + j]:
collision_x = (start_x + i) * 32
collision_y = (start_y + j) * 32
obj_list.append((collision_x, collision_y, HOLE_TYPE))
except IndexError:
pass
return obj_list