Home > Mindstorms > Mindstorms Pong

## Mindstorms Pong Simple version of the Pong game running on the latest Mindstorms brick with two motors as controllers.

### Datasheet:

Completion date: 09/12/2022
Power: electric (Mindstorms brick)
Language: Python
Bricks: 1
Motors: 2 x Mindstorms M
Sensors: none

Having recently acquired the LEGO 51515 Robot Inventor set (sadly, discontinued promptly after), I’ve become interested in programming the central brick. I’ve tried some programs in Scratch but quickly switched to Python for more complex stuff. I ended up writing a simple variation of the Pong game that ran on the brick’s display, with two motors used as inputs for controlling the two paddles. Additionally, the left and right buttons on the bricks were used to launch the ball left or right.

The ball could bounce off the upper and lower walls, but if it touched a side wall, the game registered it as a loss of ball. The ball would then start from the initial position, going in the same direction it had when going off the screen. The paddles could be moved up and down by rotating the motor’s outputs. It worked, but one of the issues I was unable to solve was the fact that the paddle control function and the ball movement function were running in the same loop, so one was interfering with the other, sometimes resulting in delayed paddle motion. All in all, it was a simple but fun programming exercise.

### Pong program:

``````from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App
from mindstorms.control import wait_for_seconds, wait_until, Timer
from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to
import math

# config
hub = MSHub()
ball = [2, 4]
ball_is_out = 0;
motorA = Motor('A')
motorB = Motor('B')

def right_upper():
hub.light_matrix.set_pixel(4, 0, 100)
hub.light_matrix.set_pixel(4, 1, 100)
hub.light_matrix.set_pixel(4, 2, 0)
hub.light_matrix.set_pixel(4, 3, 0)
hub.light_matrix.set_pixel(4, 4, 0)
global palletteR1
global palletteR2

def right_up():
hub.light_matrix.set_pixel(4, 0, 0)
hub.light_matrix.set_pixel(4, 1, 100)
hub.light_matrix.set_pixel(4, 2, 100)
hub.light_matrix.set_pixel(4, 3, 0)
hub.light_matrix.set_pixel(4, 4, 0)

def right_mid():
hub.light_matrix.set_pixel(4, 0, 0)
hub.light_matrix.set_pixel(4, 1, 0)
hub.light_matrix.set_pixel(4, 2, 100)
hub.light_matrix.set_pixel(4, 3, 100)
hub.light_matrix.set_pixel(4, 4, 0)

def right_lower():
hub.light_matrix.set_pixel(4, 0, 0)
hub.light_matrix.set_pixel(4, 1, 0)
hub.light_matrix.set_pixel(4, 2, 0)
hub.light_matrix.set_pixel(4, 3, 100)
hub.light_matrix.set_pixel(4, 4, 100)

def left_upper():
hub.light_matrix.set_pixel(0, 0, 100)
hub.light_matrix.set_pixel(0, 1, 100)
hub.light_matrix.set_pixel(0, 2, 0)
hub.light_matrix.set_pixel(0, 3, 0)
hub.light_matrix.set_pixel(0, 4, 0)

def left_up():
hub.light_matrix.set_pixel(0, 0, 0)
hub.light_matrix.set_pixel(0, 1, 100)
hub.light_matrix.set_pixel(0, 2, 100)
hub.light_matrix.set_pixel(0, 3, 0)
hub.light_matrix.set_pixel(0, 4, 0)

def left_mid():
hub.light_matrix.set_pixel(0, 0, 0)
hub.light_matrix.set_pixel(0, 1, 0)
hub.light_matrix.set_pixel(0, 2, 100)
hub.light_matrix.set_pixel(0, 3, 100)
hub.light_matrix.set_pixel(0, 4, 0)

def left_lower():
hub.light_matrix.set_pixel(0, 0, 0)
hub.light_matrix.set_pixel(0, 1, 0)
hub.light_matrix.set_pixel(0, 2, 0)
hub.light_matrix.set_pixel(0, 3, 100)
hub.light_matrix.set_pixel(0, 4, 100)

# start-up
def startup():
motorA.run_to_position(90)
motorB.run_to_position(90)

reset()

def reset():
global ball_is_out
ball_is_out = 0

global ball
ball = [2, 4]
hub.light_matrix.set_pixel(ball, ball)
hub.status_light.on('green')

a = motorA.get_position()

if a <= 70:
right_lower()
elif a > 70 and a <= 110:
right_mid()
elif a > 110 and a <= 130:
right_up()
else:
right_upper()

b = motorB.get_position()

if b >= 110:
left_lower()
elif b < 110 and b >= 70:
left_mid()
elif b < 70 and b >= 50:
left_up()
else:
left_upper()

def ball_out():
hub.status_light.on('red')
hub.speaker.play_sound('Hammer')
reset()

def move_ball():
global ball
hub.light_matrix.set_pixel(ball, ball, 0)

# 12
#X
# 34

if heading == 1: # NW
ball = [ball - 1, ball - 1]
if (ball < 1 and ball < 0):
hub.light_matrix.set_pixel(ball, ball, 100)
wait_for_seconds(tempo)
ball_out()
else:
ball = [2, 1]
elif (ball < 1):
hub.light_matrix.set_pixel(ball, ball, 100)
wait_for_seconds(tempo)
ball_out()
else:
ball = [2, ball]
elif (ball < 0):
ball = [ball, 1]
elif heading == 2: # NE
ball = [ball + 1, ball - 1]
if (ball > 3 and ball < 0):
hub.light_matrix.set_pixel(ball, ball, 100)
wait_for_seconds(tempo)
ball_out()
else:
ball = [2, 1]
elif (ball > 3):
hub.light_matrix.set_pixel(ball, ball, 100)
wait_for_seconds(tempo)
ball_out()
else:
ball = [2, ball]
elif (ball < 0):
ball = [ball, 1]
elif heading == 3: # SW
ball = [ball - 1, ball + 1]
if (ball < 1 and ball > 4):
hub.light_matrix.set_pixel(ball, ball, 100)
wait_for_seconds(tempo)
ball_out()
else:
ball = [2, 3]
elif (ball< 1):
hub.light_matrix.set_pixel(ball, ball, 100)
wait_for_seconds(tempo)
ball_out()
else:
ball = [2, ball]
elif (ball > 4):
ball = [ball, 3]
elif heading == 4: # SE
ball = [ball + 1, ball + 1]
if (ball > 3 and ball > 4):
hub.light_matrix.set_pixel(ball, ball, 100)
wait_for_seconds(tempo)
ball_out()
else:
ball = [1, 3]
elif (ball > 3):
hub.light_matrix.set_pixel(ball, ball, 100)
wait_for_seconds(tempo)
ball_out()
else:
ball = [1, ball]
elif (ball > 4):
ball = [ball, 3]

hub.light_matrix.set_pixel(ball, ball, 100)

def startGame():
while True:

if hub.right_button.is_pressed():
hub.speaker.play_sound('Ping')
global ball_is_out
ball_is_out = 0

while (ball_is_out == 0):
move_ball()
wait_for_seconds(tempo)

if hub.left_button.is_pressed():
hub.speaker.play_sound('Ping')
global ball_is_out
ball_is_out = 0

while (ball_is_out == 0):
move_ball()
wait_for_seconds(tempo)

# --- start program ---
tempo = 0.25
startup()
startGame()```
```

### Video:

Categories: Mindstorms Tags: