-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaml.py
More file actions
171 lines (120 loc) · 5.08 KB
/
yaml.py
File metadata and controls
171 lines (120 loc) · 5.08 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
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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import random
import yaml
from abc import ABC
class AbstractLevel(yaml.YAMLObject):
@classmethod
def get_map(cls):
return cls.Map()
@classmethod
def get_objects(cls):
return cls.Objects()
class Map(ABC):
pass
class Objects(ABC):
pass
class EasyLevel(AbstractLevel):
class Map:
def __init__(self):
self.Map = [[0 for _ in range(5)] for _ in range(5)]
for i in range(5):
for j in range(5):
if i == 0 or j == 0 or i == 4 or j == 4:
self.Map[j][i] = -1 # граница карты
else:
self.Map[j][i] = random.randint(0, 2) # случайная характеристика области
def get_map(self):
return self.Map
class Objects:
def __init__(self):
self.objects = [('next_lvl', (2, 2))]
self.config = {}
def get_objects(self, _map):
for obj_name in ['rat']:
coord = (random.randint(1, 3), random.randint(1, 3))
intersect = True
while intersect:
intersect = False
for obj in self.objects:
if coord == obj[1]:
intersect = True
coord = (random.randint(1, 3), random.randint(1, 3))
self.objects.append((obj_name, coord))
return self.objects
class MediumLevel(AbstractLevel):
class Map:
def __init__(self):
self.Map = [[0 for _ in range(8)] for _ in range(8)]
for i in range(8):
for j in range(8):
if i == 0 or j == 0 or i == 7 or j == 7:
self.Map[j][i] = -1 # граница карты
else:
self.Map[j][i] = random.randint(0, 2) # случайная характеристика области
def get_map(self):
return self.Map
class Objects:
def __init__(self):
self.objects = [('next_lvl', (4, 4))]
self.config = {'enemy': []}
def get_objects(self, _map):
for obj_name in self.config['enemy']:
coord = (random.randint(1, 6), random.randint(1, 6))
intersect = True
while intersect:
intersect = False
for obj in self.objects:
if coord == obj[1]:
intersect = True
coord = (random.randint(1, 6), random.randint(1, 6))
self.objects.append((obj_name, coord))
return self.objects
class HardLevel(AbstractLevel):
class Map:
def __init__(self):
self.Map = [[0 for _ in range(10)] for _ in range(10)]
for i in range(10):
for j in range(10):
if i == 0 or j == 0 or i == 9 or j == 9:
self.Map[j][i] = -1 # граница карты :: непроходимый участок карты
else:
self.Map[j][i] = random.randint(-1, 8) # случайная характеристика области
def get_map(self):
return self.Map
class Objects:
def __init__(self):
self.objects = [('next_lvl', (5, 5))]
self.config = {'enemy_count': 5, 'enemy': []}
def get_objects(self, _map):
for obj_name in self.config['enemy']:
for tmp_int in range(self.config['enemy_count']):
coord = (random.randint(1, 8), random.randint(1, 8))
intersect = True
while intersect:
intersect = False
if _map[coord[0]][coord[1]] == -1:
intersect = True
coord = (random.randint(1, 8), random.randint(1, 8))
continue
for obj in self.objects:
if coord == obj[1]:
intersect = True
coord = (random.randint(1, 8), random.randint(1, 8))
self.objects.append((obj_name, coord))
return self.objects
def easy_level_constructor(loader, node):
return {'map': EasyLevel.Map(), 'obj': EasyLevel.Objects()}
def medium_level_constructor(loader, node):
_obj = MediumLevel.Objects()
_obj.config = {'enemy': ['rat']}
return {'map': MediumLevel.Map(), 'obj': _obj}
def hard_level_constructor(loader, node):
_obj = HardLevel.Objects()
_obj.config = {'enemy': ['rat', 'snake', 'dragon'], 'enemy_count': 10}
return {'map': HardLevel.Map(), 'obj': _obj}
loader = yaml.Loader
loader.add_constructor("!easy_level", easy_level_constructor)
loader.add_constructor("!medium_level", medium_level_constructor)
loader.add_constructor("!hard_level", hard_level_constructor)