This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan
<p>It was my eldest son's birthday the other day, and it was late in the evening on
said Birthday and I thought "I will give my son the gift of learning how to
program". It worked as I expected, he looked at me, wrinkled his nose, and got
back to playing Fifa sitting next to me whilst I smashed out a terrible game on
the amazing microbit.</p>
<p>My quick summary is that I think it is an amazing litle device for quickly
starting programming and getting into programming with hardware.</p>
<p>Arguably, I think the micro:bit is better than a Raspberry-pi or a Pi-zero for
starting with Software and hardware programming.</p>
<p>There is an incredibly low barrier to getting started, you plug it into the USB
open up a web browser, do some python and drop a hex file on to it and you can
start to build something that you can interact with. There is nothing else that
you have to do, you can get feedback on button clicks, get something on to the
display and go from there.</p>
<p>Compare this with my flow on the Pi's: Get SD-Card, Format and add linux, Get
device, boot, show people how to use linux, load up an editor build some hw
integrations.</p>
<p>Don't get me wrong, I love the Pi ecosystem but I wanted something that my son
could start to input on and interact with and see feedback within minutes and
the micro:bit was perfect for this..... even though he really just went back
to FIFA 2017.</p>
<p>Whilst my son was playing FIFA, I had a lot of fun with the micro:bit. I thought
it would be fun to create a Breakout or Arkanoid clone. It would test the 25
pixel display and I could learn about basic input via the buttons.</p>
<div class="yt-embed" style="position:relative;padding-bottom: 56.25%; padding-top: 25px; height: 0;">
<iframe
data-src="//www.youtube-nocookie.com/embed/VpYC25GeFeY?autoplay=1"
style="position: absolute; left: 0; top: 0; width: 100%; height: 100%; border:0;"
allowfullscreen
title="YouTube Video"></iframe>
</div>
<p>Below is my code for game (it is terrible and has some bugs in).</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">from</span> microbit <span style="color:#f92672">import</span> <span style="color:#f92672">*</span>
<span style="color:#75715e"># quickly create a level of two rows, with pixels set to 0 hits and 1 hits</span>
blocks <span style="color:#f92672">=</span> [[<span style="color:#ae81ff">1</span> <span style="color:#f92672">-</span> i <span style="color:#66d9ef">for</span> j <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">5</span>) ] <span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">2</span>)]
ball <span style="color:#f92672">=</span> [<span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>]
ball_direction <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>]
paddle <span style="color:#f92672">=</span> [<span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">4</span>]
previous_game_time <span style="color:#f92672">=</span> running_time()
ball_timing <span style="color:#f92672">=</span> running_time()
game_time <span style="color:#f92672">=</span> running_time()
running <span style="color:#f92672">=</span> True
<span style="color:#75715e"># Set up the board.</span>
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">setup_line</span>(line, value):
<span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">5</span>):
display<span style="color:#f92672">.</span>set_pixel(i, line, value)
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">draw_blocks</span>():
<span style="color:#66d9ef">for</span> x <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">5</span>):
<span style="color:#66d9ef">for</span> y <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">2</span>):
display<span style="color:#f92672">.</span>set_pixel(x, y, blocks[y][x])
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">draw_ball</span>():
display<span style="color:#f92672">.</span>set_pixel(ball[<span style="color:#ae81ff">0</span>], ball[<span style="color:#ae81ff">1</span>], <span style="color:#ae81ff">4</span>)
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">draw_paddle</span>():
display<span style="color:#f92672">.</span>set_pixel(paddle[<span style="color:#ae81ff">0</span>], paddle[<span style="color:#ae81ff">1</span>], <span style="color:#ae81ff">5</span>)
display<span style="color:#f92672">.</span>set_pixel(paddle[<span style="color:#ae81ff">0</span>]<span style="color:#f92672">+</span><span style="color:#ae81ff">1</span>, paddle[<span style="color:#ae81ff">1</span>], <span style="color:#ae81ff">5</span>)
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">move_ball</span>():
<span style="color:#66d9ef">global</span> ball
ball <span style="color:#f92672">=</span> next_position()
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">next_position</span>():
<span style="color:#66d9ef">return</span> [ball[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">+</span> ball_direction[<span style="color:#ae81ff">0</span>], ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">+</span> ball_direction[<span style="color:#ae81ff">1</span>]]
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">check_ball_collisions</span>():
<span style="color:#66d9ef">global</span> running
<span style="color:#66d9ef">if</span> ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">==</span> <span style="color:#ae81ff">4</span>:
<span style="color:#75715e"># Has the ball hit the bottom. Stop the ball and Game over</span>
ball_direction[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
ball_direction[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
running <span style="color:#f92672">=</span> False
<span style="color:#66d9ef">return</span>
<span style="color:#75715e"># Has the ball hit the paddle.</span>
<span style="color:#66d9ef">if</span> ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">==</span> <span style="color:#ae81ff">3</span> <span style="color:#f92672">and</span> (ball[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">==</span> paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">or</span> ball[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">==</span> paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>):
<span style="color:#75715e"># 3, so it doesn't embed in the paddle</span>
ball_direction[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">=</span> <span style="color:#f92672">-</span>ball_direction[<span style="color:#ae81ff">1</span>]
<span style="color:#66d9ef">if</span> ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">==</span> <span style="color:#ae81ff">3</span> <span style="color:#f92672">and</span> (ball[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">==</span> paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span> <span style="color:#f92672">or</span> ball[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">==</span> paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">+</span> <span style="color:#ae81ff">2</span>):
<span style="color:#75715e"># ball hit odd angle of paddle.. note might reflect it back up one day</span>
ball_direction[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> <span style="color:#f92672">-</span>ball_direction[<span style="color:#ae81ff">0</span>]
<span style="color:#75715e"># Has the ball hit wall (left, right, top). We can still hit something</span>
<span style="color:#66d9ef">if</span> ball[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">or</span> ball[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">==</span> <span style="color:#ae81ff">4</span>:
ball_direction[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> <span style="color:#f92672">-</span>ball_direction[<span style="color:#ae81ff">0</span>]
<span style="color:#66d9ef">if</span> ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span>:
ball_direction[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">=</span> <span style="color:#f92672">-</span>ball_direction[<span style="color:#ae81ff">1</span>]
<span style="color:#66d9ef">if</span> ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672"><=</span> <span style="color:#ae81ff">2</span>:
<span style="color:#75715e"># Blocks can only be in top two rows in this game</span>
<span style="color:#66d9ef">if</span> blocks[ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>][ball[<span style="color:#ae81ff">0</span>]] <span style="color:#f92672">></span> <span style="color:#ae81ff">0</span>:
<span style="color:#75715e"># We hit a block, take a life from the block</span>
blocks[ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>][ball[<span style="color:#ae81ff">0</span>]] <span style="color:#f92672">=</span> blocks[ball[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>][ball[<span style="color:#ae81ff">0</span>]] <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>
<span style="color:#75715e"># send the ball back down.</span>
ball_direction[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">=</span> <span style="color:#f92672">-</span>ball_direction[<span style="color:#ae81ff">1</span>]
flipflop <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>
<span style="color:#66d9ef">while</span> running:
display<span style="color:#f92672">.</span>clear()
<span style="color:#75715e"># get input</span>
<span style="color:#66d9ef">if</span> button_a<span style="color:#f92672">.</span>is_pressed() <span style="color:#f92672">and</span> paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">>=</span> <span style="color:#ae81ff">1</span>:
paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>
<span style="color:#66d9ef">if</span> button_b<span style="color:#f92672">.</span>is_pressed() <span style="color:#f92672">and</span> paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672"><</span> <span style="color:#ae81ff">3</span>:
paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> paddle[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>
<span style="color:#66d9ef">if</span> game_time <span style="color:#f92672">-</span> ball_timing <span style="color:#f92672">></span> <span style="color:#ae81ff">500</span>:
flipflop <span style="color:#f92672">=</span> flipflop <span style="color:#f92672">^</span> <span style="color:#ae81ff">1</span>
display<span style="color:#f92672">.</span>set_pixel(<span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">4</span>, flipflop)
move_ball()
check_ball_collisions()
ball_timing <span style="color:#f92672">=</span> game_time
<span style="color:#75715e"># draw state</span>
draw_blocks()
draw_ball()
draw_paddle()
previous_game_time <span style="color:#f92672">=</span> game_time
game_time <span style="color:#f92672">=</span> running_time()
sleep(<span style="color:#ae81ff">100</span>)
display<span style="color:#f92672">.</span>clear()
display<span style="color:#f92672">.</span>scroll(<span style="color:#e6db74">'Game Over'</span>)
</code></pre></div><p>Here is an <a href="https://gist.github.com/PaulKinlan/235efaa3acc539b99e613a59097e4527">ever-up-to date gist</a>.</p>
<p>There is a lot to like about the environment, but there are somethings that I
think could be smoothed out:</p>
<ol>
<li>The Web Editor for Python is amazing but you have to DL the hex file and
then move it the USB folder. It would be great if it could integrate with
the <em>experimental</em> WebUSB API so that I could just press run and it would
deploy on the device.</li>
<li>Debugging is a pain, if you get an error the error is display on the 5x5
pixel display :) It would be great if we could either debug easily via my
development machine, or if I had an emulator of the device that would have
sped up the debug cycle.</li>
</ol>
This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan