-
Notifications
You must be signed in to change notification settings - Fork 0
/
microduplex.py
218 lines (192 loc) · 6.09 KB
/
microduplex.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
218
# Burning Flame simulation and Tetris-like game
# BBC Micro:bit
# Dmitriy Smirnov, 2019
from microbit import display, button_a, button_b, sleep
import random
class Particle:
__max_x_pos = 4
__max_intensity = 9
__min_intensity = 1
__max_fading = 3
__min_fading = 1
def reset( self ):
self.ypos = 4
self.xpos = random.randint( 0, Particle.__max_x_pos )
self.intensity = random.randint( Particle.__min_intensity, Particle.__max_intensity )
self.fading = random.randint( Particle.__min_fading, Particle.__max_fading )
self.velocity = 0.4 + self.intensity / 10 * random.random()
def __init__( self, xpos, ypos ):
self.ypos = ypos
self.xpos = xpos # random.randint( 0, Particle.__max_x_pos )
self.intensity = random.randint( Particle.__min_intensity, Particle.__max_intensity )
self.fading = random.randint( Particle.__min_fading, Particle.__max_fading )
self.velocity = 0.4 + self.intensity / 10 * random.random()
def draw( self ):
display.set_pixel( self.xpos, int( self.ypos ), int( self.intensity ) )
def next( self ):
self.ypos -= self.velocity
if self.ypos < 0:
self.ypos = 0
self.intensity -= 2 / ( 1 + pow( 2.71, -self.fading ) )
if self.intensity < 0:
self.intensity = 0
def hasNext( self ):
return self.ypos > 0
def fire():
particles = [ Particle( 0, 0 ), Particle( 1, 0 ), Particle( 2, 0 ), Particle( 3, 0 ), Particle( 4, 0 ),
Particle( 0, 2 ), Particle( 1, 2 ), Particle( 2, 2 ), Particle( 3, 2 ), Particle( 4, 2 ) ]
display.clear()
while not( button_a.was_pressed() or button_b.was_pressed() ):
for p in particles:
p.draw()
for p in particles:
if p.hasNext():
p.next()
else:
p.reset()
sleep( 40 )
return
def compact( rows ):
compacted = False
stp = 2
for step in range( 1, 8, stp ):
result = [];
for row in rows:
cnt = len( row )
if 0 == row.count( get_empty_value() ):
compacted = True
for i in range( 0, cnt ):
row[ i ] -= stp
if row[ i ] < get_empty_value():
row[ i ] = get_empty_value()
if cnt == row.count( get_empty_value() ):
result.insert( 0, row )
else:
result.append( row )
else:
result.append( row )
if not compacted:
break;
draw( result )
sleep( 40 )
return result
def get_empty_value():
return 0
def isempty( col ):
return col == get_empty_value()
def draw( rows, x = 0, y = 0, empty = True ):
rowid = y
for row in rows:
colid = x
for col in row:
if empty or not isempty( col ):
display.set_pixel( colid, rowid, int( col ) )
colid += 1
rowid += 1
return
def canmove( rows, fugure, x0, y0 ):
y = y0
for frow in fugure:
x = x0
for fcol in frow:
if not isempty( fcol ):
if y >= len( rows ): return False
elif x >= len( rows[ y ] ): return False
elif not isempty( rows[ y ][ x ] ): return False
x += 1
y += 1
return True
def merge( rows, fugure, x0, y0, empty = True ):
y = y0
for frow in fugure:
x = x0
for fcol in frow:
if empty or not isempty( fcol ):
rows[ y ][ x ] = fcol
x += 1
y += 1
return rows
def fade():
l1 = [ [ 9, 9, 9, 9, 9 ],
# [ 9, 8, 7, 8, 9 ],
[ 7, 6, 5, 6, 7 ],
[ 5, 4, 3, 4, 5 ],
# [ 4, 3, 2, 3, 4 ],
[ 3, 2, 1, 2, 3 ],
# [ 2, 1, 0, 1, 2 ],
[ 1, 0, 0, 0, 1 ] ]
for l in l1:
draw( [ l,
l,
l,
l,
l ] )
sleep( 100 )
brick = 7
duplex =[ [ [ brick ],
[ brick] ],
[ [ brick, brick ] ],
[ [ brick ],
[ brick ] ],
[ [ brick, brick ] ],
[ [ brick ],
[ brick ] ]
]
fire()
figures = []
while True:
while len( figures ) > 0 and not( button_a.was_pressed() or button_b.was_pressed() ):
sleep( 20 )
display.clear()
figures = duplex
fade()
rows = [[ 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0 ]]
draw( rows )
score = 0
total_score = score
level = 1
delay = 100
while True:
moved = False
y = -1
x = 1
figure = figures[ random.randint( 0, len( figures ) - 1 ) ]
while canmove( rows, figure, x, y + 1 ):
moved = True
y = y + 1
draw( rows )
draw( figure, x, y, False )
for i in range( 10 ):
sleep( delay )
if button_a.was_pressed() and x > 0 and canmove( rows, figure, x - 1, y ):
x -= 1
draw( rows )
draw( figure, x, y, False )
if button_b.was_pressed() and canmove( rows, figure, x + 1, y ):
x += 1
draw( rows )
draw( figure, x, y, False )
sleep( 200 )
if not moved:
break
else:
rows = merge( rows, figure, x, y, False )
rows = compact( rows )
draw( rows )
score += 1
if figures == duplex and score % ( 10 * level ) == 0:
level += 1
total_score += score;
score = 0
if delay > 10:
delay -= 5
total_score += score;
display.scroll( "SCORE " + str( total_score ) + " LEVEL " + str( level ) + " ",
delay=80, wait=False, loop=True, monospace = False )
sleep( 100 )
button_a.was_pressed()
button_b.was_pressed()