micro:bit(マイクロビット)
Please do not reuse without prior permission.

micro:bit(マイクロビット)でピンポンゲームを作る。完成編

先日、公開したピンポンゲームをベースにボールを打ち返す度にボールの速さが速くなるようにします。速くなるスピードは無制限!どこまでも速くなります!
0 Shares
0
0
0

先日、公開したピンポンゲームをベースにボールを打ち返す度にボールの速さが速くなるようにします。速くなるスピードは無制限!どこまでも速くなります!

今回作るピンポンゲーム

ボールを打ち返す度にボールの動きが速くなります!

必要なもの

micro:bitだけあれば動作します。スピーカーなど特別なものは必要ありません。

「最初だけ」のプログラム

ボールの動く間隔を短くするために使用する新しい変数intervalを追加しています。

「最初だけ」のプログラム

「ボタンAを押したとき」のプログラム

ボールを打ち返した際に、ボールの動く間隔を短くするためにintervalを50増加する処理を追加。

「ボタンAを押したとき」のプログラム

「ボタンBを押したとき」のプログラム

ボールを打ち返した際に、ボールの動く間隔を短くするためにintervalを50増加する処理を追加。

「ボタンBを押したとき」のプログラム

「ずっと」のプログラム

ボールを打ち返した際に、ボールの動く間隔を短くするためにintervalを50増加する処理を追加。
ボールの動く間隔を短くするために初期値1000からintervalだけ短くする処理を追加。

「ずっと」のプログラム

ソースコード

Javascript画面に張り付けるだけで動作します。

let on_game = false
let interval = 0
let ball_direction_x = 0
let ball_direction_y = 0
let bar_x = 0
let ball_y = 0
let ball_old_y = 0
let ball_x = 0
let ball_old_x = 0
function ball22() {
ball_old_x = ball_x
ball_old_y = ball_y
ball_x += ball_direction_x
ball_y += ball_direction_y
if (ball_x == 4) {
ball_direction_x = -1
} else if (ball_x == 0) {
ball_direction_x = 1
}
if (ball_y == 3) {
if (ball_x == bar_x) {
ball_direction_y = -1
interval = interval + 50
}
} else if (ball_y == 0) {
ball_direction_y = 1
} else if (ball_y >= 4) {
on_game = false
}
}
input.onButtonPressed(Button.A, function () {
if (bar_x != 0) {
led.unplot(bar_x, 4)
bar_x += -1
led.plot(bar_x, 4)
}
if (ball_x == bar_x && ball_y == 3) {
ball_direction_y = -1
interval = interval + 50
}
})
input.onButtonPressed(Button.B, function () {
if (bar_x != 4) {
led.unplot(bar_x, 4)
bar_x += 1
led.plot(bar_x, 4)
}
if (ball_x == bar_x && ball_y == 3) {
ball_direction_y = -1
interval = interval + 50
}
})
ball_x = 1
ball_y = 0
ball_old_x = 0
ball_old_y = 0
ball_direction_x = 1
ball_direction_y = 1
bar_x = 2
interval = 0
on_game = false
basic.forever(function () {
basic.showNumber(3)
basic.showNumber(2)
basic.showNumber(1)
basic.showNumber(0)
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
on_game = true
led.plot(bar_x, 4)
while (on_game == true) {
ball22()
led.plot(ball_x, ball_y)
led.unplot(ball_old_x, ball_old_y)
basic.pause(1000 - interval)
}
basic.showString("Game Over!")
control.reset()
})
0 Shares
コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

You May Also Like