Change the direction of the scroll text with Unicorn HAT HD

Hello friends,
How can I Change the direction of the scroll text in text.py, I need to move from left to right.

Thanks.

The clue lies here:

for scroll in range(text_width - width):
    for x in range(width):
        for y in range(height):
            pixel = image.getpixel((x+scroll, y))
            r, g, b = [int(n) for n in pixel]
            unicornhathd.set_pixel(width-1-x, y, r, g, b)

This code loops through every pixel in Unicorn HAT HD and finds the corresponding pixel on the bitmap that it needs to draw.

Think of it as a moving window of width/height pixels that slides across the total width of the text buffer, which itself is just a wide image.

To reverse the direction, you need to reverse the value of scroll.

You could either do that by subtracting the inner value of scroll from the total width, or by using Python’s reversed() to reverse the range.

Try:

for scroll in reversed(range(text_width - width)):
    for x in range(width):
        for y in range(height):
            pixel = image.getpixel((x+scroll, y))
            r, g, b = [int(n) for n in pixel]
            unicornhathd.set_pixel(width-1-x, y, r, g, b)
1 Like