分形浏览

MakeCode Arcade 游戏编程
回复
头像
shaoziyang
帖子: 3917
注册时间: 2019年 10月 21日 13:48

分形浏览

#1

帖子 shaoziyang »

一个简单的分形查看程序,可以缩放、移动。

功能:
  • A键,放大
  • B键,缩小
  • 上下左右,移动
缩放开始和结束时有声音提示。


代码

图形编程比较复杂,图形块比较多,所以只贴出了代码,可以将代码粘贴到Arcade的Javascript编辑器中,然后切换回图形方式。

代码: 全选

function calc (x: number, y: number) {
    a = x
    b = y
    n = 0
    for (let p = 0; p <= 220; p++) {
        tx = x * x - y * y + a
        ty = 2 * x * y + b
        if (tx * tx + ty * ty > 4) {
            n = p
            return
        }
        x = tx
        y = ty
    }
}
function draw (cx: number, cy: number, zoom: number) {
    music.playTone(784, music.beat(BeatFraction.Sixteenth))
    for (let i = 0; i <= scene.screenWidth(); i++) {
        for (let j = 0; j <= scene.screenHeight(); j++) {
            calc(cx + 4 * (i - scene.screenWidth() / 2) / scene.screenWidth() / zoom, cy + 4 * (j - scene.screenHeight() / 2) / scene.screenHeight() / zoom)
            scene.backgroundImage().setPixel(i, j, n)
        }
    }
    music.playTone(880, music.beat(BeatFraction.Sixteenth))
}
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
    zoom = zoom * 2
    draw(cx, cy, zoom)
})
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
    zoom = zoom / 2
    draw(cx, cy, zoom)
})
controller.left.onEvent(ControllerButtonEvent.Pressed, function () {
    cx = cx - 1 / 8 / zoom
    draw(cx, cy, zoom)
})
controller.up.onEvent(ControllerButtonEvent.Pressed, function () {
    cy = cy - 1 / 8 / zoom
    draw(cx, cy, zoom)
})
controller.down.onEvent(ControllerButtonEvent.Pressed, function () {
    cy = cy + 1 / 8 / zoom
    draw(cx, cy, zoom)
})
controller.right.onEvent(ControllerButtonEvent.Pressed, function () {
    cx = cx + 1 / 8 / zoom
    draw(cx, cy, zoom)
})
let y = 0
let x = 0
let ty = 0
let tx = 0
let n = 0
let b = 0
let a = 0
let cy = 0
let cx = 0
let zoom = 0
zoom = 1
draw(cx, cy, zoom)
动画演示
 
mandelbrot演示.gif
mandelbrot演示.gif (40.32 KiB) 查看 2448 次
 

在线编辑

头像
shaoziyang
帖子: 3917
注册时间: 2019年 10月 21日 13:48

Re: 分形浏览

#2

帖子 shaoziyang »

受到MCU性能限制,实际显示速度还是有点慢,可以将循环的次数减少,比如将220改为30,速度就会快一些了。

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章