制作坊宝藏游戏 专区

宝藏游戏

cxbd2020-04-25 475人围观

前言
小朋友们都很爱玩捉迷藏游戏以及藏东西找东西的游戏,小朋友们在躲藏和找到目标的过程中获得了很大的乐趣,我们也可以用 microbit 实现藏宝游戏,让小朋友们获得类似的乐趣。

方案
原本想实现每人一个 microbit 一个藏一个找,但只有一片 microbit,那就只用一片来实现这个游戏吧,只是要分两个阶段来玩了。
先由第一个玩家来藏宝箱,确定好位置后隐藏宝箱的显示,给第二个玩家控制精灵来寻找,确定位置后判断是否有宝箱,有的话进入下一关,否则游戏结束。每过一关,宝箱缩小一圈,一共有三关。


程序流程
提示玩家一藏宝箱
玩家一使用重力计控制宝箱移动到合适位置后,按任意按钮确认,隐藏宝箱
提示玩家二寻找宝箱
玩家二使用重力计控制精灵移动到认为可能是藏宝地点后,按任意按钮确认,开挖宝箱!
如果挖到宝箱,则进入下一关,过三关后通关
如果没有挖到宝箱,则游戏结束
通关或游戏结束后,按任意按钮重新开始游戏
JavaScript代码如下:
function levelUp () {
level += 1
game.pause()
basic.showIcon(IconNames.Heart)
music.beginMelody(music.builtInMelody(Melodies.JumpUp), MelodyOptions.Once)
}
function hideAllSprites () {
for (let tempSprite2 of hiderBox) {
tempSprite2.set(LedSpriteProperty.Brightness, 0)
}
spriteSeeker.set(LedSpriteProperty.Brightness, 0)
}
function gameWin2 () {
hideAllSprites()
game.pause()
clearAllSprites()
basic.showIcon(IconNames.Happy)
music.beginMelody(music.builtInMelody(Melodies.PowerUp), MelodyOptions.Once)
}
input.onButtonPressed(Button.B, function () {
onClick()
})
input.onButtonPressed(Button.A, function () {
onClick()
})
function moveSeeker () {
spriteSeeker.set(LedSpriteProperty.X, spriteSeeker.get(LedSpriteProperty.X) + deltaX)
spriteSeeker.set(LedSpriteProperty.Y, spriteSeeker.get(LedSpriteProperty.Y) + deltaY)
spriteSeeker.set(LedSpriteProperty.Brightness, 255)
}
function gameOver2 () {
hideAllSprites()
game.pause()
clearAllSprites()
basic.showIcon(IconNames.Sad)
music.beginMelody(music.builtInMelody(Melodies.Wawawawaa), MelodyOptions.Once)
}
function onClick () {
music.playTone(330, music.beat(BeatFraction.Sixteenth))
game.resume()
if (gameOver || gameWin) {
init()
} else {
if (state == "seeking") {
hitTest()
if (gameOver) {
gameOver()
} else if (level < MAX_LEVEL) {
levelUp()
} else if (level >= MAX_LEVEL) {
gameWin = true
gameWin()
} else {

}
}
stateManager()
}
}
function moveHider () {
if (spriteHider.get(LedSpriteProperty.X) + deltaX + (MAX_LEVEL - level) <= 4) {
spriteHider.set(LedSpriteProperty.X, spriteHider.get(LedSpriteProperty.X) + deltaX)
}
if (spriteHider.get(LedSpriteProperty.Y) + deltaY + (MAX_LEVEL - level) <= 4) {
spriteHider.set(LedSpriteProperty.Y, spriteHider.get(LedSpriteProperty.Y) + deltaY)
}
drawHiderBox()
}
function stateManager () {
if (state == "hidePrompt") {
state = "hiding"
} else if (state == "hiding") {
state = "seekPrompt"
} else if (state == "seekPrompt") {
state = "seeking"
} else if (state == "seeking") {
if (gameOver) {
state = "gameOver"
} else if (gameWin) {
state = "win"
} else {
state = "hidePrompt"
}
} else if (state == "gameOver" || state == "win") {
state = "hidePrompt"
}
}
function drawHiderBox () {
i = 0
while (i <= MAX_LEVEL - level) {
j = 0
while (j <= MAX_LEVEL - level) {
tempSprite = hiderBox[i * (MAX_LEVEL - level + 1) + j]
tempSprite.set(LedSpriteProperty.X, spriteHider.get(LedSpriteProperty.X) + i)
tempSprite.set(LedSpriteProperty.Y, spriteHider.get(LedSpriteProperty.Y) + j)
tempSprite.set(LedSpriteProperty.Brightness, 255)
j += 1
}
i += 1
}
}
function init () {
ACCELLARATION_THRESHOLD = 100
spriteSeeker = game.createSprite(2, 2)
direction = "none"
level = 1
spriteSeeker.set(LedSpriteProperty.Brightness, 0)
spriteSeeker.set(LedSpriteProperty.Blink, 5)
initHiderBox()
state = "hidePrompt"
gameOver = false
gameWin = false
MAX_LEVEL = 3
}
function getDirection () {
if (Math.abs(input.acceleration(Dimension.X)) >= Math.abs(input.acceleration(Dimension.Y))) {
if (input.acceleration(Dimension.X) > ACCELLARATION_THRESHOLD) {
direction = "right"
} else if (input.acceleration(Dimension.X) < 0 - ACCELLARATION_THRESHOLD) {
direction = "left"
} else {
direction = "none"
}
} else {
if (input.acceleration(Dimension.Y) > ACCELLARATION_THRESHOLD) {
direction = "down"
} else if (input.acceleration(Dimension.Y) < 0 - ACCELLARATION_THRESHOLD) {
direction = "up"
} else {
direction = "none"
}
}
}
function clearAllSprites () {
spriteSeeker.delete()
for (let index = 0; index < hiderBox.length; index++) {
tempSprite = hiderBox.pop()
tempSprite.delete()
}
}
function initHiderBox () {
hiderBox = []
for (let index = 0; index < 9; index++) {
tempSprite = game.createSprite(0, 0)
tempSprite.set(LedSpriteProperty.Brightness, 0)
hiderBox.push(tempSprite)
}
spriteHider = hiderBox[0]
}
function hitTest () {
i = 0
while (i <= MAX_LEVEL - level) {
j = 0
while (j <= MAX_LEVEL - level) {
tempSprite = hiderBox[i * (MAX_LEVEL - level + 1) + j]
if (spriteSeeker.isTouching(tempSprite)) {
return
}
j += 1
}
i += 1
}
gameOver = true
}
function updateDirection2 () {
if (direction == "none") {
deltaX = 0
deltaY = 0
} else if (direction == "up") {
deltaX = 0
deltaY = -1
} else if (direction == "down") {
deltaX = 0
deltaY = 1
} else if (direction == "left") {
deltaX = -1
deltaY = 0
} else if (direction == "right") {
deltaX = 1
deltaY = 0
}
}
let direction = ""
let ACCELLARATION_THRESHOLD = 0
let tempSprite: game.LedSprite = null
let j = 0
let i = 0
let spriteHider: game.LedSprite = null
let MAX_LEVEL = 0
let state = ""
let gameWin = false
let gameOver = false
let deltaY = 0
let deltaX = 0
let spriteSeeker: game.LedSprite = null
let hiderBox: game.LedSprite[] = []
let level = 0
init()
basic.forever(function () {
getDirection()
if (state == "hidePrompt") {
hideAllSprites()
basic.showString("hide")
} else if (state == "hiding") {
updateDirection2()
moveHider()
} else if (state == "seekPrompt") {
hideAllSprites()
basic.showString("seek")
} else if (state == "seeking") {
updateDirection2()
moveSeeker()
}
basic.pause(200)
})
0 +1

评论