我最近得到了 BBC-Micro:bit,它有一个小的 5x5 LED“显示器”。
我想制作没有楼梯效果的平滑移动点的代码,但我很难在移动时使其垂直和水平平滑。
这就是我所拥有的,它只会垂直平滑点:
while (true) {
basic.clearScreen()
plotAA(input.acceleration(Dimension.X), input.acceleration(Dimension.Y),
255, 255)
basic.pause(15)
}
function plotAA(_x: number, _y: number, _brightness: number, _scale: number) {
/*
* Draw a dot without "staircase effect"/aliasing.
* _x : from 0 to 4 but in _scale scale
* _y : from 0 to 4 but in _scale scale
* _brightness : led brightness
*
* Half of _scale is a number of [0,0] pixel center
*/
// Keep in mind that numbers are 32 bit signed integers!
let px = (_x + _scale) % _scale * 2 // subpixel x position
let py = (_y + _scale) % _scale * 2 // subpixel y position
// It should be _x / _scale, but to fix a bug I use this...
let rx = (_x + _scale) / _scale - 1 // real x position (ceil)
let ry = (_y + _scale) / _scale - 1 // real y position (ceil)
led.plotBrightness(rx, ry, ((_scale - py) + /* Add something here? */0) * _brightness / _scale)
led.plotBrightness(rx + 1, ry, ((_scale - py)) * _brightness / _scale)
led.plotBrightness(rx, ry + 1, (py) * _brightness / _scale)
led.plotBrightness(rx + 1, ry + 1, (py) * _brightness / _scale)
}
要测试此代码,请使用https://makecode.microbit.org/(在顶部选择 JavaScript)。
如何让它在垂直和水平方向同时平滑移动?