From 6293e8f34256a4a398e58d17eeb08f0eeb491542 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 26 Oct 2018 00:10:41 -0700 Subject: [PATCH] restoring some docs (#1533) --- docs/projects/micro-coin.md | 87 ++++++++++++++++++++++++++++++ docs/projects/radio-games.md | 5 ++ docs/reference/images.md | 3 +- docs/reference/images/pixel.md | 42 ++++++++++++++- docs/reference/images/set-pixel.md | 40 +++++++++++++- 5 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 docs/projects/micro-coin.md diff --git a/docs/projects/micro-coin.md b/docs/projects/micro-coin.md new file mode 100644 index 00000000..8f70be35 --- /dev/null +++ b/docs/projects/micro-coin.md @@ -0,0 +1,87 @@ +# micro:coin + +## ~ avatar + +Have you heard about BitCoin and all those new Crypto currencies? Well micro:bit has **micro:coin** now! + +## ~ + +## How does a @boardname@ make coins? + +Each @boardname@ contains a **block chain**, a sequence of **blocks**, that is public and can't be modified. Each block represents a **coin**. To mine new coins, the user shakes +the @boardname@ and, if they are in luck, their coin added to the chain as a new block! +Once the block is added, it is broadcasted to the other @boardname@ (the block chain is public and can't be modified so it's ok to share it). Other @boardname@s receive the block, validate the transaction and update their block chain as needed. + +Pressing ``A`` shows the number of block you added to the chain, that's your score. +Pressing ``B`` shows you the length of the chain. + +Happy mining! + +## Coins, blocks, chains + +A _block chain_ is a list of _blocks_ that record transactions of a crypto-currency like BitCoin. A block might contain information like the time it was created (mined) and who mined it. The most important part of the block is it's _hash_. This is a special number made from the information in the last block of the block list combined with the hash number of previous block in the list. The new block contains information for the current transaction and this new hash number. The new block is added to the list of previous blocks. This list is then transmitted to the crypto currency network. It's really hard (like impossible) to tamper or forge a hash which allows the block chain to be transmitted publically. + +## ~ hint + +Build yourself a [@boardname@ wallet](/projects/wallet) to hold your coins! + +## ~ + +## Code + +The code uses blocks from [radio-blockchain](https://makecode.microbit.org/pkg/microsoft/pxt-radio-blockchain) package. + +* Click on **Advanced**, then **Extensions** +* search for **blockchain** and add **radio-blockchain** + +```blocks +// shaking is mining... +input.onGesture(Gesture.Shake, () => { + led.stopAnimation() + basic.clearScreen() + basic.pause(200) // display a short pause + if (Math.randomRange(0, 2) == 0) { // 30% chances to add a transaction + // we found a coin!!! + blockchain.addBlock(1); + basic.showIcon(IconNames.Diamond); + } else { + // missed! + basic.showIcon(IconNames.Asleep); + } +}) + +// show my coins +input.onButtonPressed(Button.A, () => { + led.stopAnimation() + let coins = blockchain.valuesFrom(blockchain.id()).length; + basic.showNumber(coins); + basic.showString("COINS"); +}) + +// show the block chain size +input.onButtonPressed(Button.B, () => { + led.stopAnimation() + basic.showNumber(blockchain.length()); + basic.showString("BLOCKS"); +}) + +// instructions on how to play +basic.showString("A=COINS B=CHAIN SHAKE=MINE") +``` + +## How does the blockchain code work? + +The [radio-blockchain](https://makecode.microbit.org/pkg/microsoft/pxt-radio-blockchain) package uses radio to transmit blocks between @boardname@s. You can see how this package works by reading the JavaScript sources at https://github.com/microsoft/pxt-radio-blockchain. To go right to the blockchain code, see this file: + +[main.ts](https://github.com/Microsoft/pxt-radio-blockchain/blob/master/main.ts) - contains the complete JavaScript source for the blockhain. Have a good read! + +## References + +* ["A blockchain in 200 lines of code"](https://medium.com/@lhartikk/a-blockchain-in-200-lines-of-code-963cc1cc0e54). _medium.com_. +* ["Let’s Build the Tiniest Blockchain"](https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b). _medium.com_. +* ["How does the blockchain work?"](https://medium.com/@micheledaliessi/how-does-the-blockchain-work-98c8cd01d2ae). _medium.com_. + +```package +radio +radio-blockchain=github:Microsoft/pxt-radio-blockchain#v0.1.4 +``` \ No newline at end of file diff --git a/docs/projects/radio-games.md b/docs/projects/radio-games.md index a5faf475..b7ffc09f 100644 --- a/docs/projects/radio-games.md +++ b/docs/projects/radio-games.md @@ -48,6 +48,11 @@ Here are some cool projects that you can build with your @boardname@! "url": "/projects/rps-teams", "description": "The Rock-Paper-Scissors game with lots and lots of players.", "imageUrl": "/static/mb/projects/rpsteams.png" +}, { + "name": "Micro:Coin", + "url": "/projects/micro-coin", + "description": "A block-chain for micro:bit", + "imageUrl": "/static/mb/projects/micro-coin.png" }, { "name": "Infection", "url": "/projects/infection", diff --git a/docs/reference/images.md b/docs/reference/images.md index 51dfc1cf..9ea3794a 100644 --- a/docs/reference/images.md +++ b/docs/reference/images.md @@ -28,4 +28,5 @@ images.arrowNumber(ArrowNames.North) [createImage](/reference/images/create-image), [createBigImage](/reference/images/create-big-image), [showImage](/reference/images/show-image), [scrollImage](/reference/images/scroll-image), -[arrowImage](/reference/images/arrow-image), [iconImage](/reference/images/icon-image), [arrowNumber](/reference/images/arrow-number) +[arrowImage](/reference/images/arrow-image), [iconImage](/reference/images/icon-image), [arrowNumber](/reference/images/arrow-number), +[pixel](/reference/images/pixel), [set-pixel](/reference/images/set-pixel) diff --git a/docs/reference/images/pixel.md b/docs/reference/images/pixel.md index 3bf86835..0d7a3c2f 100644 --- a/docs/reference/images/pixel.md +++ b/docs/reference/images/pixel.md @@ -1 +1,41 @@ -TODO: Fix for microbit master \ No newline at end of file +# Pixel + +Get the state of a pixel in an [Image](/reference/images/image). + + +## JavaScript + +```sig +let item: Image = null; +item.pixel(0, 0) +``` +## Parameters + +* x - [Number](/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/reference/images/image) +* y - [Number](/types/number); the *y coordinate* or vertical position of a pixel in an [image](/reference/images/image) + +## x, y coordinates? + +To figure out the ``x``, ``y`` coordinates, see [LED screen](/device/screen). + +## Returns + +* [Boolean](/blocks/logic/boolean) - `true` for on and `false` for off + +## Example + +This example gets the state of pixel `0, 0` in the `img` variable: + +```blocks +let img = images.createImage(` +. . # . . . . . . . +. # . # . . . # . . +. . # . . . . . . . +. # . # . . . # . .. . # . . . . . . . +`) +let state = img.pixel(0, 0) +``` + +## See also + +[set pixel](/reference/images/set-pixel), [show image](/reference/images/show-image), [image](/reference/images/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image) \ No newline at end of file diff --git a/docs/reference/images/set-pixel.md b/docs/reference/images/set-pixel.md index 3bf86835..5a06d3de 100644 --- a/docs/reference/images/set-pixel.md +++ b/docs/reference/images/set-pixel.md @@ -1 +1,39 @@ -TODO: Fix for microbit master \ No newline at end of file +# Set Pixel + +Set the on/off state of pixel in an [Image](/reference/images/image). + +## JavaScript + +```sig +let item: Image = null; +item.setPixel(0, 0, true) +``` + +## Parameters + +* x - [Number](/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/reference/images/image) +* y - [Number](/types/number); the *y coordinate* or vertical position of a pixel in an [image](/reference/images/image) +* value -[Boolean](/blocks/logic/boolean); the on/off state of a pixel; `true` for on, `false` for off + +## x, y coordinates? + +To figure out the ``x``, ``y`` coordinates, see [LED screen](/device/screen). + +## Example + +The following example creates an image and stores it in the `img` variable. The `set pixel` function sets the center pixel off, before `img` is shown using `show image`. + +```blocks +let img = images.createImage(` +. . # . . +. # . # . +. . # . . +. # . # . +. . # . . +`) +img.setPixel(2, 2, false) +img.showImage(0) +``` + ## See also + + [pixel](/reference/images/pixel), [show image](/reference/images/show-image), [image](/reference/images/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image) \ No newline at end of file