STARLEAP / SPIKE PRIME PYTHON / 2026–27

Build 2026.09.22-090630-0400

On this page

Project 06: Bouncing Rhino Arena

You need: a LEGO SPIKE Prime set and the SPIKE App Python editor.

Goal

Make Rhino drive until the bumper hits something, back away, turn, and keep going.

This is a core class challenge. Multiple Rhinos can run in the same arena if they bounce away safely.

Build

Build Rhino from the LEGO SPIKE Prime building instructions.

Starting ports:

If your build uses a different bumper port, change the bumper label.

Using the code: gray lines are already in your program; dark lines are the edit. Keep the shown indentation. Replace a displayed main() definition as a whole, keeping imports and the final start call.

What You Will Learn

Before you run: use a clear floor area away from table edges. Test the wheel direction with the wheels lifted, then stop before placing the robot on the floor.

Step 1: Stop On Bump

Open a new Python project named Rhino Arena.

Start with this short code:

CODE
from hub import port
import runloop
import motor_pair
import force_sensor

left_wheel = port.B
right_wheel = port.A
bumper = port.C

def bumper_pressed():
    return force_sensor.pressed(bumper)

async def main():
    motor_pair.pair(motor_pair.PAIR_1, left_wheel, right_wheel)
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=350)
    await runloop.until(bumper_pressed)
    motor_pair.stop(motor_pair.PAIR_1)

runloop.run(main())

Run the program facing a wall, box, or bin.

Observe: Rhino stops when the bumper is pressed.

Step 2: Add Helper Functions

Add these above main():

CODE
def go_forward():
    print("forward")
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=350)

def stop():
    print("stop")
    motor_pair.stop(motor_pair.PAIR_1)

async def back_turn():
    print("back turn")
    await motor_pair.move_for_degrees(motor_pair.PAIR_1, -180, 80, velocity=300)

Change main():

EDIT VIEW · Gray = already there; dark = add or change
async def main():
    motor_pair.pair(motor_pair.PAIR_1, left_wheel, right_wheel)
    go_forward()
    await runloop.until(bumper_pressed)
    stop()
    await back_turn()

Run the program.

Observe: Rhino drives, bumps, stops, backs away, and turns.

Step 3: Name The Whole Behavior

Add this above main():

CODE
async def bounce_once():
    go_forward()
    await runloop.until(bumper_pressed)
    stop()
    await back_turn()

Change main():

EDIT VIEW · Gray = already there; dark = add or change
async def main():
    motor_pair.pair(motor_pair.PAIR_1, left_wheel, right_wheel)
    await bounce_once()
    await bounce_once()

Run the program.

Observe: Rhino should bounce twice.

Step 4: Keep Bouncing

Change main():

EDIT VIEW · Gray = already there; dark = add or change
async def main():
    motor_pair.pair(motor_pair.PAIR_1, left_wheel, right_wheel)

    while True:
        await bounce_once()

Run the program in the arena.

Stop the program manually when you are done.

Physical Challenge: Bouncing Rhino Arena

Tune one value at a time:

Extra setup idea: Try the Walled Cleanup Area from the Challenge Setups sheet.

Teacher Check

Show your teacher:

Before You Put The Kit Away

Save your program. Stop the program before moving wires or changing the build. Show the teacher your working behavior, point to one changed line, and explain what it controls. Keep your place for next time.