-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrickanoid_levels.py
165 lines (145 loc) · 5.51 KB
/
brickanoid_levels.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
import brickanoid_elements
LVL0 = '''
- - - - - - - - - - - - - - - -
'''
LVL1 = '''
- - - - - - - - - - - - - - - -
------------------------------------------------
------------------------------------------------
------------------4rnrrrrrrrrr------------------
- - - - - - - - - - - - - - - -
'''
LVL2 = '''
- - - - - - - - - - - - - - - -
------------------------------------------------
------------------3bnbbbbbb---------------------
------------3bnbbbbbb1pn3bnbbbbbb---------------
------------------3bnbbbbbb---------------------
- - - - - - - - - - - - - - - -
'''
LVL3 = '''
- - - - - - - - - - - - - - - -
------------3rnrrrrrr------3rnrrrrrr------------
---------4rnrrrrrrrrr2rnrrr4rnrrrrrrrrr---------
------------3rnrrrrrr2snsss3rnrrrrrr------------
---------------2rnrrr2snsss2rnrrr---------------
---------------3rnrrrrrr3rnrrrrrr---------------
------------------4rnrrrrrrrrr------------------
---------------------2rnrrr---------------------
- - - - - - - - - - - - - - - -
'''
LVL4 = '''
- - - - - - - - - - - - - - - -
---------------1cn------------1cn---------------
---------------1cn4gnggggggggg1cn---------------
---------------1cn---2gnggg---1cn---------------
---------------1cn4gnggggggggg1cn---------------
---------------1cn------------1cn---------------
---------------1cn------------1cn---------------
- - - - - - - - - - - - - - - -
'''
LVL5 = '''
- - - - - - - - - - - - - - - -
------------------------------------------------
---------------1dn4bnbbbbbbbbb1dn---------------
------------------------------------------------
------1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn------
- - - - - - - - - - - - - - - -
'''
LVL6 = '''
- - - - - - - - - - - - - - - -
------------------------1bn---------------------
---------------------1bn1dn1bn------------------
------------------1bn3dndddddd1bn---------------
------------1bn3dndddddd1dn3dndddddd1bn---------
---------------1bn2dnddd1dn2dnddd1bn------------
------------------1bn3dndddddd1bn---------------
---------------1bn2dnddd1bn2dnddd1bn------------
---------------1bn1dn1bn---1bn1dn1bn------------
------------------1bn---------1bn---------------
- - - - - - - - - - - - - - - -
'''
LVLEND = '''
------------------4rnrrrrrrrrr------------------
1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn1cn
------------------------------------------------
'''
def load_levels(logic):
level_loader = LevelLoader(logic)
level_loader.define_level(1, LVL1)
level_loader.define_level(2, LVL2)
level_loader.define_level(3, LVL3)
level_loader.define_level(4, LVL4)
level_loader.define_level(5, LVL5)
level_loader.define_level(6, LVL6)
level_loader.define_level(7, LVLEND)
return level_loader
class LevelLoader():
_logic = None
_charbuffer = ''
_level_strings = {}
def __init__(self, logic):
self._logic = logic
@staticmethod
def create_brick(chars):
if len(chars) < 3:
raise BufferError(f"String '{chars}' must contain exactly 3 chars")
length = chars[0]
if length not in ['1', '2', '3', '4']:
raise ValueError(f"'{length}' is not a valid brick length")
color = chars[1]
enhanc = chars[2]
if enhanc not in ['n', 'l', 'g', 's']:
raise ValueError(f"'{enhanc}' is not a valid brick enhancement")
#dinamically create classname for brick
classname = 'Brick'
if color in ['r', 'R']:
classname = classname + 'Red'
elif color in ['b', 'B']:
classname = classname + 'Blu'
elif color in ['g', 'G']:
classname = classname + 'Green'
elif color in ['p', 'P']:
classname = classname + 'Pink'
elif color in ['s', 'S']:
classname = classname + 'Silv'
elif color in ['d', 'D']:
classname = classname + 'Gold'
elif color in ['c', 'C']:
classname = classname + 'Concrete'
else:
raise ValueError(f"'{color}' is not a valid brick color")
classname = classname + '_' + length + '_0'
tt = getattr(brickanoid_elements, classname)
return tt()
def define_level(self, level, charbuffer):
self._level_strings[level] = charbuffer
def load_level(self, level):
charbuffer = self._level_strings[level]
self._current_column = 0
self._add_from_chars(charbuffer)
def _add_from_chars(self, charbuffer):
row = 0
column = 0
# remove first empty lines
while charbuffer[0] in ('\n', '\r'):
charbuffer = charbuffer[1:]
while len(charbuffer[3 * column:]) > 3:
while charbuffer[3 * column] in ('-', ' '):
column += 1
continue
newrow = False
while len(charbuffer[3 * column:]) > 0 and \
charbuffer[3 * column] in ('\n', '\r'):
newrow = True
charbuffer = charbuffer[(3 * column) + 1:]
column = 0
if newrow:
row += 1
continue
brick = LevelLoader.create_brick(charbuffer[3 * column:])
if brick:
# go over next 3 x brick-length chars in buffer
self._logic.add_brick_to_screen(brick, column, row)
column += brick.bwidth
# newline if CR/LF found or end of string