PyRaspBeta

← All practice problems

Raspberry Pi practice problem · Buttons & input

Tug of war

HardBudget $9Python · gpiozero

A two-player game. Five LEDs in a row on GPIO5, 6, 13, 19, 26 (positions 0–4) and two buttons (to ground): the Left player on GPIO20, the Right player on GPIO21.

  • One LED is lit: the "rope", starting in the middle (position 2, GPIO13).
  • Each Left press moves it one step left (towards GPIO5); each Right press one step right (towards GPIO26). A press counts once, however long it is held — no auto-repeat.
  • When the rope reaches an end, that player wins: print Left wins or Right wins once. The end LED stays lit and all further presses are ignored.

The buttons are bouncy (about 8 ms). The test: Right held 1.0–2.5 s; Left at 3.0 and 3.5 s; Right at 4.0 s; Left at 4.5 and 5.0 s; then Right at 5.5 and 6.0 s.

More in Buttons & input