玩剪刀石头布通常是两人游戏……但它也将与更多玩家一起使用!当与两个以上的玩家一起玩时,它变成了团队游戏:所有玩家同时摇动,然后在所有玩家之间计算石头,纸张和剪刀的数量。通过摇动micro:bit,根据选择的工具自动组队。摇动后拥有最多玩家的团队将赢得比赛。
程序从石头剪刀布游戏的基本版本开始,我们将更改代码,以使micro:bit计数并显示团队中的玩家数量。micro:bit将使用无线电通信发送其状态并接收其他板的状态。
代码: 全选
let temp = 0
let found = false
let player_index = 0
let tool = 0
let match = false
let players: number[] = []
let serialNumber = 0;
radio.onReceivedNumber(function (receivedNumber) {
serialNumber = radio.receivedPacket(RadioPacketProperty.SerialNumber)
match = tool == receivedNumber
player_index = players.indexOf(serialNumber)
found = player_index >= 0
if (match && !(found)) {
players.push(serialNumber)
}
if (!(match) && found) {
temp = players.removeAt(player_index)
}
})
input.onGesture(Gesture.Shake, function() {
players = [0]
tool = Math.randomRange(0, 2)
})
basic.forever(function() {
radio.sendNumber(tool)
if (tool == 0) {
basic.showIcon(IconNames.SmallSquare)
} else if (tool == 1) {
basic.showIcon(IconNames.Square)
} else {
basic.showIcon(IconNames.Scissors)
}
basic.showNumber(players.length)
})
players = [0]
radio.setGroup(10)
radio.setTransmitSerialNumber(true)
在线查看