未加星标

Some thoughts on the microbit

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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.

My quick summary is that I think it is an amazing litle device for quickly starting programming and getting into programming with hardware.

Arguably, I think the micro:bit is better than a Raspberry-pi or a Pi-zero for starting with Software and hardware programming.

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.

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.

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.

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.

Below is my code for game (it is terrible and has some bugs in).

from microbit import * # quickly create a level of two rows, with pixels set to 0 hits and 1 hits blocks = [[1 - i for j in range(5) ] for i in range(2)] ball = [2, 3] ball_direction = [1,-1] paddle = [2, 4] previous_game_time = running_time() ball_timing = running_time() game_time = running_time() running = True # Set up the board. def setup_line(line, value): for i in range(5): display.set_pixel(i, line, value) def draw_blocks(): for x in range(5): for y in range(2): display.set_pixel(x, y, blocks[y][x]) def draw_ball(): display.set_pixel(ball[0], ball[1], 4) def draw_paddle(): display.set_pixel(paddle[0], paddle[1], 5) display.set_pixel(paddle[0]+1, paddle[1], 5) def move_ball(): global ball ball = next_position() def next_position(): return [ball[0] + ball_direction[0], ball[1] + ball_direction[1]] def check_ball_collisions(): global running if ball[1] == 4: # Has the ball hit the bottom. Stop the ball and Game over ball_direction[0] = 0 ball_direction[1] = 0 running = False return # Has the ball hit the paddle. if ball[1] == 3 and (ball[0] == paddle[0] or ball[0] == paddle[0] + 1): # 3, so it doesn't embed in the paddle ball_direction[1] = -ball_direction[1] if ball[1] == 3 and (ball[0] == paddle[0] - 1 or ball[0] == paddle[0] + 2): # ball hit odd angle of paddle.. note might reflect it back up one day ball_direction[0] = -ball_direction[0] # Has the ball hit wall (left, right, top). We can still hit something if ball[0] == 0 or ball[0] == 4: ball_direction[0] = -ball_direction[0] if ball[1] == 0: ball_direction[1] = -ball_direction[1] if ball[1] <= 2: # Blocks can only be in top two rows in this game if blocks[ball[1] - 1][ball[0]] > 0: # We hit a block, take a life from the block blocks[ball[1] -1][ball[0]] = blocks[ball[1] -1][ball[0]] -1 # send the ball back down. ball_direction[1] = -ball_direction[1] flipflop = 1 while running: display.clear() # get input if button_a.is_pressed() and paddle[0] >= 1: paddle[0] = paddle[0] - 1 if button_b.is_pressed() and paddle[0] < 3: paddle[0] = paddle[0] + 1 if game_time - ball_timing > 500: flipflop = flipflop ^ 1 display.set_pixel(4, 4, flipflop) move_ball() check_ball_collisions() ball_timing = game_time # draw state draw_blocks() draw_ball() draw_paddle() previous_game_time = game_time game_time = running_time() sleep(100) display.clear() display.scroll('Game Over')

Here is an ever-up-to date gist .

There is a lot to like about the environment, but there are somethings that I think could be smoothed out:

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 experimental WebUSB API so that I could just press run and it would deploy on the device. 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.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: Python
分页:12
转载请注明
本文标题:Some thoughts on the microbit
本站链接:http://www.codesec.net/view/483852.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(16)