microbit百度百科简介
微信上看到的广告,感觉挺好玩,49块买来玩。其实是儿童编程课程,结果是我玩的还挺开心,哈哈。
其实就是支持自己编程的硬件,有块5x5的点阵屏,其实能显示的内容很有限,但是也能玩一些有趣的游戏。编程语言支持scratch、python、javascript。scratch拖来拖去太累,python没研究过,javascript还比较简单,就有js编写的。微软有个makecode在线编辑器可以直接在线编辑。
microbit迷宫
讲下迷宫游戏的制作过程吧
首先网上搜了一张迷宫图
将其转化为二维矩阵,0代表可以走,1代表有墙,路线略微修改了一点。
let maze = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
剩下的事情就比较简单了,从迷宫地图里取出一块5*5的矩阵在显示屏上显示,再亮一个灯表示玩家。A键向左或向下运动,B键向上或向右运动。通过AB键切换运动方向。判断下玩家可以行动的范围,到达最上面时游戏胜利。
完整代码如下:
class Point {
x: number
y: number
constructor(x: number, y: number) {
this.x = x
this.y = y
}
left() {
return new Point(this.x - 1, this.y)
}
right() {
return new Point(this.x + 1, this.y)
}
top() {
return new Point(this.x, this.y - 1)
}
below() {
return new Point(this.x, this.y + 1)
}
}
//判断p点灯是否点亮
function isLight(p: Point) {
return led.point(p.x, p.y)
}
function moveLeft() {
//左边灯不亮,而且player还没有到达最左边时可以向左移动
if (!isLight(player.left()) && player.x > 0) {
//已经到达地图最左边或者player位置靠右时移动player位置
//否则移动地图位置
if (col == 0 || player.x > 2) {
player.x -= 1
} else {
col -= 1
}
loadMap()
}
}
function moveRight() {
if (!isLight(player.right()) && player.x < 4) {
//总共21列,col=15时已经到达地图最右侧
if (col >= 15 || player.x < 2) {
player.x += 1
} else {
col += 1
}
loadMap()
}
}
function moveUp() {
if (!isLight(player.top())) {
if (row <= 0 || player.y > 2) {
player.y -= 1
} else {
row -= 1
}
loadMap()
}
}
function moveDown() {
if (!isLight(player.below())) {
if (row >= 18 || player.y < 2) {
if (player.y < 4) {
player.y += 1
}
} else {
row += 1
}
loadMap()
}
}
function loadMap() {
basic.clearScreen()
for (let i = row; i < row + 5 && i < maze.length; i++) {
for (let j = col; j < col + 5 && j < maze[i].length; j++) {
if (maze[i][j] == 1) {
led.plot(j - col, i - row)
}
}
}
}
input.onButtonPressed(Button.A, function () {
//y=0时游戏已结束
if (player.y > 0) {
if (direction == directions.vertical) {
moveDown()
} else {
moveLeft()
}
}
})
input.onButtonPressed(Button.AB, function () {
if (direction == directions.vertical) {
direction = directions.horizatal
} else {
direction = directions.vertical
}
})
input.onButtonPressed(Button.B, function () {
if (player.y > 0) {
if (direction == directions.vertical) {
moveUp()
} else {
moveRight()
}
}
})
let maze = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1,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 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
let wait = 500
let col = 9
let row = 18
let player = new Point(2, 4)
enum directions {
vertical = 0,
horizatal = 1
}
let direction = directions.vertical
loadMap()
while (player.y != 0) {
led.unplot(player.x, player.y)
basic.pause(250)
led.plot(player.x, player.y)
basic.pause(250)
}
while (true) {
basic.pause(wait)
basic.showIcon(IconNames.Heart)
basic.pause(wait)
basic.showIcon(IconNames.SmallHeart)
}
贪食蛇
俄罗斯方块
更多推荐
micro:bit 5x5点阵屏玩迷宫、俄罗斯方块、贪食蛇
发布评论