Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Primary micro-bit code
- let lifes = 0
- let vals: string[] = []
- let X = 0
- let Y = 0
- radio.setGroup(18)
- serial.setTxBufferSize(128)
- serial.setRxBufferSize(128)
- let values: { [key: string]: string } = {};
- control.inBackground(()=> {
- serial.onDataReceived("d:g", function () {
- vals = serial.readBuffer(75).toString().split('\n')
- vals.forEach(pair => {
- const [key, value] = pair.split(':');
- values[key] = value;
- });
- if (values.d != undefined) {
- radio.sendString(values.d.charAt(0))
- }
- if (values.l != undefined) {
- basic.showString(values.l)
- }
- })
- });
- radio.onReceivedString(function (control) {
- // basic.showString(control);
- serial.writeLine(control)
- });
- function getGameState(){
- return values.d;
- }
- basic.forever(()=>{
- if (getGameState() === 'g') {
- controlGame(values);
- }
- })
- function controlGame (vals:{[key:string] : string}) {
- X = input.acceleration(Dimension.X)
- Y = input.acceleration(Dimension.Y)
- if (Y > 250) {
- thrustOn()
- } else {
- thrustOff()
- }
- if (X < -300) {
- // left
- rotateLeft()
- } else if (X > 300) {
- // right
- rotateRight()
- } else {
- halt()
- }
- if (input.buttonIsPressed(Button.A)) {
- serial.writeLine("a:1")
- } else {
- serial.writeLine("a:0")
- }
- if (input.buttonIsPressed(Button.B)) {
- serial.writeLine("b:1")
- } else {
- serial.writeLine("b:0")
- }
- }
- function thrustOn() {
- serial.writeLine("t:-1")
- }
- function thrustOff() {
- serial.writeLine("t:0")
- }
- function rotateLeft() {
- serial.writeLine("l:1")
- serial.writeLine("r:0")
- }
- function halt() {
- serial.writeLine("l:0")
- serial.writeLine("r:0")
- serial.writeLine("t:0")
- }
- function rotateRight() {
- serial.writeLine("l:0")
- serial.writeLine("r:1")
- }
- //Secondary micro-bit code
- radio.setGroup(18);
- radio.onReceivedNumber((number)=>{
- basic.showNumber(number)
- });
- radio.onReceivedString((value)=>{
- basic.showString(value);
- })
- let paused = false;
- let shield = false;
- basic.forever(()=>{
- if(input.buttonIsPressed(Button.A)){
- radio.sendString("s:1")
- }else{
- radio.sendString("s:0")
- }
- if(input.buttonIsPressed(Button.B)){
- radio.sendString("u:1")
- }else{
- radio.sendString("u:0")
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement