Thursday, February 3, 2022

Microbit: Pong Game

 


Code

JavaScript

let bar_x = 0
let score = 0
let interval = 0
let interval_step = 0
let ball_x = 0
let ball_y = 0
let ball_dx = 0
let ball_dy = 0
let in_game = false
// 
input.onButtonPressed(Button.Afunction () {
    if (bar_x > 0) {
        led.unplot(bar_x + 14)
        bar_x = bar_x - 1
        led.plot(bar_x, 4)
    }
})
// 
input.onButtonPressed(Button.Bfunction () {
    if (bar_x < 3) {
        led.unplot(bar_x, 4)
        bar_x = bar_x + 1
        led.plot(bar_x + 14)
    }
})
basic.forever(function () {
    score = 0
    interval = 500
    interval_step = 10
    ball_x = 3
    ball_y = 4
    ball_dx = -1
    ball_dy = -1
    bar_x = 0
    basic.showString("GO")
    led.plot(ball_x, ball_y)
    led.plot(bar_x, 4)
    led.plot(bar_x + 14)
    in_game = true
    while (in_game) {
        if (ball_x + ball_dx > 4) {
            ball_dx = ball_dx * -1
        } else if (ball_x + ball_dx < 0) {
            ball_dx = ball_dx * -1
        }
        if (ball_y + ball_dy < 0) {
            ball_dy = ball_dy * -1
        } else if (ball_y + ball_dy > 3) {
            if (led.point(
            ball_x + ball_dx,
            ball_y + ball_dy
            )) {
                ball_dy = ball_dy * -1
                score = score + 1
                if (interval - interval_step >= 0) {
                    interval = interval - interval_step
                }
            } else {
                in_game = false
            }
        }
        if (in_game) {
            led.plot(
            ball_x + ball_dx,
            ball_y + ball_dy
            )
            led.unplot(ball_x, ball_y)
            ball_x = ball_x + ball_dx
            ball_y = ball_y + ball_dy
            basic.pause(interval)
        } else {
            game.setScore(score)
            game.gameOver()
        }
    }
})



6 comments:

  1. Can you change the Java Script to Python Please

    ReplyDelete
    Replies
    1. heres the pyton code:
      bar_x = 0
      score = 0
      interval = 0
      interval_step = 0
      ball_x = 0
      ball_y = 0
      ball_dx = 0
      ball_dy = 0
      in_game = False
      #

      def on_button_pressed_a():
      global bar_x
      if bar_x > 0:
      led.unplot(bar_x + 1, 4)
      bar_x = bar_x - 1
      led.plot(bar_x, 4)
      input.on_button_pressed(Button.A, on_button_pressed_a)

      #

      def on_button_pressed_b():
      global bar_x
      if bar_x < 3:
      led.unplot(bar_x, 4)
      bar_x = bar_x + 1
      led.plot(bar_x + 1, 4)
      input.on_button_pressed(Button.B, on_button_pressed_b)

      def on_forever():
      global score, interval, interval_step, ball_x, ball_y, ball_dx, ball_dy, bar_x, in_game
      score = 0
      interval = 500
      interval_step = 10
      ball_x = 3
      ball_y = 4
      ball_dx = -1
      ball_dy = -1
      bar_x = 0
      basic.show_string("GO")
      led.plot(ball_x, ball_y)
      led.plot(bar_x, 4)
      led.plot(bar_x + 1, 4)
      in_game = True
      while in_game:
      if ball_x + ball_dx > 4:
      ball_dx = ball_dx * -1
      elif ball_x + ball_dx < 0:
      ball_dx = ball_dx * -1
      if ball_y + ball_dy < 0:
      ball_dy = ball_dy * -1
      elif ball_y + ball_dy > 3:
      if led.point(ball_x + ball_dx, ball_y + ball_dy):
      ball_dy = ball_dy * -1
      score = score + 1
      if interval - interval_step >= 0:
      interval = interval - interval_step
      else:
      in_game = False
      if in_game:
      led.plot(ball_x + ball_dx, ball_y + ball_dy)
      led.unplot(ball_x, ball_y)
      ball_x = ball_x + ball_dx
      ball_y = ball_y + ball_dy
      basic.pause(interval)
      else:
      game.set_score(score)
      game.game_over()
      basic.forever(on_forever)

      Delete
  2. let bar_x = 0
    let score = 0
    let interval = 0
    let interval_step = 0
    let ball_x = 0
    let ball_y = 0
    let ball_dx = 0
    let ball_dy = 0
    let in_game = false
    //
    input.onButtonPressed(Button.A, function () {
    if (bar_x > 0) {
    led.unplot(bar_x + 1, 4)
    bar_x = bar_x - 1
    led.plot(bar_x, 4)
    }
    })
    //
    input.onButtonPressed(Button.B, function () {
    if (bar_x < 3) {
    led.unplot(bar_x, 4)
    bar_x = bar_x + 1
    led.plot(bar_x + 1, 4)
    }
    })
    basic.forever(function () {
    score = 0
    interval = 500
    interval_step = 10
    ball_x = 3
    ball_y = 4
    ball_dx = -1
    ball_dy = -1
    bar_x = 0
    basic.showString("GO")
    led.plot(ball_x, ball_y)
    led.plot(bar_x, 4)
    led.plot(bar_x + 1, 4)
    in_game = true
    while (in_game) {
    if (ball_x + ball_dx > 4) {
    ball_dx = ball_dx * -1
    } else if (ball_x + ball_dx < 0) {
    ball_dx = ball_dx * -1
    }
    if (ball_y + ball_dy < 0) {
    ball_dy = ball_dy * -1
    } else if (ball_y + ball_dy > 3) {
    if (led.point(
    ball_x + ball_dx,
    ball_y + ball_dy
    )) {
    ball_dy = ball_dy * -1
    score = score + 1
    if (interval - interval_step >= 0) {
    interval = interval - interval_step
    }
    } else {
    in_game = false
    }
    }
    if (in_game) {
    led.plot(
    ball_x + ball_dx,
    ball_y + ball_dy
    )
    led.unplot(ball_x, ball_y)
    ball_x = ball_x + ball_dx
    ball_y = ball_y + ball_dy
    basic.pause(interval)
    } else {
    game.setScore(score)
    game.gameOver()
    }
    }
    })





    at February 03, 2022
    Email This
    BlogThis!
    Share to Twitter
    Share to Facebook
    Share to Pinterest
    2 comments:

    AnonymousApril 28, 2022 at 8:26 AM
    Can you change the Java Script to Python Please

    Reply
    Replies

    AnonymousDecember 12, 2022 at 10:24 PM
    heres the pyton code:
    bar_x = 0
    score = 0
    interval = 0
    interval_step = 0
    ball_x = 0
    ball_y = 0
    ball_dx = 0
    ball_dy = 0
    in_game = False
    #

    def on_button_pressed_a():
    global bar_x
    if bar_x > 0:
    led.unplot(bar_x + 1, 4)
    bar_x = bar_x - 1
    led.plot(bar_x, 4)
    input.on_button_pressed(Button.A, on_button_pressed_a)

    #

    def on_button_pressed_b():
    global bar_x
    if bar_x < 3:
    led.unplot(bar_x, 4)
    bar_x = bar_x + 1
    led.plot(bar_x + 1, 4)
    input.on_button_pressed(Button.B, on_button_pressed_b)

    def on_forever():
    global score, interval, interval_step, ball_x, ball_y, ball_dx, ball_dy, bar_x, in_game
    score = 0
    interval = 500
    interval_step = 10
    ball_x = 3
    ball_y = 4
    ball_dx = -1
    ball_dy = -1
    bar_x = 0
    basic.show_string("GO")
    led.plot(ball_x, ball_y)
    led.plot(bar_x, 4)
    led.plot(bar_x + 1, 4)
    in_game = True
    while in_game:
    if ball_x + ball_dx > 4:
    ball_dx = ball_dx * -1
    elif ball_x + ball_dx < 0:
    ball_dx = ball_dx * -1
    if ball_y + ball_dy < 0:
    ball_dy = ball_dy * -1
    elif ball_y + ball_dy > 3:
    if led.point(ball_x + ball_dx, ball_y + ball_dy):
    ball_dy = ball_dy * -1
    score = score + 1
    if interval - interval_step >= 0:
    interval = interval - interval_step
    else:
    in_game = False
    if in_game:
    led.plot(ball_x + ball_dx, ball_y + ball_dy)
    led.unplot(ball_x, ball_y)
    ball_x = ball_x + ball_dx
    ball_y = ball_y + ball_dy
    basic.pause(interval)
    else:
    game.set_score(score)
    game.game_over()
    basic.forever(on_forever)

    ReplyDelete
  3. Can you give me the video/make it unprivate

    ReplyDelete