微軟 micro:bit 創意課程系列--Karel the LED

2017-09-09 18:21

微軟 micro:bit Javascript 創意課程系列--Karel the LED

Help Karel make LED art!

 

幫助卡雷爾做LED藝術!

卡雷爾LED不能右轉,但他可以做一些偉大的LED藝術!

這個活動的目標是將下面給出的JavaScript代碼下載到一個主板上,然後使用該程序來引入新的學生到Micro:bit, 這次學生不會做編碼。

How to play

怎麼玩

•向左轉 Turn Left

沒有畫出任何東西只是改變了卡雷爾(閃光led)面臨的方向

•B按鈕向前移動 Move Forward

將卡雷爾向前移動一步,將LED留在他身後

•搖動跳 Jump

將卡雷爾向前移動一步,而不將LED留在他身後

•A + B按鈕隱藏卡雷爾 Hide Karel

展示或隱藏卡雷爾(閃爍的LED),一旦您的作品完成,就可以使用

•復位按鈕清除電路板並重新啟動  Clear the board and restart

重新啟動程序並清除板

請注意,除了重新啟動之外,沒有辦法擦除。

看看你是否可以使用A,B和搖動使每個模式下面。 一旦你完成了挑戰,A和B同時隱藏Karel。 對於您設計的圖案,您決定要打開哪些LED,然後使用Karel進行設計。

Spiral

Right turn

Eyes

Smile

Check

First letter of your name

Figure out how to make the first letter of your name with the LEDs.

Your design!

Make something fun!

Thanks for playing with Karel the LED!

/**

 * Karel the LED

 *

 * Copy this code into the JavaScript editor and program the board.

 * Use the instructions above to complete the challenges.

 */

basic.forever(() => {

    if (board.isKarelActive) {

        led.toggle(board.karelX, board.karelY)

        basic.pause(500)

    }

})

input.onButtonPressed(Button.A, () => {

    board.pressedA();

    updateLeds();

})

input.onButtonPressed(Button.B, () => {

    board.pressedB();

    updateLeds();

})

input.onGesture(Gesture.Shake, () => {

    board.shake();

    updateLeds();

})

input.onButtonPressed(Button.AB, () => {

    board.pressedAB();

    updateLeds();

})

function updateLeds() {

    for (let j = 0; j < 5; j++) {

        for (let k = 0; k < 5; k++) {

            if (board.ledState[j][k]) {

                led.plot(k, j);

            } else {

                led.unplot(k, j);

            }

        }

    }

}

const board = new Board();

enum Direction {

    UP = 0,

    LEFT,

    DOWN,

    RIGHT

}

class Board {

    public isKarelActive: boolean;

    public karelX: number;

    public karelY: number;

 

    public ledState: Array < Array < boolean >>;

    private karelDirection: Direction;

 

    constructor() {

        this.isKarelActive = true;

        this.karelX = 2;

        this.karelY = 2;

        this.karelDirection = Direction.UP;

        this.ledState =[];

        for (let i = 0; i < 5; i++) {

            this.ledState.push([false, false, false, false, false]);

        }

    }

 

    pressedA() {

        if (!this.isKarelActive) {

            return;

        }

        this.karelDirection = (this.karelDirection + 1) % 4;

    }

 

    pressedB() {

        if (!this.isKarelActive) {

            return;

        }

        this.ledState[this.karelY][this.karelX] = true;

        this.moveKarel()

    }

 

    shake() {

        if (!this.isKarelActive) {

            return;

        }

        this.moveKarel()

    }

 

    private moveKarel() {

        if (!this.isKarelActive) {

            return;

        }

        switch (this.karelDirection) {

            case Direction.UP:

            if (this.karelY > 0) {

            this.karelY -= 1;

        }

            break;

            case Direction.LEFT:

            if (this.karelX > 0) {

            this.karelX -= 1;

        }

            break;

            case Direction.DOWN:

            if (this.karelY < 4) {

            this.karelY += 1;

        }

            break;

            case Direction.RIGHT:

            if (this.karelX < 4) {

            this.karelX += 1;

        }

            break;

        }

    }

 

    pressedAB() {

        this.isKarelActive = !this.isKarelActive;

    }

 

}about the authors

This project was contributed by Dr. David Fisher a professor at Rose-Hulman Institute of Technology. Dr. Fisher loves educational robotics and runs various outreach programming activities, including a summar camp, called Connecting with Code, which gives a micro:bit to each participant.

該項目由Rose-Hulman理工學院教授David Fisher博士提供。 費雪博士喜歡教育機器人,並開展各種外聯計劃活動,包括一個匯總營,稱為“與代碼連接”,它為每個參與者提供了一個微觀的位置。

 

Microbit 台灣 商店

Microbit 中文 課程 : Python , Javascript, 物聯網

              中國

Edit this page on GitHub

 

—————

返回