Skip to content

Instantly share code, notes, and snippets.

@oschettler
Last active June 25, 2020 21:40
Show Gist options
  • Save oschettler/43a4cc2678a7ddf80794a27746927d83 to your computer and use it in GitHub Desktop.
Save oschettler/43a4cc2678a7ddf80794a27746927d83 to your computer and use it in GitHub Desktop.
A small Microblocks game
module main
author unknown
version 1 0
description ''
variables x y px verloren punkte
spec ' ' 'berechne' 'berechne'
spec ' ' 'nerviger ton' 'nerviger ton'
spec ' ' 'runde' 'runde'
spec 'r' 'scaliere' 'scaliere _' 'auto' 10
spec 'r' 'treffer' 'treffer'
spec ' ' 'verloren' 'verloren'
to berechne {
y += 1
x += (random -1 1)
x = (minimum 5 (maximum 1 x))
px = (scaliere ('[sensors:tiltX]'))
}
to 'nerviger ton' {
'play frequency' ((y * 20) + 400) 50
}
to runde {
punkte += 1
x = (random 1 5)
y = 0
repeat 5 {
'nerviger ton'
'[display:mbDisplayOff]'
berechne
if (treffer) {
verloren
} else {
'[display:mbPlot]' x y
'[display:mbPlot]' px 5
}
waitMillis 500
}
}
to scaliere messwert {
return (((messwert + 160) / 40) + 1)
}
to treffer {
return (and (y == 5) (px == x))
}
to verloren {
'play tone' 'G' 0 50
'play tone' 'C' 0 100
'[display:mbDisplay]' 18284881
waitMillis 1000
scroll_number punkte
verloren = (booleanConstant true)
}
script 50 50 {
whenButtonPressed 'A'
verloren = (booleanConstant false)
punkte = 0
repeatUntil verloren {
runde
}
}
script 345 63 {
to runde {}
}
script 619 69 {
whenStarted
'Calliope set speaker' true
}
script 610 163 {
to berechne {}
}
script 70 346 {
to verloren {}
}
script 559 421 {
to scaliere {}
}
script 288 500 {
to treffer {}
}
script 425 597 {
to 'nerviger ton' {}
}
module 'Basic Sensors' Input
author MicroBlocks
version 1 0
tags tilt acceleration light sensor
description 'Provides blocks to read tilt in the three axes, acceleration, temperature and light level. Many boards come with this particular set of sensors, such as the micro:bit, the Circuit Playground Express, the Calliope or the Citilab ED1.'
spec 'r' '[sensors:tiltX]' 'tilt x'
spec 'r' '[sensors:tiltY]' 'tilt y'
spec 'r' '[sensors:tiltZ]' 'tilt z'
spec 'r' '[sensors:acceleration]' 'acceleration'
spec 'r' '[display:lightLevel]' 'light level'
spec 'r' '[sensors:temperature]' 'temperature (°C)'
module Calliope
author MicroBlocks
version 1 0
tags calliope led sound speaker
description 'Blocks to control the LED and speaker in Calliope boards.'
spec ' ' 'Calliope set LED' 'Calliope set LED red _ green _ blue _' 'auto auto auto' 0 0 100
spec ' ' 'Calliope set speaker' 'Calliope set speaker _' 'bool' true
spec 'r' 'Calliope sound' 'Calliope loudness'
to 'Calliope set LED' red green blue {
'[display:neoPixelSetPin]' -1 false
local 'rgb' ((((255 * red) / 100) & 255) << 16)
rgb = (rgb | ((((255 * green) / 100) & 255) << 8))
rgb = (rgb | (((255 * blue) / 100) & 255))
'[display:neoPixelSend]' rgb
}
to 'Calliope set speaker' onOff {
digitalWriteOp 23 true
digitalWriteOp 24 onOff
digitalWriteOp 25 (not onOff)
}
to 'Calliope sound' {
local 'min' 10000
local 'max' 0
local 'end_msecs' ((millisOp) + 50)
repeatUntil ((millisOp) > end_msecs) {
local 'sample' (analogReadOp 0)
if (sample < min) {
min = sample
}
if (sample > max) {
max = sample
}
}
return ((100 * (max - min)) / 1020)
}
module 'LED Display' Output
author MicroBlocks
version 1 0
tags pixel matrix led tft
description 'Display primitives for the 5x5 LED display on the BBC micro:bit, Calliope mini and M5Atom Matrix. Boards with TFT displays (such as the Citilab ED1 or the M5Stack family) also support this primitives in a simulated "fat pixel" display.'
spec ' ' '[display:mbDisplay]' 'display _' 'microbitDisplay' 15237450
spec ' ' '[display:mbDisplayOff]' 'clear display'
spec ' ' '[display:mbPlot]' 'plot x _ y _' 'num num' 3 3
spec ' ' '[display:mbUnplot]' 'unplot x _ y _' 'num num' 3 3
spec ' ' 'displayCharacter' 'display character _' 'str' 'A'
to displayCharacter s {
'[display:mbDrawShape]' ('[display:mbShapeForLetter]' (at 1 s))
}
module Scrolling Output
author MicroBlocks
version 1 0
depends 'LED Display'
tags animation text pixel matrix led tft
description 'Scroll text across the screen of the 5x5 LED display on the BBC micro:bit, Calliope mini or M5Atom Matrix. Also works on boards with a TFT display by simulating the 5x5 matrix with "fat pixels".'
variables _digit_shapes _digit_shapes_start_index _stop_scrolling_text
spec ' ' 'scroll_text' 'scroll text _ : pausing _ ms' 'str num' 'HELLO ROSA!' 100
spec ' ' 'scroll_number' 'scroll number _ : pausing _ ms' 'num num' 123 100
spec ' ' '_covert_to_shapes' '_covert_to_shapes _' 'num' 42
spec ' ' '_scroll_digit_shapes' '_scroll_digit_shapes _' 'num' 100
spec ' ' 'stopScrollingText' 'stop scrolling'
to '_covert_to_shapes' n {
if (_digit_shapes == 0) {_digit_shapes = (newList 12)}
fillList _digit_shapes ('[display:mbShapeForLetter]' ' ')
local 'index' (size _digit_shapes)
local 'isNegative' (booleanConstant false)
if (n < 0) {
isNegative = (booleanConstant true)
n = (0 - n)
} (n == 0) {
atPut index _digit_shapes ('[display:mbShapeForLetter]' '0')
_digit_shapes_start_index = index
}
repeatUntil (n == 0) {
local 'digit' (n % 10)
atPut index _digit_shapes ('[display:mbShapeForLetter]' (48 + digit))
index += -1
n = (n / 10)
}
if isNegative {
atPut index _digit_shapes ('[display:mbShapeForLetter]' '-')
index += -1
}
_digit_shapes_start_index = index
}
to '_scroll_digit_shapes' delay {
_stop_scrolling_text = (booleanConstant false)
local 'prevShape' ('[display:mbShapeForLetter]' ' ')
local 'i' _digit_shapes_start_index
local 'end' ((size _digit_shapes) + 1)
repeatUntil (i > end) {
local 'shape' ('[display:mbShapeForLetter]' ' ')
if (i < end) {
shape = (at i _digit_shapes)
}
for j 5 {
local 'x' (1 - j)
'[display:mbDrawShape]' prevShape x 1
'[display:mbDrawShape]' shape (x + 5) 1
waitMillis delay
if _stop_scrolling_text {
'[display:mbDisplayOff]'
return 0
}
}
prevShape = shape
i += 1
}
}
to scroll_number n optionalDelay {
delay = 100
if ((pushArgCount) > 1) {
delay = optionalDelay
}
'_covert_to_shapes' n
'_scroll_digit_shapes' delay
}
to scroll_text text optionalDelay {
delay = 100
if ((pushArgCount) > 1) {
delay = optionalDelay
}
_stop_scrolling_text = (booleanConstant false)
local 'length' (size text)
for position ((length * 6) + 6) {
if _stop_scrolling_text {return 0}
for i length {
'[display:mbDrawShape]' ('[display:mbShapeForLetter]' ('[data:unicodeAt]' i text)) (((i * 6) + 2) - position) 1
}
waitMillis delay
}
}
to stopScrollingText {
_stop_scrolling_text = (booleanConstant true)
waitMillis 10
'[display:mbDisplayOff]'
}
module Tone Output
author MicroBlocks
version 1 1
tags tone sound music audio note speaker
description 'Audio tone generation. Make music with MicroBlocks!'
variables _tonePin _toneInitalized _toneLoopOverhead
spec ' ' 'play tone' 'play note _ octave _ for _ ms' 'auto num num' 'C' 0 500
spec ' ' 'playMIDIKey' 'play midi key _ for _ ms' 'num num' 60 500
spec ' ' 'play frequency' 'play frequency _ for _ ms' 'num num' 261 500
spec ' ' 'attach buzzer to pin' 'attach buzzer to pin _' 'auto' ''
spec 'r' '_measureLoopOverhead' '_measureLoopOverhead'
spec 'r' '_baseFreqForNote' '_baseFreqForNote _' 'auto' 'c'
spec 'r' '_baseFreqForSharpOrFlat' '_baseFreqForSharpOrFlat _' 'auto' 'c#'
spec 'r' '_baseFreqForSemitone' '_baseFreqForSemitone _' 'num' 0
spec ' ' '_toneLoop' '_toneLoop _ for _ ms' 'num num' 440000 100
to '_baseFreqForNote' note {
comment 'Return the frequency for the given note in the middle-C octave
scaled by 1000. For example, return 440000 (440Hz) for A.
Note names may be upper or lower case. Note names
may be followed by # for a sharp or b for a flat.'
if (or (note == 'c') (note == 'C')) {
return 261626
} (or (note == 'd') (note == 'D')) {
return 293665
} (or (note == 'e') (note == 'E')) {
return 329628
} (or (note == 'f') (note == 'F')) {
return 349228
} (or (note == 'g') (note == 'G')) {
return 391995
} (or (note == 'a') (note == 'A')) {
return 440000
} (or (note == 'b') (note == 'B')) {
return 493883
}
return ('_baseFreqForSharpOrFlat' note)
}
to '_baseFreqForSemitone' semitone {
if (0 == semitone) {
return 261626
} (1 == semitone) {
return 277183
} (2 == semitone) {
return 293665
} (3 == semitone) {
return 311127
} (4 == semitone) {
return 329628
} (5 == semitone) {
return 349228
} (6 == semitone) {
return 369994
} (7 == semitone) {
return 391995
} (8 == semitone) {
return 415305
} (9 == semitone) {
return 440000
} (10 == semitone) {
return 466164
} (11 == semitone) {
return 493883
}
}
to '_baseFreqForSharpOrFlat' note {
comment 'Return the frequency for the given sharp or flat note in the
middle-C octave scaled by 1000. Only handles black keys.
Thus, you can''t write E# to mean F.'
if (or (or (note == 'c#') (note == 'C#')) (or (note == 'db') (note == 'Db'))) {
return 277183
} (or (or (note == 'd#') (note == 'D#')) (or (note == 'eb') (note == 'Eb'))) {
return 311127
} (or (or (note == 'f#') (note == 'F#')) (or (note == 'gb') (note == 'Gb'))) {
return 369994
} (or (or (note == 'g#') (note == 'G#')) (or (note == 'ab') (note == 'Ab'))) {
return 415305
} (or (or (note == 'a#') (note == 'A#')) (or (note == 'bb') (note == 'Bb'))) {
return 466164
}
comment 'Unrecognized note names map to 0.1 Hz, which is inaudible.
This helps users find typos in their tunes.'
return 100
}
to '_measureLoopOverhead' {
comment 'Measure the loop overhead on this device'
local 'halfCycle' 100
local 'startT' (microsOp)
repeat 100 {
digitalWriteOp _tonePin false
waitMicros halfCycle
digitalWriteOp _tonePin false
waitMicros halfCycle
}
local 'usecs' ((microsOp) - startT)
return ((usecs - 20000) / 200)
}
to '_toneLoop' scaledFreq ms {
if (_toneInitalized == 0) {'attach buzzer to pin' ''}
if ('[io:hasTone]') {
'[io:playTone]' _tonePin (scaledFreq / 1000)
waitMillis ms
'[io:playTone]' _tonePin 0
} else {
local 'halfCycle' ((500000000 / scaledFreq) - _toneLoopOverhead)
local 'cycles' ((ms * 500) / halfCycle)
repeat cycles {
digitalWriteOp _tonePin true
waitMicros halfCycle
digitalWriteOp _tonePin false
waitMicros halfCycle
}
}
}
to 'attach buzzer to pin' pinNumber {
if (pinNumber == '') {
comment 'Pin number not specified; use default pin for this device'
if ((boardType) == 'Citilab ED1') {
_tonePin = 26
} ((boardType) == 'M5Stack-Core') {
_tonePin = 25
} ((boardType) == 'M5StickC') {
_tonePin = 26
} ((boardType) == 'Calliope') {
digitalWriteOp 23 true
digitalWriteOp 24 true
_tonePin = 25
} else {
_tonePin = 0
}
} else {
_tonePin = pinNumber
}
_toneLoopOverhead = ('_measureLoopOverhead')
_toneInitalized = (booleanConstant true)
}
to 'play frequency' freq ms {
'_toneLoop' (freq * 1000) ms
}
to 'play tone' note octave ms {
local 'freq' ('_baseFreqForNote' note)
if (octave < 0) {
repeat (absoluteValue octave) {
freq = (freq / 2)
}
}
repeat octave {
freq = (freq * 2)
}
'_toneLoop' freq ms
}
to playMIDIKey key ms {
local 'freq' ('_baseFreqForSemitone' (key % 12))
local 'octave' ((key / 12) - 5)
if (octave < 0) {
repeat (absoluteValue octave) {
freq = (freq / 2)
}
}
repeat octave {
freq = (freq * 2)
}
'_toneLoop' freq ms
}
module main
author unknown
version 1 0
description ''
variables x y px verloren punkte
spec 'r' 'scaliere' 'scaliere _' 'auto' 10
to scaliere messwert {
return (((messwert + 160) / 40) + 1)
}
script 50 50 {
whenButtonPressed 'A'
verloren = (booleanConstant false)
punkte = 0
repeatUntil verloren {
punkte += 1
x = (random 1 5)
y = 0
repeat 5 {
'[display:mbDisplayOff]'
y += 1
x += (random -1 1)
x = (minimum 5 (maximum 1 x))
px = (scaliere ('[sensors:tiltX]'))
if (and (y == 5) (px == x)) {
verloren = (booleanConstant true)
'[display:mbDisplay]' 18284881
waitMillis 1000
scroll_number punkte
} else {
'[display:mbPlot]' x y
'[display:mbPlot]' px 5
}
waitMillis 500
}
}
}
script 395 432 {
to scaliere {}
}
module 'Basic Sensors' Input
author MicroBlocks
version 1 0
tags tilt acceleration light sensor
description 'Provides blocks to read tilt in the three axes, acceleration, temperature and light level. Many boards come with this particular set of sensors, such as the micro:bit, the Circuit Playground Express, the Calliope or the Citilab ED1.'
spec 'r' '[sensors:tiltX]' 'tilt x'
spec 'r' '[sensors:tiltY]' 'tilt y'
spec 'r' '[sensors:tiltZ]' 'tilt z'
spec 'r' '[sensors:acceleration]' 'acceleration'
spec 'r' '[display:lightLevel]' 'light level'
spec 'r' '[sensors:temperature]' 'temperature (°C)'
module Calliope
author MicroBlocks
version 1 0
tags calliope led sound speaker
description 'Blocks to control the LED and speaker in Calliope boards.'
spec ' ' 'Calliope set LED' 'Calliope set LED red _ green _ blue _' 'auto auto auto' 0 0 100
spec ' ' 'Calliope set speaker' 'Calliope set speaker _' 'bool' true
spec 'r' 'Calliope sound' 'Calliope loudness'
to 'Calliope set LED' red green blue {
'[display:neoPixelSetPin]' -1 false
local 'rgb' ((((255 * red) / 100) & 255) << 16)
rgb = (rgb | ((((255 * green) / 100) & 255) << 8))
rgb = (rgb | (((255 * blue) / 100) & 255))
'[display:neoPixelSend]' rgb
}
to 'Calliope set speaker' onOff {
digitalWriteOp 23 true
digitalWriteOp 24 onOff
digitalWriteOp 25 (not onOff)
}
to 'Calliope sound' {
local 'min' 10000
local 'max' 0
local 'end_msecs' ((millisOp) + 50)
repeatUntil ((millisOp) > end_msecs) {
local 'sample' (analogReadOp 0)
if (sample < min) {
min = sample
}
if (sample > max) {
max = sample
}
}
return ((100 * (max - min)) / 1020)
}
module 'LED Display' Output
author MicroBlocks
version 1 0
tags pixel matrix led tft
description 'Display primitives for the 5x5 LED display on the BBC micro:bit, Calliope mini and M5Atom Matrix. Boards with TFT displays (such as the Citilab ED1 or the M5Stack family) also support this primitives in a simulated "fat pixel" display.'
spec ' ' '[display:mbDisplay]' 'display _' 'microbitDisplay' 15237450
spec ' ' '[display:mbDisplayOff]' 'clear display'
spec ' ' '[display:mbPlot]' 'plot x _ y _' 'num num' 3 3
spec ' ' '[display:mbUnplot]' 'unplot x _ y _' 'num num' 3 3
spec ' ' 'displayCharacter' 'display character _' 'str' 'A'
to displayCharacter s {
'[display:mbDrawShape]' ('[display:mbShapeForLetter]' (at 1 s))
}
module Scrolling Output
author MicroBlocks
version 1 0
depends 'LED Display'
tags animation text pixel matrix led tft
description 'Scroll text across the screen of the 5x5 LED display on the BBC micro:bit, Calliope mini or M5Atom Matrix. Also works on boards with a TFT display by simulating the 5x5 matrix with "fat pixels".'
variables _digit_shapes _digit_shapes_start_index _stop_scrolling_text
spec ' ' 'scroll_text' 'scroll text _ : pausing _ ms' 'str num' 'HELLO ROSA!' 100
spec ' ' 'scroll_number' 'scroll number _ : pausing _ ms' 'num num' 123 100
spec ' ' '_covert_to_shapes' '_covert_to_shapes _' 'num' 42
spec ' ' '_scroll_digit_shapes' '_scroll_digit_shapes _' 'num' 100
spec ' ' 'stopScrollingText' 'stop scrolling'
to '_covert_to_shapes' n {
if (_digit_shapes == 0) {_digit_shapes = (newList 12)}
fillList _digit_shapes ('[display:mbShapeForLetter]' ' ')
local 'index' (size _digit_shapes)
local 'isNegative' (booleanConstant false)
if (n < 0) {
isNegative = (booleanConstant true)
n = (0 - n)
} (n == 0) {
atPut index _digit_shapes ('[display:mbShapeForLetter]' '0')
_digit_shapes_start_index = index
}
repeatUntil (n == 0) {
local 'digit' (n % 10)
atPut index _digit_shapes ('[display:mbShapeForLetter]' (48 + digit))
index += -1
n = (n / 10)
}
if isNegative {
atPut index _digit_shapes ('[display:mbShapeForLetter]' '-')
index += -1
}
_digit_shapes_start_index = index
}
to '_scroll_digit_shapes' delay {
_stop_scrolling_text = (booleanConstant false)
local 'prevShape' ('[display:mbShapeForLetter]' ' ')
local 'i' _digit_shapes_start_index
local 'end' ((size _digit_shapes) + 1)
repeatUntil (i > end) {
local 'shape' ('[display:mbShapeForLetter]' ' ')
if (i < end) {
shape = (at i _digit_shapes)
}
for j 5 {
local 'x' (1 - j)
'[display:mbDrawShape]' prevShape x 1
'[display:mbDrawShape]' shape (x + 5) 1
waitMillis delay
if _stop_scrolling_text {
'[display:mbDisplayOff]'
return 0
}
}
prevShape = shape
i += 1
}
}
to scroll_number n optionalDelay {
delay = 100
if ((pushArgCount) > 1) {
delay = optionalDelay
}
'_covert_to_shapes' n
'_scroll_digit_shapes' delay
}
to scroll_text text optionalDelay {
delay = 100
if ((pushArgCount) > 1) {
delay = optionalDelay
}
_stop_scrolling_text = (booleanConstant false)
local 'length' (size text)
for position ((length * 6) + 6) {
if _stop_scrolling_text {return 0}
for i length {
'[display:mbDrawShape]' ('[display:mbShapeForLetter]' ('[data:unicodeAt]' i text)) (((i * 6) + 2) - position) 1
}
waitMillis delay
}
}
to stopScrollingText {
_stop_scrolling_text = (booleanConstant true)
waitMillis 10
'[display:mbDisplayOff]'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment