Home > Mindstorms > Mindstorms Pong

Mindstorms Pong

December 9th, 2022 Leave a comment Go to comments

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]
heading = 0
ball_is_out = 0;
paddleL1 = 2
paddleL2 = 3
paddleR1 = 2
paddleR2 = 3
motorA = Motor('A')
motorB = Motor('B')

# right paddle
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
    paddleR1 = 0
    paddleR2 = 1

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)
    global paddleR1
    global paddleR2
    paddleR1 = 1
    paddleR2 = 2

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)
    global paddleR1
    global paddleR2
    paddleR1 = 2
    paddleR2 = 3

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)
    global paddleR1
    global paddleR2
    paddleR1 = 3
    paddleR2 = 4

# left paddle
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)
    global paddleL1
    global paddleL2
    paddleL1 = 0
    paddleL2 = 1

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)
    global paddleL1
    global paddleL2
    paddleL1 = 1
    paddleL2 = 2

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)
    global paddleL1
    global paddleL2
    paddleL1 = 2
    paddleL2 = 3

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)
    global paddleL1
    global paddleL2
    paddleL1 = 3
    paddleL2 = 4

# start-up
def startup():
    global paddleL1
    global paddleL2
    global paddleR1
    global paddleR2
    motorA.run_to_position(90)
    motorB.run_to_position(90)
    paddleL1 = 2
    paddleL2 = 3
    paddleR1 = 2
    paddleR2 = 3

    hub.light_matrix.set_pixel(0, paddleL1)
    hub.light_matrix.set_pixel(0, paddleL2)

    hub.light_matrix.set_pixel(4, paddleR1)
    hub.light_matrix.set_pixel(4, paddleR2)
    reset()

def reset():
    global ball_is_out
    ball_is_out = 0

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

def move_paddles():
    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
    global heading
    hub.light_matrix.set_pixel(ball[0], ball[1], 0)

    # 12
    #X
    # 34

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

    hub.light_matrix.set_pixel(ball[0], ball[1], 100)

def startGame():
    while True:
        move_paddles()

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

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

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

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

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

Photos:

IMG_0422.JPG IMG_0424.JPG IMG_0425.JPG IMG_0429.JPG

Video:

Categories: Mindstorms Tags:
  1. No comments yet.
  1. No trackbacks yet.