Skip to content

Commit e5ac1fc

Browse files
Mark Shannondpgeorge
authored andcommitted
Update examples to work with new image/display API.
1 parent f94d3eb commit e5ac1fc

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

examples/conway.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
arena2 = bytearray(7 * 7)
1111

1212
def show():
13+
img = microbit.Image(5,5)
1314
for y in range(5):
1415
for x in range(5):
15-
microbit.display.image.set_pixel(x, y, arena1[8 + y * 7 + x])
16+
img.set_pixel(x, y, arena1[8 + y * 7 + x]*9)
17+
microbit.display.print(img)
1618

1719
# do 1 iteration of Conway's Game of Life
1820
def conway_step():

examples/maze.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import microbit
77

8-
d = microbit.display.image
8+
d = microbit.display
99
ac = microbit.accelerometer
1010

1111
# the maze data, as binary numbers (outside walls are added automatically)
@@ -34,10 +34,15 @@ def get_maze(x, y):
3434
else:
3535
return 1
3636

37-
def draw(x, y):
37+
def draw(x, y, tick):
38+
img = microbit.Image(5,5)
3839
for j in range(5):
3940
for i in range(5):
40-
d.set_pixel(i, j, get_maze(x + i - 2, y + j - 2))
41+
img.set_pixel(i, j, get_maze(x + i - 2, y + j - 2)*5)
42+
43+
# draw the player, flashing
44+
img.set_pixel(2, 2, (tick & 1)*4+5)
45+
d.print(img)
4146

4247
def main():
4348
x = 0
@@ -60,10 +65,8 @@ def main():
6065
y = min(15, max(0, y))
6166

6267
# draw the maze
63-
draw(x, y)
68+
draw(x, y, tick)
6469

65-
# draw the player, flashing
66-
d.set_pixel(2, 2, tick & 1)
6770

6871
microbit.sleep(50)
6972

examples/pomodoro.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
CLOCK_ADJUST = 4/60
1414

15-
ALL_LEDS_ON = Image('1,1,1,1,1\n'*5)
15+
ALL_LEDS_ON = Image('99999\n'*5)
1616

1717

1818
def index_to_xy(i):

examples/simple_slalom.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import microbit as m
66

7-
m.display.set_display_mode(1)
8-
97
p = m.display.print
108

119
min_x = -1024
@@ -98,7 +96,7 @@
9896
use_wall_y = max(wall_y, 0)
9997
for wall_x in range(5):
10098
if wall_x != hole:
101-
s(wall_x, use_wall_y, 32)
99+
s(wall_x, use_wall_y, 6)
102100

103101
if wall_reached_player and not handled_this_wall:
104102
handled_this_wall = True
@@ -108,7 +106,7 @@
108106
score += 1
109107

110108
if player_update:
111-
s(player_x, 4, 255) # turn on new pixel
109+
s(player_x, 4, 9) # turn on new pixel
112110

113111
p(i)
114112

examples/watch.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def show_time(time):
7878

7979

8080
def show_colon(visible):
81-
display.image.set_pixel(2, 1, visible)
82-
display.image.set_pixel(2, 3, visible)
81+
display.set_pixel(2, 1, visible*9)
82+
display.set_pixel(2, 3, visible*9)
8383

8484

8585
def get_clock_time():
@@ -98,16 +98,16 @@ def plot_LED_column(column, number):
9898
if number <= 5:
9999
for i in range(4, -1, -1):
100100
if i < 5 - number:
101-
display.image.set_pixel(column, i, 0)
101+
display.set_pixel(column, i, 0)
102102
else:
103-
display.image.set_pixel(column, i, 1)
103+
display.set_pixel(column, i, 9)
104104

105105
if number > 5:
106106
for i in range(4, -1, -1):
107107
if i < 5 - (number - 5):
108-
display.image.set_pixel(column, i, 1)
108+
display.set_pixel(column, i, 9)
109109
else:
110-
display.image.set_pixel(column, i, 0)
110+
display.set_pixel(column, i, 0)
111111

112112

113113
while True:

0 commit comments

Comments
 (0)