主題
Micro:bit射方塊連線對打
一.主題說明
利用兩片micro:bit來玩連線射方塊對打
二.材料說明
1.BBC Micro:bit x 2
三.Micro:bit 接線說明
無
四.基本程式結構說明 (使用中文)
STEP 1
AB鍵同時按發球
STEP 2
A鍵讓plate向左,B鍵讓plate向右
STEP 3
根據球的方向去移動球
當碰到左右牆壁時,反彈
當碰到反射板時,反彈
當碰到最底邊時,就輸了
STEP 4
當球從一個板子射到另一個板子時,或是輸了,會傳輸資料
str: 球的方向與座標,或是輸贏
val: 方向值或座標值
STEP 5
每次迴圈會先把led燈全部熄滅,然後更新球的座標,再把led燈點亮
五.程式碼
/*
* demo/射方塊連線對打
* 請用兩個micro:bit板子載入同一個hex檔才能連線
* 請將這個code複製至https://makecode.microbit.org/,放入javascript編輯器後即可使用
*/
let ballDirX = 0 // 球的方向
let ballDirY = 0
let ballX = 0 // 球的座標
let ballY = 0
let playing = false // 1號玩家
let playing2nd = false // 2號玩家
let plate = 0 // 反射板位置(以左邊的點當做座標)
let ballIn = false; // 球是否在我們這邊的板子上
let period = 300 // 每300ms更新一次
/*
* AB鍵同時按發球
*/
input.onButtonPressed(Button.AB, () => {
if (!(playing) && !(playing2nd)) {
ballX = plate
ballY = 3
ballDirX = 1
ballDirY = -1
playing = true
ballIn = true
led.plot(ballX, ballY)
basic.pause(period)
}
})
/*
* A鍵讓plate向左,B鍵讓plate向右
*/
input.onButtonPressed(Button.B, () => {
if (plate < 3) {
led.unplot(plate, 4)
led.unplot(plate + 1, 4)
plate += 1
led.plot(plate, 4)
led.plot(plate + 1, 4)
}
})
input.onButtonPressed(Button.A, () => {
if (plate > 0) {
led.unplot(plate, 4)
led.unplot(plate + 1, 4)
plate -= 1
led.plot(plate, 4)
led.plot(plate + 1, 4)
}
})
/*
* 根據球的方向去移動球
* 當碰到左右牆壁時,反彈
* 當碰到反射板時,反彈
* 當碰到最底邊時,就輸了
*/
function move() {
if (ballX + ballDirX > 4 || ballX + ballDirX < 0) {
ballDirX = -ballDirX
ballX += ballDirX
} else ballX += ballDirX
if (ballY + ballDirY < 0) {
//ballDirY = -ballDirY
//ballY += ballDirY
//return
hitEdge()
led.unplot(ballX, ballY)
ballIn = false
} else if (ballY + ballDirY == 4) {
if (ballX == plate || ballX == plate + 1) {
ballDirY = -ballDirY
ballY += ballDirY
return
}
} else if (ballY + ballDirY > 4) {
radio.sendValue("Win", 1)
basic.showString("You Lose!")
playing = false
playing2nd = false
ballIn = false
}
ballY += ballDirY
return
}
function hitEdge() {
radio.sendValue("ballX", 4 - ballX)
basic.pause(10)
radio.sendValue("ballY", ballY)
basic.pause(10)
radio.sendValue("ballDirX", -ballDirX)
basic.pause(10)
radio.sendValue("ballDirY", -ballDirY)
basic.pause(10)
ballIn = false
}
/*
* 當球從一個板子射到另一個板子時,或是輸了,會傳輸資料
* str: 球的方向與座標,或是輸贏
* val: 方向值或座標值
*/
radio.onDataPacketReceived(({ receivedString: str, receivedNumber: val }) => {
switch (str) {
case "Win":
basic.showString("You Win!")
playing = false
playing2nd = false
ballIn = false
break
case "ballX":
ballX = val
break;
case "ballY":
ballY = val;
break;
case "ballDirX":
ballDirX = val;
break;
case "ballDirY":
ballDirY = val
if (!playing) playing2nd = true
ballIn = true
led.plot(ballX, ballY)
basic.pause(period)
break;
}
})
led.plot(plate, 4)
led.plot(plate + 1, 4)
/*
* 每次迴圈會先把led燈全部熄滅,然後更新球的座標,再把led燈點亮
*/
basic.forever(() => {
if (playing && ballIn) {
basic.pause(period)
led.unplot(plate, 4)
led.unplot(plate + 1, 4)
led.unplot(ballX, ballY)
move()
led.plot(plate, 4)
led.plot(plate + 1, 4)
}
if (playing2nd && ballIn) {
basic.pause(period)
led.unplot(plate, 4)
led.unplot(plate + 1, 4)
led.unplot(ballX, ballY)
move()
led.plot(plate, 4)
led.plot(plate + 1, 4)
}
if (ballIn) {
led.plot(ballX, ballY)
}
})