Teaching Kids Programming – Design and Develop an Apple Catching Game on Microbit using Python


Teaching Kids Programming: Videos on Data Structures and Algorithms

We have actually developed and designed the Apple Catching/Eating Game on Microbit using Javascript: Microbit Programming: How to Make a Catching-Apple Game by Using Sprites Objects?

Today, we are going to use the Python to re-implement the Game on Microbit. Actually we can switch the programming language between Javascript and Python, and the code will be automatically translated to one and another.

Microbit Game Programming: Catch and Eat Apples (Python)

We need to create two sprites (objects): Apple and bowl. Then on main game loop, we let the apple fall one pixel down. Meantime we control the delay (higher score, less delay, faster falling speed). If we catch the apple (we can check this by sprite.is_touching method), we increment the score and move apple back to the top (first row). Otherwise, if the apple is on the last row (on the ground), we trigger the game-over procedure.

Full source code in Python for Microbit Game: Catch and Eat Apples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Apple Catching Game Made by Love
# Teaching Kids Programming
# 2022-2-17
 
# move bowl one pixel to the left
# wrap to right if go beyond the left boundary e.g x=0 then x=4
def moveLeft():
    if not game.is_running():
        return
    if bowl.x() == 0:
        bowl.set_x(4)
        return
    bowl.move(-1)
 
input.on_button_pressed(Button.A, moveLeft)
 
# game over: show score, pause and display game over
def gameOver():
    if not game.is_running():
            return
    basic.show_number(game.score())
    basic.pause(2000)
    game.game_over()
 
# start a new game
# triggered only when apple is on the ground
# clear score, reset apple position
def resetGame():
    if apple.y() != 4:
        return
    game.set_score(0)
    apple.set_x(randint(0, 4))
    apple.set_y(0)
    game.resume()
 
# A+B button reset the game when apple is on the ground
input.on_button_pressed(Button.AB, resetGame)
 
# move bowl one pixel to the right and
# wrap to left if beyond the boundary e.g. x=4 then x=0
def moveRight():
    if bowl.x() == 4:
        bowl.set_x(0)
        return
    bowl.move(1)
 
input.on_button_pressed(Button.B, moveRight)
 
# higher score, less delay, faster apple falling speed
def getCurrentDelay(score: number):
    delay = 500 - 10 * score
    # min delay is 50ms
    delay = max(50, delay)
    return delay
 
apple = game.create_sprite(randint(0, 4), 0)
bowl = game.create_sprite(2, 4)
 
def on_forever():
    if not game.is_running():
            return
    # apple should be falling
    basic.pause(getCurrentDelay(game.score()))
    # falling one pixel at a time
    apple.change_yby(1)
    # we've got the apple
    if bowl.is_touching(apple):
        # score += 1
        game.set_score(game.score() + 1)
        # move apple back to top with a random x
        apple.go_to(randint(0, 4), 0)
        return
    # apple is on the ground
    if apple.y() == 4:
        gameOver()
        return
 
# driver - main game loop
basic.forever(on_forever)
# Apple Catching Game Made by Love
# Teaching Kids Programming
# 2022-2-17

# move bowl one pixel to the left
# wrap to right if go beyond the left boundary e.g x=0 then x=4
def moveLeft():
    if not game.is_running():
        return
    if bowl.x() == 0:
        bowl.set_x(4)
        return
    bowl.move(-1)

input.on_button_pressed(Button.A, moveLeft)

# game over: show score, pause and display game over
def gameOver():
    if not game.is_running():
            return
    basic.show_number(game.score())
    basic.pause(2000)
    game.game_over()

# start a new game
# triggered only when apple is on the ground
# clear score, reset apple position
def resetGame():
    if apple.y() != 4:
        return
    game.set_score(0)
    apple.set_x(randint(0, 4))
    apple.set_y(0)
    game.resume()

# A+B button reset the game when apple is on the ground
input.on_button_pressed(Button.AB, resetGame)

# move bowl one pixel to the right and
# wrap to left if beyond the boundary e.g. x=4 then x=0
def moveRight():
    if bowl.x() == 4:
        bowl.set_x(0)
        return
    bowl.move(1)

input.on_button_pressed(Button.B, moveRight)

# higher score, less delay, faster apple falling speed
def getCurrentDelay(score: number):
    delay = 500 - 10 * score
    # min delay is 50ms
    delay = max(50, delay)
    return delay

apple = game.create_sprite(randint(0, 4), 0)
bowl = game.create_sprite(2, 4)

def on_forever():
    if not game.is_running():
            return
    # apple should be falling
    basic.pause(getCurrentDelay(game.score()))
    # falling one pixel at a time
    apple.change_yby(1)
    # we've got the apple
    if bowl.is_touching(apple):
        # score += 1
        game.set_score(game.score() + 1)
        # move apple back to top with a random x
        apple.go_to(randint(0, 4), 0)
        return
    # apple is on the ground
    if apple.y() == 4:
        gameOver()
        return

# driver - main game loop
basic.forever(on_forever)

Tomorrow, we’ll enlighten the computer with AI to play this game nicely using Simple Decision Tree aka Decision Rules: Teaching Kids Programming – Simple AI Algorithm of Decision Rules/Trees in Microbit Apple Catching Game

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
628 words
Last Post: Teaching Kids Programming - Introduction to Microbit Programming in Python
Next Post: Teaching Kids Programming - Simple AI Algorithm of Decision Rules/Trees in Microbit Apple Catching Game

The Permanent URL is: Teaching Kids Programming – Design and Develop an Apple Catching Game on Microbit using Python

Leave a Reply