Page 1 of 1

Animating The Sense Hat

Posted: Sun Oct 11, 2015 1:03 am
by Woll
I sort of grew up with the ZX81 and Commodore 16 Basic, although I was never any good at it.
I'm playing around with the Sense Hat, which I'm rather enjoying, but programming has moved on a touch since I was young.
I'm trying to get the Sense Hat to animate two space invader images and go back to the first image in the program once it has displayed the last.
In Basic the command would have been goto and the line number of the image. But Python is more up to date than Basic and doesn't recognise it.
Does anyone know the Python equivalent goto command?

Code: Select all

# Space Invaders

from sense_hat import SenseHat
import time
sense = SenseHat()

x = (255,0,0)
o = (255,255,255)

image_1 = [
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,x,x,x,x,x,x,o,
    x,o,o,o,o,o,o,x,
    x,o,o,o,o,o,o,x,
    x,x,o,o,o,o,x,x,
    ]

image_2 = [
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,x,x,x,x,x,x,o,
    o,x,o,o,o,o,x,o,
    o,x,o,o,o,o,x,o,
    o,x,x,o,o,x,x,o,
    ]

sense.set_pixels(image_1)
time.sleep(1)
sense.set_pixels(image_2)
time.sleep(1)
goto 10

Re: Animating The Sense Hat

Posted: Sun Oct 11, 2015 9:45 am
by AndersM
Use a while loop.

An infinite loop:
Basic

Code: Select all

10 do things
20 goto 10
Python

Code: Select all

while True :
     do things

Re: Animating The Sense Hat

Posted: Sun Oct 11, 2015 11:34 am
Don't worry ... BASIC (RTB) will soon understand the display on the sense HAT, (along with everything else), however I'd still recommend not using goto as RTB supports more structured forms of looping...
-Gordon

Re: Animating The Sense Hat

Posted: Sun Oct 11, 2015 5:57 pm
by Woll
Thanks for the replies.
I'm not really understanding loops as I can't follow the code in my head at the moment. Hopefully it will click.

Re: Animating The Sense Hat

Posted: Sun Oct 11, 2015 7:47 pm
by Woll
Hi, I'm finding while loops a bit of a challenge.
Dose anyone know whiy my while loops not working?

Code: Select all

# Space Invaders

from sense_hat import SenseHat
import time
sense = SenseHat()

x = (255,0,0)
o = (255,255,255)

image_1 = [
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,x,x,x,x,x,x,o,
    x,o,o,o,o,o,o,x,
    x,o,o,o,o,o,o,x,
    x,x,o,o,o,o,x,x,
    ]

image_2 = [
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,x,x,x,x,x,x,o,
    o,x,o,o,o,o,x,o,
    o,x,o,o,o,o,x,o,
    o,x,x,o,o,x,x,o,

n=0
while n <= 10:
    sense.set_pixels(image_1)
    time.sleep(1)
    sense.set_pixels(image_2)
    time.sleep(1)

    n=n+1

Re: Animating The Sense Hat

Posted: Sun Oct 11, 2015 11:37 pm
by billintad
Well you could start by printing out the value of n as it increments in the loop and seeing what happens, that would give you an idea of what is happening in your while loop.

Re: Animating The Sense Hat

Posted: Mon Oct 12, 2015 8:04 am
by AndersM
If the code is the exact code in your program there are errors in the syntax of the lists:
Remove the last ,

Code: Select all

image_1 = [
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,x,x,x,x,x,x,o,
    x,o,o,o,o,o,o,x,
    x,o,o,o,o,o,o,x,
    x,x,o,o,o,o,x,x,
    ]
Remove the last , and add a closing ]

Code: Select all

image_2 = [
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,o,x,x,x,x,o,o,
    o,x,x,x,x,x,x,o,
    o,x,o,o,o,o,x,o,
    o,x,o,o,o,o,x,o,
    o,x,x,o,o,x,x,o,

The while loop is fine.
If you want to loop a certain number of times can use a for loop, almost like in BASIC, like this

Code: Select all

for n in range(10) :
  #do things

Re: Animating The Sense Hat

Posted: Tue Oct 13, 2015 12:59 am
by Woll
Thanks for the replies. Yep I forgot to close the indent off. Thanks for the tip on the other loop as well.