Woll
Posts: 472
Joined: Mon Jul 06, 2015 2:14 am
Location: Cloud Cuckoo Land

Animating The Sense Hat

Sun Oct 11, 2015 1:03 am

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
Who ate all the Pi's? Who ate all the Pi's?
Boris Johnson, Boris Johnson,
Who ate all the Pi's?

AndersM
Posts: 33
Joined: Sun Sep 06, 2015 1:18 pm

Re: Animating The Sense Hat

Sun Oct 11, 2015 9:45 am

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

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Animating The Sense Hat

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
--
Gordons projects: https://projects.drogon.net/

Woll
Posts: 472
Joined: Mon Jul 06, 2015 2:14 am
Location: Cloud Cuckoo Land

Re: Animating The Sense Hat

Sun Oct 11, 2015 5:57 pm

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.
Who ate all the Pi's? Who ate all the Pi's?
Boris Johnson, Boris Johnson,
Who ate all the Pi's?

Woll
Posts: 472
Joined: Mon Jul 06, 2015 2:14 am
Location: Cloud Cuckoo Land

Re: Animating The Sense Hat

Sun Oct 11, 2015 7:47 pm

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
Who ate all the Pi's? Who ate all the Pi's?
Boris Johnson, Boris Johnson,
Who ate all the Pi's?

billintad
Posts: 42
Joined: Tue Jul 28, 2015 10:21 pm

Re: Animating The Sense Hat

Sun Oct 11, 2015 11:37 pm

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.

AndersM
Posts: 33
Joined: Sun Sep 06, 2015 1:18 pm

Re: Animating The Sense Hat

Mon Oct 12, 2015 8:04 am

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

Woll
Posts: 472
Joined: Mon Jul 06, 2015 2:14 am
Location: Cloud Cuckoo Land

Re: Animating The Sense Hat

Tue Oct 13, 2015 12:59 am

Thanks for the replies. Yep I forgot to close the indent off. Thanks for the tip on the other loop as well.
Who ate all the Pi's? Who ate all the Pi's?
Boris Johnson, Boris Johnson,
Who ate all the Pi's?

Return to “Astro Pi”