CraigT
Posts: 21
Joined: Mon Oct 27, 2014 10:45 pm

Unicorn HAT Demo Ideas

Wed Feb 04, 2015 9:11 pm

I haven't got a Unicorn HAT yet, but I am very close to getting one. :D
I was just looking for some ideas how to show it off. 8-)
It doesn't have to be actual code (but please feel free to add or link to some), I'd be quite happy just to hear of an explanation of an idea.
Quick note - I'm looking for pretty patterns, not (scrolling) text.

For example.
1. A full matrix gradual fade from black > red > black > green > black > blue > black > white > black (and back around again)
2. A set of red, green, blue, white stripes that will scroll across the matrix (horizontally, vertically, diagonally or some combination of all 3).
3. A dark (close to black centre) and each corner set to put red, green, blue and white. Then make the pattern rotate so that the corner colours rotate by 90 degrees (like turning the front face of a Rubik's cube).

I have another idea, but that is even more difficult to explain. I might keep that secret until I get my Unicorn HAT. ;)

I look forward to hearing of some ideas.

Craig

PS. There are already some good demos on YouTube.

User avatar
ukscone
Forum Moderator
Forum Moderator
Posts: 3936
Joined: Fri Jul 29, 2011 2:51 pm
Contact: Website

Re: Unicorn HAT Demo Ideas

Wed Feb 04, 2015 9:31 pm

i stopped messing w/ my unicornhat for a while but one of the last things i was messing w/ was

Code: Select all

[email protected] /mnt/usb/pi/unimess/unimess/5488053 $ cat spiral.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unicornhat as unicorn
import time, datetime, webcolors

unicorn.rotation(90)
unicorn.brightness(0.2)


def left(stop, start, row, colour):
    for x in range(start-1, stop-1, -1):
        unicorn.set_pixel(x, row, colour[0], colour[1], colour[2])
        unicorn.show()
        time.sleep(0.1)

def right(start, stop , row, colour):
    for x in range(start, stop, 1):
        unicorn.set_pixel(x, row, colour[0], colour[1], colour[2])
        unicorn.show()
        time.sleep(0.1)

def up(stop, start, column, colour):
    for y in range(start-1, stop, -1):
        unicorn.set_pixel(column, y, colour[0], colour[1], colour[2])
        unicorn.show()
        time.sleep(0.1)

def down(start, stop, column, colour):
    for y in range(start, stop, 1):
        unicorn.set_pixel(column, y, colour[0], colour[1], colour[2])
        unicorn.show()
        time.sleep(0.1)

def curlicue(start, stop, iterations):
  for z in range(iterations):
    right (start+z,stop-z,start+z,(255,255,0))
    down  (start+z,stop-z,(stop-1)-z,(255,255,0))
    left  (start+z,stop-z,(stop-1)-z,(255,255,0))
    up    (start+z,stop-z,start+z,(255,255,0))

curlicue(1,7,1)
time.sleep(5)
btw that was from a directory that was a WIP w/ a lot of fiddling going on. it might not be 100% working as it's listed above but i'm sure if it isn't you can get it sussed

CraigT
Posts: 21
Joined: Mon Oct 27, 2014 10:45 pm

Re: Unicorn HAT Demo Ideas

Wed Feb 04, 2015 9:46 pm

Ooh a spiral. I like the sound of that.

I've seen a lot of code with the brightness set to 0.2, is this just because the LEDs are so bright?

Craig

User avatar
ukscone
Forum Moderator
Forum Moderator
Posts: 3936
Joined: Fri Jul 29, 2011 2:51 pm
Contact: Website

Re: Unicorn HAT Demo Ideas

Wed Feb 04, 2015 9:54 pm

the leds on the unicornhat are bright and really do need a diffusion layer or something as i don't have one and the paper i had was too thick i just turn the intensity down. normally use 0.2 to 0.4 (also at higher/lower intensities i find some values of rgb are too dim/too bright to get a decent range YMMV)

Timcw
Posts: 7
Joined: Sun Feb 08, 2015 12:43 pm

Re: Unicorn HAT Demo Ideas

Sun Feb 08, 2015 12:52 pm

Hi, new here and was looking at some of the ideas that others might of had for their Unicorn Hats. I picked one up this week and decided to put Battleships onto it.

Anyway here's what a little bit of work ended up looking like. Feel free to play with the code yourself.

Code: Select all

"""
Program: Battleships Unicorn Hat display
Author: Tim Mulford
Date: 08/02/2015
Version: 0.2
Python3
"""

#import libraries
import unicornhat as unicorn
import time, random

# create initial grid
def create_sea():
    # initialise blank grid
    grid = [[0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0]]

    # generate position for battleship
    x = random.randint(0,3)
    y = random.randint(0,7)

    # place battleship
    for a in range(5):
        grid[y][x+a]=1

    # generate co-ordinate for cruiser
    x = random.randint(0,5)
    y = random.randint(0,7)

    # check if the generated co-ordinates are clear
    # regenerate co-ordinates until a clear position is found
    while [grid[y][x],grid[y][x+1],grid[y][x+2]] != [0,0,0]:
        x = random.randint(0,5)
        y = random.randint(0,7)

    # place cruiser
    for a in range(3):
        grid[y][x+a]=1

    # generate co-ordintes for patrol boat   
    x = random.randint(0,6)
    y = random.randint(0,7)

    # check if the geneated co-ordinates are clear
    # regenerate co-ordinates until a clear position is found
    while [grid[y][x],grid[y][x+1]] != [0,0]:
        x = random.randint(0,6)
        y = random.randint(0,7)

    # place patrol boat
    for a in range(2):
        grid[y][x+a]=1

    # used for testing grid generated properly
    # displays grid to screen
    #for row in grid:
    #    print(row)
        
    return(grid)

# display the current state of the sea
# blue LED for undisturbed sea
# red LED for HIT
# unlit for MISS
def display_sea(sea):
    for y in range(8):
        for x in range(8):
            if sea[y][x] == 0 or sea[y][x] == 1:
                unicorn.set_pixel(x,y,79,106,226)
            if sea[y][x] == 2:
                unicorn.set_pixel(x,y,66,68,77)
            if sea[y][x] == 3:
                unicorn.set_pixel(x,y,214,28,31)
    unicorn.show()

# set the whole display to colour of the passed RGB code
def flood_colour(red,green,blue):
    for x in range(8):
        for y in range(8):
            unicorn.set_pixel(x,y,red,green,blue)
    unicorn.show()

# repeat game loop
while True:

    # initilse game conditions
    sea = create_sea()
    ammo = 20
    ships = 10
    game_over = False

    # display game grid on unicorn hat
    display_sea(sea)

    # repeat until end of game condition met
    while game_over == False:

        # display remaining ammo
        print("You have",ammo,"shots left")
        
        # capture x co-ordinate from player
        x = int(input("Enter the x co-ordinate for your shot: ")) - 1

        while x not in (0,1,2,3,4,5,6,7):
            print(x+1,"is not in range try again")
            x = int(input("Enter the x co-ordinate for your shot: ")) - 1
            
        for a in range(8):
            unicorn.set_pixel(x,a,0,150,0)
            unicorn.show()
            time.sleep(0.05)
                

        # capture y co-ordinate from player
        y = 8 - int(input("Enter the y co-ordinate for your shot: "))

        while y not in (0,1,2,3,4,5,6,7):
            print(8 - y,"is not in range try again")
            y = 8 - int(input("Enter the y co-ordinate for your shot: "))

        for a in range(8):
            unicorn.set_pixel(a,y,0,150,0)
            unicorn.show()
            time.sleep(0.05)

        # pause 1 second
        time.sleep(1)

        # highlight aimed co-ordinate
        
        for a in range(0,255,5):
            unicorn.set_pixel(x,y,a,a,a)
            unicorn.show()
            
        for a in range(0,255,5):
            unicorn.set_pixel(x,y,255-a,255-a,255-a)
            unicorn.show()

        # check if shot is a hit
        # if a ship is hit flash green to unicorn hat and update number
        # of remaining ships and grid.
        # if the shot misses flash red to unicorn hat.
        # Update grid and display updated sea on the unicorn hat
        if sea[y][x] == 1:
            print("Hit")
            for repeat in range(3):
                flood_colour(0,255,0)
                time.sleep(0.2)
                flood_colour(0,0,0)
                time.sleep(0.2)
                
            sea[y][x] = 3
            ships = ships - 1
            display_sea(sea)
            
        else:
            print("Miss")
            for repeat in range(3):
                flood_colour(255,0,0)
                time.sleep(0.2)
                flood_colour(0,0,0)
                time.sleep(0.2)
                
            sea[y][x] = 2
            ammo = ammo - 1
            display_sea(sea)

        # check if either game over condition has been met.
        # if it has set game_over to true
        if ships == 0 or ammo == 0:
            game_over = True

    # if game was ended with no ammo
    # display defeat animation on unicorn hat
    if ammo == 0:
        print("Our fleet is defeated.")
        for y in range(8):
            for x in range(8):
                for fade in range(0,255,50):
                    unicorn.set_pixel(x,y,255,255-fade,255-fade)
                    unicorn.show()
            
    # if game was ended with no ships left
    # display victory animation on unicorn hat
    if ships == 0:
        print("Our fleet is victorious.")
        for y in range(8):
            for x in range(8):
                for fade in range(0,255,50):
                    unicorn.set_pixel(x,y,255-fade,255,255-fade)
                    unicorn.show()

    # prompt user for a rematch
    # if the user says no then break loop and end program
    repeat = input("Do you want a rematch? ").lower()

    if repeat in ["no","n"]:
        print("Thanks for playing")
        break
    

CraigT
Posts: 21
Joined: Mon Oct 27, 2014 10:45 pm

Re: Unicorn HAT Demo Ideas

Mon Feb 09, 2015 7:50 am

Timcw wrote:Hi, new here and was looking at some of the ideas that others might of had for their Unicorn Hats. I picked one up this week and decided to put Battleships onto it.
Genius. It did briefly occur to me that the unicorn would be good for battleships but I ruled it out as I thought I'd need two unicorn HATs or to program something that would enable two players to play over a network. It never occurred to me to play against the computer. I like the flood function too.

New Pi 2, HAT and ninja diffusion layer ordered and despatched. Can't wait to have a go. :mrgreen:

Timcw
Posts: 7
Joined: Sun Feb 08, 2015 12:43 pm

Re: Unicorn HAT Demo Ideas

Mon Feb 09, 2015 11:34 pm

Thanks. Still working on a few things with the code, at the moment the game only places the ships in the grid horizontally and I am continuing to tinker with different animations on the Unicorn Hat to keep the game interesting.

Have fun with the kit, though should warn you that the Unicorn Hat isn't playing nice with the RPi2 at the moment. Pimoroni are looking into the problem so hopefully a fix isn't far away/

Timcw
Posts: 7
Joined: Sun Feb 08, 2015 12:43 pm

Re: Unicorn HAT Demo Ideas

Mon Feb 16, 2015 7:18 pm

Have carried on tinkering with the Battleships program and have made it prettier and changed the grid generation algorithm to include vertical placement of boats.

Code: Select all

"""
Program: Battleships Unicorn Hat display
Author: Tim Mulford
Date: 16/02/2015
Version: 0.3
"""

#import libraries
import unicornhat as unicorn
import time, random

unicorn.brightness(0.2)
unicorn.rotation(180)

# create initial grid
def create_sea():
    # initialise blank grid
    grid = [[0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0]]

    # generate position for battleship
    orientation = random.choice(["horizontal","vertical"])
    
    if orientation == "horizontal":
        x = random.randint(0,3)
        y = random.randint(0,7)

        # place battleship
        for a in range(5):
            grid[y][x+a]=1

    else:
        x = random.randint(0,7)
        y = random.randint(0,3)

        for a in range(5):
            grid[y+a][x]=1

    # generate co-ordinate for cruiser
    orientation = random.choice(["horizontal","vertical"])
    
    if orientation == "horizontal":

        x = random.randint(0,5)
        y = random.randint(0,7)

        # check if the generated co-ordinates are clear
        # regenerate co-ordinates until a clear position is found
        while [grid[y][x],grid[y][x+1],grid[y][x+2]] != [0,0,0]:
            x = random.randint(0,5)
            y = random.randint(0,7)

        # place cruiser horizontally
        for a in range(3):
            grid[y][x+a]=1

    else:
        x = random.randint(0,7)
        y = random.randint(0,5)

        # check if the generated co-ordinates are clear
        # regenerate co-ordinates until a clear position is found
        while [grid[y][x],grid[y+1][x],grid[y+2][x]] != [0,0,0]:
            x = random.randint(0,7)
            y = random.randint(0,5)

        # place cruiser vertically
        for a in range(3):
            grid[y+a][x]=1    
    

    # generate co-ordintes for patrol boat   
    orientation = random.choice(["horizontal","vertical"])
    
    if orientation == "horizontal":

        x = random.randint(0,6)
        y = random.randint(0,7)

        # check if the geneated co-ordinates are clear
        # regenerate co-ordinates until a clear position is found
        while [grid[y][x],grid[y][x+1]] != [0,0]:
            x = random.randint(0,6)
            y = random.randint(0,7)

        # place patrol boat horizontally
        for a in range(2):
            grid[y][x+a]=1

    else:
        x = random.randint(0,7)
        y = random.randint(0,6)

        # check if the geneated co-ordinates are clear
        # regenerate co-ordinates until a clear position is found
        while [grid[y][x],grid[y+1][x]] != [0,0]:
            x = random.randint(0,7)
            y = random.randint(0,6)

        # place patrol boat vertically
        for a in range(2):
            grid[y+a][x]=1        

    # used for testing grid generated properly
    # displays grid to screen
    #for row in grid:
    #    print(row)
        
    return(grid)

# display the current state of the sea
# blue LED for undisturbed sea
# red LED for HIT
# unlit for MISS
def display_sea(sea):
    for y in range(8):
        for x in range(8):
            if sea[y][x] == 0 or sea[y][x] == 1:
                unicorn.set_pixel(x,y,79,106,226)
            if sea[y][x] == 2:
                unicorn.set_pixel(x,y,0,0,0)
            if sea[y][x] == 3:
                unicorn.set_pixel(x,y,255,0,0)
    unicorn.show()

# set the whole display to colour of the passed RGB code
def flood_colour(red,green,blue):
    for x in range(8):
        for y in range(8):
            unicorn.set_pixel(x,y,red,green,blue)
    unicorn.show()

# repeat game loop
while True:

    # initilse game conditions
    sea = create_sea()
    ammo = 20
    ships = 10
    game_over = False

    # display game grid on unicorn hat
    display_sea(sea)

    # repeat until end of game condition met
    while game_over == False:

        # display remaining ammo
        print("You have",ammo,"shots left")
        
        # capture x co-ordinate from player
        x = int(input("Enter the x co-ordinate for your shot: ")) - 1

        while x not in (0,1,2,3,4,5,6,7):
            print(x+1,"is not in range try again")
            x = int(input("Enter the x co-ordinate for your shot: ")) - 1
            
        for a in range(8):
            unicorn.set_pixel(x,a,0,150,0)
            unicorn.show()
            time.sleep(0.05)
                

        # capture y co-ordinate from player
        y = 8 - int(input("Enter the y co-ordinate for your shot: "))

        while y not in (0,1,2,3,4,5,6,7):
            print(8 - y,"is not in range try again")
            y = 8 - int(input("Enter the y co-ordinate for your shot: "))

        for a in range(8):
            unicorn.set_pixel(a,y,0,150,0)
            unicorn.show()
            time.sleep(0.05)

        # pause 1 second
        time.sleep(1)

        # highlight aimed co-ordinate
        
        for a in range(0,255,10):
            unicorn.set_pixel(x,y,a,a,a)
            unicorn.show()
            
        for a in range(0,255,10):
            unicorn.set_pixel(x,y,255-a,255-a,255-a)
            unicorn.show()

        # check if shot is a hit
        # if a ship is hit flash green to unicorn hat and update number
        # of remaining ships and grid.
        # if the shot misses flash red to unicorn hat.
        # Update grid and display updated sea on the unicorn hat
        if sea[y][x] == 1:
            print("Hit")
            for repeat in range(3):
                flood_colour(255,0,0)
                time.sleep(0.2)
                flood_colour(255,255,0)
                time.sleep(0.2)
            flood_colour(0,0,255)
            time.sleep(0.5)
            
            sea[y][x] = 3
            ships = ships - 1
            display_sea(sea)
            
        else:
            print("Miss")
            flood_colour(255,255,255)
            time.sleep(0.3)
            for a in range(0,255,10):
                flood_colour(0,0,a)
            #time.sleep(0.5)
                
            sea[y][x] = 2
            ammo = ammo - 1
            display_sea(sea)

        # check if either game over condition has been met.
        # if it has set game_over to true
        if ships == 0 or ammo == 0:
            game_over = True

    # if game was ended with no ammo
    # display defeat animation on unicorn hat
    if ammo == 0:
        print("Our fleet is defeated.")
        for fade in range(0,255):
            for y in range(8):
                for x in range(8):
                    unicorn.set_pixel(x,y,255,255-fade,255-fade)
            unicorn.show()
            
    # if game was ended with no ships left
    # display victory animation on unicorn hat
    if ships == 0:
        print("Our fleet is victorious.")
        for fade in range(0,255):
            for y in range(8):
                for x in range(8):
                    unicorn.set_pixel(x,y,255-fade,255,255-fade)
            unicorn.show()

    # prompt user for a rematch
    # if the user says no then break loop and end program
    repeat = input("Do you want a rematch? ").lower()

    if repeat in ["no","n"]:
        print("Thanks for playing")
        break
    

CraigT
Posts: 21
Joined: Mon Oct 27, 2014 10:45 pm

Re: Unicorn HAT Demo Ideas

Fri Feb 20, 2015 11:30 pm

Eventually after a bit of faff setting up the Unicorn HAT (Pi2 and case issues) I got the demos working - hoorah :D - my 5 year old was impressed :-), my 3 year old and wife were not so impressed :-(
Another few nights passed before I had chance to tinker again, then with quite a few Google searches I was able to work out the Python I needed and made a few successful variations of the standard demos.
Then tonight, with more time I managed to code my version of the spiral. :D :D :D
@ukscone - just noticed you posted your version of the spiral :roll:, I wish I'd remembered this - your code is much simpler to read and understand.
For what it's worth here's my spiral code:

Code: Select all

#!/usr/bin/env python

# Produces a spiral pattern on the Unicorn HAT
# Craig Thomas - 2015.02.20

# 0,0 is the corner of the Unicorn HAT furthest away from the the ethernet port.

import unicornhat as UH
import time

x = y = xlo = ylo = 0
xhi = yhi = 7
r = 255
g = 64
b = 255

def draw_dot( x1, y1, r1, g1, b1, str1):
  "This draws the dot and outputs some text"
  UH.set_pixel(x1,y1,r1,g1,b1)
  UH.show()
  print (str1 + str(x1) + ' ' + str(y1))
  time.sleep(0.05)
  return

  
for ring in range(4):
  y = ylo
  for x in range(xlo, xhi+1):
    draw_dot(x,y,r,g,b,'W ');
  x = xhi
  for y in range(ylo+1, yhi+1):
    draw_dot(x,y,r,g,b,'N ');
  y = yhi
  for x in range(xhi-1, xlo, -1):
    draw_dot(x,y,r,g,b,'E ');
  x = xlo
  for y in range(yhi, ylo, -1):
    draw_dot(x,y,r,g,b,'S ');
  xlo += 1
  xhi -= 1
  ylo += 1
  yhi -= 1

time.sleep(1)
@timcw - loving the battleships idea. Hopefully next time I get chance to play with the Pi, I'll give it whirl.

I also managed to modify my code above to work with a "pattern.csv" file, so I can specify a list of pixels to turn on in order. My file format is:
x,y,r,g,b,delay
This works great, but I am already thinking about how I could write it to change several pixels at a time (I need to do more testing with delay=0 and maybe lose the output to screen).

Hope this helps,

Craig

simon_prickett
Posts: 1
Joined: Sat Apr 18, 2015 12:53 am

Re: Unicorn HAT Demo Ideas

Sat Apr 18, 2015 1:05 am

I wrote a flappy bird game for the Unicorn hat and did a couple of versions... one that uses the Enter key on a keyboard, the other using GPIO and separate wired buttons for start and flap. I put this in a Gameboy and wired up the original controls using a common ground PCB from Kitsch-Bent.

The game gets faster (harder) every so many pipes and the pipes change colour as you successfully go through them. The score is represented by individual LEDs that light on game over, or a big 0 if you didn't score anything.

Source is on Github at https://github.com/simonprickett/flappypi

Video example of it running in the Gameboy: https://www.youtube.com/watch?v=yT1DzEbvj4Y

I used car window tint to diffuse the LEDs and had to also dremel out the Gameboy screen aperture a bit to make all 64 LEDs visible. Here's another video that shows a bit more before I got the window tint done: https://www.youtube.com/watch?v=mRjUC8TcPNQ

Inside it's an A+ Raspberry Pi with 9 GPIO pins used for the 8 gameboy buttons plus one common ground, and 3 for the Unicorn hat -- needed to fit this via trailing wires to make it fit nicely in the Gameboy and also to allow me to wire up the control PCB too.

It looks like this inside https://instagram.com/p/1UXLLdgbqe accessing the SD card is painful but it has a wifi adaptor so I just SSH in and change things.

Feel free to use the code if you find it useful/fun.

Simon.

Timcw
Posts: 7
Joined: Sun Feb 08, 2015 12:43 pm

Re: Unicorn HAT Demo Ideas

Fri Aug 28, 2015 6:56 pm

Just to return to this idea again. I've found a bit of spare time and have returned to the code again and had a further play.

Hopefully it is a bit easier for others to tinker with. I've made the game area a class and made quite a lot into methods. Also I have tidied up the procedure for placing boats and now have one method to place boats of any size.

Anyhow, if you have a Unicorn hat feel free to have a play with the code and tinker.

Code: Select all

"""
Program: Battleships Unicorn Hat display
Author: Tim Mulford
Date: 28/08/2015
Version: 0.5

Changes:
I have update the code to make the game area a class with methods for the game. 
"""

import unicornhat as unicorn
import time, random

# initialise Unicornhat board
# Unless you are using a difusser keep brightness low
unicorn.brightness(0.05)
unicorn.rotation(180)

# ocean stores the information about the game area and the methods required to
# interact with it

class ocean:

    ships = 0
    ammo = 0
    water = [[0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0]]

    # Reset the game board
    def reset(self):

        self.water = [[0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0]]
    
    # Display the current board state on the Unicorn hat
    def display_water(self):

        for y in range(8):
            for x in range(8):

                if self.water[y][x] == 0 or self.water[y][x] == 1:
                    
                    unicorn.set_pixel(x,y,79,106,226)

                elif self.water[y][x] == 2:
                    
                    unicorn.set_pixel(x,y,0,0,0)

                elif self.water[y][x] == 3:
                    
                    unicorn.set_pixel(x,y,255,0,0)

        unicorn.show()

    # Set every light on the board to the set colour
    def flood_colour(self,red,green,blue):

        for x in range(8):
            for y in range(8):

                unicorn.set_pixel(x,y,red,green,blue)

        unicorn.show()

    # Animation for when a shot is a hit
    def hit_flash(self,count):
        
        for repeat in range(count):
                    
            self.flood_colour(255,0,0)
            time.sleep(0.2)

            self.flood_colour(255,255,0)
            time.sleep(0.2)
            
        self.flood_colour(0,0,255)
        time.sleep(0.5)

    # Animation for when a shot is a miss
    def miss_flash(self):

        self.flood_colour(255,255,255)
        time.sleep(0.3)
        
        for a in range(0,255,10):
            
            self.flood_colour(0,0,a)

    # Animation for targetting using passed co-ordinates
    def target_flash(self,x,y):

        for a in range(8):
            
            unicorn.set_pixel(x,a,0,200,0)
            unicorn.show()
            time.sleep(0.05)
                 
        for a in range(8):
            
            unicorn.set_pixel(a,y,0,200,0)
            unicorn.show()
            time.sleep(0.05)

        time.sleep(1)
        
        for a in range(0,255,10):
            
            unicorn.set_pixel(x,y,a,a,a)
            unicorn.show()
            time.sleep(0.01)
            
        for a in range(0,255,10):
            
            unicorn.set_pixel(x,y,255-a,255-a,255-a)
            unicorn.show()
            time.sleep(0.01)

    # Animation for when the game is lost
    def defeated(self):
        
        for fade in range(0,255):
            for y in range(8):
                for x in range(8):
                    
                    unicorn.set_pixel(x,y,255,255-fade,255-fade)

            unicorn.show()
            time.sleep(0.01)

    # Animation for when the game is won
    def victory(self):

        for fade in range(0,255):
            for y in range(8):
                for x in range(8):
                    
                    unicorn.set_pixel(x,y,255-fade,255,255-fade)
                    
            unicorn.show()
            time.sleep(0.01)
    
    # Place a boat of the passed size on the game board
    # There is no limit to the number of boats placed though
    # if too many boats are placed it might not be possible to
    # place more and the program will get stuck in an infinite loop
    # A "1" is placed in the water 2D array for each part of the boat
    # The ships variable is updated to reflect the number of boat parts
    # on the game area in total after the boat it placed.
    def boat(self, size):

        boat_mask = []

        for a in range(size):
            
            boat_mask.append(0)
        
        orientation = random.choice(["horizontal","vertical"])
        
        if orientation == "horizontal":
            
            x = random.randint(0,8-size)
            y = random.randint(0,7)

            placement = []

            for a in range(size):
                
                placement.append(self.water[y][x+a])
            
            while placement != boat_mask:

                x = random.randint(0,8-size)
                y = random.randint(0,7)

                placement = []

                for a in range(size):
                    
                    placement.append(self.water[y][x+a])
            
            for a in range(size):
                
                self.water[y][x+a] = 1

        else:
            
            x = random.randint(0,7)
            y = random.randint(0,8-size)

            placement = []

            for a in range(size):
                
                placement.append(self.water[y+a][x])

            while placement != boat_mask:
                
                x = random.randint(0,8-size)
                y = random.randint(0,7)

                placement = []

                for a in range(size):
                    
                    placement.append(self.water[y+a][x])

            for a in range(size):
                
                self.water[y+a][x]=1

        self.ships += size

    # Process a shot using the passed co-ordinates on the game area
    # determine if the shot is a hit, miss or an already hit area.
    # The game area is then updated and the updated game area is displayed
    # on the unicorn hat.
    def shot(self,x,y):

        if self.water[y][x] == 1:

            print("Hit")

            self.water[y][x] = 3

            self.hit_flash(3)
            
            self.ships -= 1

        elif self.water[y][x] == 2:

            print("You have already hit this area.")

        else:

            print("Miss")

            self.water[y][x] = 2

            self.miss_flash()
        
            self.ammo -= 1        

        self.display_water()
        
# Enter and validate a co-ordinate
def enter_coord(direction):

    valid = False

    while valid != True:

        try:

            co_ord = int(input("Enter the "+direction+"-cordinate for your shot: "))

            if co_ord in range(1,9):
                
                valid = True

            else:
                
                print("Must be a number between 1 and 8.")

        except:
            
            print("Must be an number between 1 and 8.")

    if direction == "x":

        co_ord = co_ord - 1

    else:

        co_ord = 8 - co_ord
        
    return co_ord

# Main Program

# Declare an instance of the game area
ocean = ocean()

# Main program loop 
while True:
   
    # Initialise the game area
    ocean.reset()
    ocean.boat(5)
    ocean.boat(3)
    ocean.boat(3)
    ocean.boat(2)
    ocean.boat(2)
    ocean.ammo = 20
    game_over = False

    # Display the initial game area to the Unicornhat
    ocean.display_water()

    # Loop until game over conditions are met
    while game_over == False:

        # Display current game stats to the screen
        print("\nYou have",ocean.ammo,"shots left")
        print("There are",ocean.ships,"targets in our waters.")
        print("")
        
        # Get x and y co-ordinates of the shot
        x = enter_coord("x")    
        y = enter_coord("y")

        # Targetting animation
        ocean.target_flash(x,y)
        
        # Process the shot
        ocean.shot(x,y)

        # Check if the game over conditions are met
        if ocean.ships == 0 or ocean.ammo == 0:

            game_over = True

    # If the game is over because it is lost display game over
    if ocean.ammo == 0:
        
        print("\nOur fleet is defeated.")
        ocean.defeated()
            
    # Else if it is over because the game is won display victory
    elif ocean.ships == 0:
        
        print("\nOur fleet is victorious.")
        ocean.victory()

    # Ask if the player wants another game
    repeat = input("\nDo you want a rematch? ").lower()

    # If the player doesn't want to play again quit the program
    if repeat in ["no","n"]:
        print("Thanks for playing")
        break

bbx10node
Posts: 6
Joined: Wed Aug 08, 2012 5:51 am

Re: Unicorn HAT Demo Ideas

Thu Sep 17, 2015 5:55 am

The Glediator and PixelController programs generate pixel animation for LED grids and strips. They are usually used to drive large installations at concerts and nightclubs but also work with the Unicorn Hat 8x8 LED grid. Install the driver from github to add Art-Net network protocol support to the Unicorn.

The driver also supports the Open Pixel Control (OPC) protocol created for the Fadecandy controller. Most OPC demo programs including ones written in Processing can now control the Unicorn LEDs. This driver does not have the advanced features of the Fadecandy controller so the results are not as good but still dazzling.

The Art-Net and OPC driver: https://github.com/bbx10/artnet-unicorn-hat

References

http://www.solderlab.de/index.php/software/glediator
https://github.com/neophob/PixelController/releases
http://openpixelcontrol.org/

evilunix
Posts: 1
Joined: Sun Dec 20, 2015 6:38 pm

Re: Unicorn HAT Demo Ideas

Sun Dec 20, 2015 6:43 pm

I wrote this code for a CPU vs CPU game of pong with rainbow colours... Enjoy!

Code: Select all

import unicornhat as UH
import time
import random
import colorsys as col

#UH.brightness(1) # max brightness :D

SPEED = 0.1

# convert hex to rgb tuple
def hex_to_rgb(value):
	value = value.lstrip('#')
	lv = len(value)
	return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))


class Player:

	def __init__(self, hue):
		self.hue = hue
 
	direction_x = 0 # 1 = up, 2 = down
	y = 0 # position of topmost pixel

	def spawn(self):
		self.y = random.randint(0,5)

	def think(self, ball):

		# change colour
		if self.hue < 360:
			self.hue = self.hue + 1
		else:
			self.hue = 0

        	rand = random.randint(0,19)
        	if(rand == 13):
                	derp = 1
        	else:
                	derp = 0

        	if ball.y > self.y + 1 and self.y < 5:
                	# ball is below player center - move down
                	if derp == 0:
                        	self.y = self.y + 1


        	if ball.y < self.y + 1 and self.y > 0:
                	# ball is above player center - move up
                	if derp == 0:
                        	self.y = self.y - 1

		

class Ball:
    
	direction_x = 0 # 1 = up, 2 = down
    	direction_y = 0 # 1 = left, 2 = right
    
    	x = 0
    	y = 0
    
    	colour = hex_to_rgb('ffffff')
    
    	def spawn(self):

		print 'spawning ball...'

        	# ball starting directions
        	self.direction_x = random.randint(1,2)
		self.direction_y = random.randint(1,2)
	
		# ball starting position
		self.x = random.randint(3,4)
		self.y = random.randint(3,4)	


player1 = Player(0)
player2 = Player(90)
ball = Ball()


def update():
	global ball, player1, player2

	player1.think(ball)
	player2.think(ball)
    
    	# check if ball hit upper or lower wall
    	if ball.y == 0:
        	#change direction
        	ball.direction_y = 2 # down
    	if ball.y == 7:
        	# change direction
        	ball.direction_y = 1 # up
    
    	if ball.direction_y == 2: # down
        	# move 1 cell down
        	ball.y = ball.y + 1
    	elif ball.direction_y == 1: #up
        	# move 1 cell up
        	ball.y = ball.y - 1
        
    
    	if ball.direction_x == 1: # moving left
		
		ball.x = ball.x - 1
		
        	if ball.x == 0:
           		# check for collision
			if ball.y >=  player1.y and ball.y <= player1.y + 3:
				print 'SAVE!'
				#change direction
				ball.direction_x = 2
				ball.x = ball.x + 1
			else: 
           			# GOAL!
				print 'GOAL!'
				goal()
				ball.spawn()
					

    	if ball.direction_x == 2: # moving right

		ball.x = ball.x + 1

        	if ball.x == 7:
            		# check for collision
			if ball.y >= player2.y and ball.y <= player2.y + 3:
				print 'SAVE!'
				# change direction
				ball.direction_x = 1
				ball.x = ball.x - 2
			else: 	
				# GOAL!
				goal()
				print 'GOAL!'
            			ball.spawn()

def goal():
	global ball

	draw()
	time.sleep(SPEED)

	set_pixel(ball.x, ball.y, hex_to_rgb('000000'))
	UH.show()
	time.sleep(SPEED)
	
	set_pixel(ball.x, ball.y, ball.colour)
	UH.show()
	time.sleep(SPEED)

	set_pixel(ball.x, ball.y, hex_to_rgb('000000'))
	UH.show()
	time.sleep(SPEED)


        set_pixel(ball.x, ball.y, ball.colour)
        UH.show()
        time.sleep(SPEED)
	
	

def set_pixel(x, y, colour):
	UH.set_pixel(x, y, colour[0], colour[1], colour[2])

def fill_hex(colour):
	rgb = hex_to_rgb(colour)
	for x in range(0, 8):
		for y in range(0, 8):
			set_pixel(x, y, rgb)
	UH.show()


def set_pixel_hue(x, y, h):

	hfloat = h / 255.0	

	rgb = col.hsv_to_rgb(hfloat, 0.5, 1.0)
	r = int(rgb[0] * 255)
	g = int(rgb[1] * 255)
	b = int(rgb[2] * 255)
	UH.set_pixel(x, y, r, g, b)  


def draw():
    	global ball, player1, player2, BG_COLOUR
    	UH.off()

	#fill_hex(BG_COLOUR)

	# draw ball
    	set_pixel(ball.x, ball.y, ball.colour)

	# draw player1
	set_pixel_hue(0, player1.y, player1.hue)
	set_pixel_hue(0, player1.y + 1, player1.hue)
	set_pixel_hue(0, player1.y + 2, player1.hue)

    	# draw player2
        set_pixel_hue(7, player2.y, player2.hue)
        set_pixel_hue(7, player2.y + 1, player2.hue)
        set_pixel_hue(7, player2.y + 2, player2.hue)
	

    	UH.show()


player1.spawn()
player2.spawn()    
ball.spawn()
draw()

while True:
	
    	time.sleep(SPEED)
    	update()
    	draw()

    


kaykasus
Posts: 3
Joined: Thu Mar 09, 2017 7:15 pm

Re: Unicorn HAT Demo Ideas

Wed Apr 05, 2017 8:05 pm

thx guys for your codes. I like the spiral a lot. Do you have any other examples to share? I am new to python coding and could learn a lot by your codes.

voinageo
Posts: 1
Joined: Wed Oct 25, 2017 12:14 pm

Re: Unicorn HAT Demo Ideas

Wed Oct 25, 2017 12:24 pm

Hi all, a very nice thread to start. I was looking for some demos for my Unicorn HAT HD and stumbled over this thread.
My contribution to this: I ported the Pong and Battleship programs to the Unicorn HAT HD https://blog.voina.org/raspberrypi-pyth ... rn-hat-hd/.

Timcw
Posts: 7
Joined: Sun Feb 08, 2015 12:43 pm

Re: Unicorn HAT Demo Ideas

Tue Nov 28, 2017 8:57 pm

Finally got round to picking up a UnicornHatHD and have programmed a visualisation for a Bubblesort. It's a little rough and ready, but works quite nicely and looks great on the Hat.

I have included three randomisers (one each for Red, Green and Blue) and in the Bubblesort function the colour argument takes a value of 0, 1 or 2 for RGB respectively.

Code: Select all

"""
Program: Bubble Sort
Author: Tim Mulford
Date: 28/11/2017
Bubblesort LEDS on Unicorn HAT HD by brightness
"""

from random import *
from unicornhathd import *

def randomise_red():
    pixels = []

    for i in range(256):
        pixel = [randint(0,255),0,0]
        pixels.append(pixel)
    return pixels

def randomise_green():
    pixels = []

    for i in range(256):
        pixel = [0,randint(0,255),0]
        pixels.append(pixel)
    return pixels

def randomise_blue():
    pixels = []

    for i in range(256):
        pixel = [0,0,randint(0,255)]
        pixels.append(pixel)
    return pixels

def display(pixels):        
    for i in range(256):
        set_pixel(i//16,i%16,pixels[i][0],pixels[i][1],pixels[i][2])

    show()

# colour use 0 for R, 1 for G, 2 for B
def bubblesort(pixels,colour):
    for i in range(255):
        swapped = False
        for j in range(0,255-(i)):
            if pixels[j][colour] > pixels[j+1][colour]:
                temp = pixels[j][colour]
                pixels[j][colour] = pixels[j+1][colour]
                pixels[j+1][colour] = temp
                swapped = True
            display(pixels)
        if swapped == False:
            break

pixels=randomise_blue()

bubblesort(pixels,2)

Timcw
Posts: 7
Joined: Sun Feb 08, 2015 12:43 pm

Re: Unicorn HAT Demo Ideas

Sat Dec 02, 2017 1:25 pm

Tidied everything up into a class and added a couple of more colour options. The default option with no parameters is greyscale LEDs, but can use with parameters for "red", "green", "blue", "purple", "yellow", "grey" and "rgb". The RGB option works by comparing the average of the three colour channels.

Code: Select all

"""
Program: Bubble Sort
Author: Tim Mulford
Date: 28/11/2017
Bubblesort LEDS on Unicorn HAT HD by brightness
"""

from random import *
from unicornhathd import *


class Bubblesort():

    def __init__(self, colour = "grey"):

        self.pixels = []        

        for i in range(256):
            if colour.lower() == "red":
                pixel = [randint(0,255),0,0]
            elif colour.lower() == "green":
                pixel = [0,randint(0,255),0]
            elif colour.lower() == "blue":
                pixel = [0,0,randint(0,255)]
            elif colour.lower() == "rgb":        
                pixel = [randint(0,255),randint(0,255),randint(0,255)]
            elif colour.lower() == "purple":
                purple = randint(0,255)
                pixel = (purple,0,purple)
            elif colour.lower() == "yellow":
                yellow = randint(0,255)
                pixel = (yellow,yellow,0)
            elif colour.lower() == "grey":
                grey = randint(0,255)
                pixel = [grey,grey,grey]
            self.pixels.append(pixel)
        self.display()
        self.bubblesort()

    def display(self):        
        for i in range(256):
            set_pixel(i//16,i%16,self.pixels[i][0],self.pixels[i][1],self.pixels[i][2])
        show()

    def bubblesort(self):
        for i in range(255):
            swapped = False
            for j in range(0,255-(i)):
                if self.pixels[j][0]+self.pixels[j][1]+self.pixels[j][2] > self.pixels[j+1][0]+self.pixels[j+1][1]+self.pixels[j+1][2]:
                    temp = self.pixels[j]
                    self.pixels[j] = self.pixels[j+1]
                    self.pixels[j+1] = temp
                    swapped = True
                self.display()
            if swapped == False:
                break

unicorn = Bubblesort()

CraigT
Posts: 21
Joined: Mon Oct 27, 2014 10:45 pm

Re: Unicorn HAT Demo Ideas

Fri Dec 08, 2017 11:56 pm

Thanks all.
Unicorn HAT HD on the way for Christmas.

Currently about to start some work on a xmas display for the old Unicorn HAT (non-HD). Thinking of rewriting the show-png.py program from the Unicorn HAT HD, then using it to show some Xmas pixel art (mostly desinged by me - snowman, xmas tree, candy cane, stocking etc.) and possibly adding the falling snow demo as a transition.

Keep coding...

datagod
Posts: 2
Joined: Tue Dec 12, 2017 6:10 am

Re: Unicorn HAT Demo Ideas

Tue Dec 12, 2017 6:18 am

I was at a Pinball tournament in Florida and a guy there had a huge LED matrix that displayed various animations and the time. When I saw the Unicorn HAT, I though "man, I bet I can do something sweet with that!"

4 months later, I know have a clock with scrolling messages and animations. It also plays 6 games (by itself) in a sort of attract mode.

Here is a video of my work so far.

https://www.youtube.com/watch?v=5JwyETzr-BU

CraigT
Posts: 21
Joined: Mon Oct 27, 2014 10:45 pm

Re: Unicorn HAT Demo Ideas

Wed Jan 03, 2018 3:19 pm

Well done all, I really like the unicornhat games!

I am very pleased with my new xmas Unicorn HAT HD, it's fab. The only issue I have had so far, has been getting a case to fit!

(Optional reading) I ordered a new Pi 3 to go with the UH-HD and ordered the special limited edition Midnight Pi-bow case (https://shop.pimoroni.com/products/pibow-midnight) and an official black case too (https://www.modmypi.com/raspberry-pi/ca ... -grey-case).
Unless Pimoroni have changed the website info, it appears I completely missed that the midnight case does not fit the Pi 3 (which I only found out when trying to put it together). The official case didn't fare much better either as it does not allow enough room for the unicorn hat hd! (please reply if someone has managed to shoe-horn it in)

Anyway, I thought the new UH-HD would look good with a chessboard pattern with ranging colours 2-pixel wide squares on the 16x16 display. I had the "white spaces" cycling through many colours (hues) and the "black spaces" going through from black to white, so that all the major colour combinations were shown. Unfortunately, it didn't look as impressive as it seemed in my head.

During my googling to help with the python syntax and my algorithms, I came across these useful pages which I thought were worth sharing:
http://richardhayler.blogspot.co.uk/201 ... tions.html
https://github.com/jayniz/unicorn-hat-sim
http://sandyjmacdonald.github.io/2015/0 ... gonometry/

Given a lot more time, I would quite like to modify Richard Hayler's code to make it work for the UH-HD and add a few GUI/UI tweaks. Ah well, maybe someday.

Thanks all. (Off to buy yet another Pi case)

Craig

CraigT
Posts: 21
Joined: Mon Oct 27, 2014 10:45 pm

Re: Unicorn HAT Demo Ideas

Thu Jan 18, 2018 11:40 pm

I came up with the idea of creating a power on animation using the Unicorn Hat HD. I also wanted a little indication that there was power still going to the Pi so I decided to leave a single red dot lit. My plan is to tie this in to a start up script.

(I left a bit of debugging code commented out)

Code: Select all

#!/usr/bin/python
# -*- coding: latin-1 -*-


import unicornhathd as uhhd
import numpy as np
import time
from sys import exit



#
#  A Power On Animation
#

#  © Craig Thomas - 2018.01.18

print("Unicorn HAT HD: Power On Demo")


## Set up some initial settings

uhhd.rotation( 270 )  ## Turns so that the point 15,15 is by the power in cable
uhhd.brightness( 0.4 )  ## Bright but not too bright (used with diffuser)

## Size of the Unicorn Hat HD
width = 16
height = 16

## The text "Pi On" in matrix with 1 = Blue, 2 = Green
PiOn = np.array(  [ [1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0],
                    [1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0],
                    [1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0],
                    [1, 1, 1, 0, 0, 1, 0, 2, 0, 0, 2, 0, 2, 2, 2, 0],
                    [1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2],
                    [1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2],
                    [1, 0, 0, 0, 0, 1, 0, 0, 2, 2, 0, 0, 2, 0, 0, 2] ] )

PiOn = np.flipud(PiOn)  ## Flip the matrix upside down to match co-ordinates
#print(PiOn)

r = 255  ## Use red for the dots and line
g = 0
b = 0

x = 0
y = 0



## Flash The Dot (Bottom Left)
##

flash_count = 3

for i in range(0,flash_count):
    
    uhhd.set_pixel( x,y, r,g,b )
    uhhd.show()
    time.sleep( 0.2 )
    
    uhhd.off()
    time.sleep( 0.1 )
    


## Stretch The Dot Across The Bottom Line
##

uhhd.set_pixel( x,y, r,g,b )
uhhd.show()

for x in range(0,width):
    
    uhhd.set_pixel( x,y, r,g,b )
    uhhd.show()
    time.sleep( 0.05 )

time.sleep( 0.4 )



## Lift A Duplicate Line To The Top
##

for y in range(1,16):  ## Not needed for bottom line = 0
    
    ## Draw horizontal Line
    for x in range(0,16):
        
        uhhd.set_pixel( x,y, r,g,b )
        if ( y != 1 ):  ## Don't remove the line before the top of the grid
            ## Draw a black line to remove the previous red line
            uhhd.set_pixel( x,y-1, 0,0,0 )
        
    uhhd.show()
    time.sleep( 0.03 )
    
time.sleep( 0.5 )




## Fade In - "Pi On"
##

## Get dimensions of the text matrix
th, tw = np.shape(PiOn)
#print(tw,th)

## Text starting point
tx = 0  ## Text starts against the left side
ty = 5  ## Text starts on the 6th line up

## Use factor to increase the amount of brightness of each colour
for factor in range(0,256,17):
    #print factor
    
    for x in range(tw):
        for y in range(th):
            #print "x",x,"tx+x",tx+x
            #print "                  y",y,"ty+y",ty+y
            if ( PiOn[y][x] == 1 ):
                uhhd.set_pixel( tx+x, ty+y, 0, 0, factor )  ## Blue
            if ( PiOn[y][x] == 2 ):
                uhhd.set_pixel( tx+x, ty+y, 0, factor, 0 )  ## Green
    uhhd.show()
    time.sleep(0.1)

uhhd.show()
time.sleep(1)



## Drop Top Line Down, Erasing Text
##

for y in range(15,0,-1):  ## Not needed for bottom line =0
    
    ## Draw horizontal Line
    for x in range(0,16):
        
        ## Draw the red line
        uhhd.set_pixel( x,y, r,g,b )
        if ( y != 15 ):
            ## Draw a black line to remove the previous red line
            uhhd.set_pixel( x,y+1, 0,0,0 )
        
    uhhd.show()
    time.sleep( 0.03 )
    
## Wipe out the last but one line
for x in range(0,16):
        
    uhhd.set_pixel( x,1, 0,0,0 )

uhhd.show()
time.sleep( 0.4 )




## Shrink Line Across To Bottom Left Dot
##

for x in range(width-1,0,-1):
    
    uhhd.set_pixel( x,0, 0,0,0 )
    uhhd.show()
    time.sleep( 0.05 )

time.sleep( 0.5 )  

    
##  Flash Dot (Bottom Left)
##

for i in range(0,flash_count):
    
    uhhd.set_pixel( 0,0, r,g,b )
    uhhd.show()
    time.sleep( 0.2 )
    
    uhhd.off()
    time.sleep( 0.1 )
    
    
uhhd.set_pixel( 0,0, r,g,b )
uhhd.show()


quit()


CraigT
Posts: 21
Joined: Mon Oct 27, 2014 10:45 pm

Re: Unicorn HAT Demo Ideas

Fri Jan 19, 2018 6:54 pm

Just added a video of the splash screen / boot animation:

https://youtu.be/mopXVMo726A

Timcw
Posts: 7
Joined: Sun Feb 08, 2015 12:43 pm

Re: Unicorn HAT Demo Ideas

Sat Feb 03, 2018 3:37 pm

Started a new project, generating and displaying a randomly generated maze on the unicorn hat. I have the maze generation work and it adds an entrance to the top of the maze and an exit at the bottom of the maze. Next step is to add maze solver.

Code: Select all

"""
Program: Maze Generator
Author: Tim Mulford
Date: 03/02/2018
Description: Program that generates a random maze and displays on UnicornHD Hat
"""

from random import *
from unicornhathd import *

class Maze():
    
    
    def __init__(self):
        
    # initialise the array to store the maze
    # maze size is 15 as it must be an odd number
        self.array_size = 15
        self.maze_array = []

        for j in range(self.array_size):
            self.maze_row = []
            for i in range(self.array_size):
                self.maze_row.append(0)
            self.maze_array.append(self.maze_row)
            
        self.generate_maze(randint(0,(self.array_size-3)/2)*2+1, randint(0,(self.array_size-3)/2)*2+1)
        self.entrance = self.add_entrance()
        self.goal = self.add_goal()
        self.display_maze()

    # determine whether a position is inside the grid or not            
    def on_grid(self,x,y):
        if x<1 or x>self.array_size-2:
            return False
        elif y<1 or y>self.array_size-2:
            return False
        else:
            return True
                
    # recursive algorithm to generate maze, initial values for x and y
    # must be odd
    def generate_maze(self,x,y):

        self.maze_array[y][x] = 1
         
        poss_moves = [[0,-2],[0,2],[-2,0],[2,0]]
        shuffle(poss_moves)

        self.display_maze()

        for (move_x,move_y) in poss_moves:
            
            if self.on_grid(move_x+x,move_y+y) and self.maze_array[y+move_y][x+move_x] == 0:
                self.maze_array[y+move_y][x+move_x] = 1
                self.maze_array[int((y*2+move_y)/2)][int((x*2+move_x)/2)] = 1
                y += move_y
                x += move_x
                self.generate_maze(x,y)

    # add entrance to the maze and return co-ordinates
    def add_entrance(self):
        entrance = randint(0,(self.array_size-3)/2)*2+1
        while self.maze_array[1][entrance] == 0:
            entrance = randint(0,(self.array_size-3)/2)*2+1
        self.maze_array[0][entrance]=1
        return [0,entrance]

    # add goal to the maze and return co-ordinates
    def add_goal(self):
        goal = randint(0,(self.array_size-3)/2)*2+1
        while self.maze_array[13][goal] == 0:
            goal = randint(0,(self.array_size-3)/2)*2+1
        self.maze_array[14][goal]=1
        return [0,goal]
    
    # display maze on hat
    def display_maze(self):        
        for j in range(self.array_size):
            for i in range(self.array_size):
                if self.maze_array[j][i] == 0:
                    set_pixel(j,i,255,255,255)
                else:
                    set_pixel(j,i,0,0,0)
        show()
                    
                

m = Maze()


rickhewes
Posts: 1
Joined: Sat Feb 17, 2018 2:25 pm

Re: Unicorn HAT Demo Ideas

Sat Feb 17, 2018 2:31 pm

Hi,

I made a small 'glowing' weather app that shows observable and 3 day forecast weather in a indicative way. https://github.com/rickhewes/raspberry- ... ng-weather

Image

Happy coding...

Outpost31d
Posts: 29
Joined: Sun Nov 20, 2016 5:27 am

Re: Unicorn HAT Demo Ideas

Sat May 19, 2018 9:44 pm

Hi I tried to run both your battle ship and pong programs on my unicorn hd but came up with multiple errors in thonny, indentation mostly?

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 6 guests