Trebor37
Posts: 42
Joined: Thu Nov 19, 2015 1:39 am

SenseHat Problems

Sat Jul 02, 2016 10:21 pm

I am a retiree who will be working with some 8th grade kids with Raspberry Pi's. I am a Mac user and my only programming experience is with BASIC and Fortran. Right now I am trying to learn Python and get the project "Sense Hat Marble Maze" on the raspberry.org/learing site to work, and there are somethings I don't understand.

1. What does the instruction 'sense = SenseHat()' do? My guess is that 'sense' is a variable and 'SenseHat()' is a function. What is it that the function is storing in 'sense'?

2. This is the code for a subroutine in that project .

def move_marble(pitch,roll,x,y):
new_x = x
new_y = y
if 1 < pitch < 179:
new_x -= 1
elif 359 > pitch > 181:
new_x += 1
return new_x, new_y

The first line passes values for 'pitch' and 'roll' that are read from the Sense Hat and values for the variables x and y. However I am puzzled by the 'return new_x, new_y'. How does the program know that those are new values for the x and y variables that were passed in. Does that statement just put the new values in x and y because they were the only variables passed in?

3. Another subroutine is:

def check_wall(x,y,new_x,new_y):
if maze[new_y][new_x] != r:
return new_x, new_y
elif maze[new_y][x] != r:
return x, new_y
elif maze[y][new_x] != r:
return new_x, y
return x,y

The instruction 'if maze[new_y][new_x] != r:' seems strange to me in that the y-coordinate is listed before the x-coordinate. All of the other instructions list the x-coordingate first. At first I thought that was a typo.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: SenseHat Problems

Sat Jul 02, 2016 10:34 pm

1. Calling SenseHat() creates an instance of the SenseHat class which is then stored to the variable sense. The whole instance of the class is stored in the variable and this includes the local variables and function that exist in SenseHat.
This is explained on the first sense hat tutorial https://www.raspberrypi.org/learning/ge ... worksheet/

2. Python doesn't know that it is a new value of x or y. The two items returned could have been called anything! The function takes the x and y values passed in stores them in new_x and new_y, modifies them and returns the values.

3. I had a quick look at the code and when it refers to the maze it is always y coordinate first. If it was x coordinate first then the maze would be rotated by 90°
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: SenseHat Problems

Sat Jul 02, 2016 10:50 pm

If you're going to post python code please wrap it in [code]...[/code] tags.

So that
def move_marble(pitch,roll,x,y):
new_x = x
new_y = y
if 1 < pitch < 179:
new_x -= 1
elif 359 > pitch > 181:
new_x += 1
return new_x, new_y

Marked up as [code]def move_marble(pitch,roll,x,y):
new_x = x
new_y = y
if 1 < pitch < 179:
new_x -= 1
elif 359 > pitch > 181:
new_x += 1
return new_x, new_y[/code]

Appears as

Code: Select all

def move_marble(pitch,roll,x,y):
  new_x = x
  new_y = y
  if 1 < pitch < 179:
    new_x -= 1
  elif 359 > pitch > 181:
    new_x += 1
  return new_x, new_y
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Trebor37
Posts: 42
Joined: Thu Nov 19, 2015 1:39 am

Re: SenseHat Problems

Sun Jul 03, 2016 7:27 pm

Thanks Dougie for the tip about posting code and the help reference.

MarcScott
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 82
Joined: Sat Aug 08, 2015 11:30 am

Re: SenseHat Problems

Wed Jul 06, 2016 7:41 am

Hi, author here. It's a while since I wrote this resource but I can have a go at explaining.
Trebor37 wrote:The first line passes values for 'pitch' and 'roll' that are read from the Sense Hat and values for the variables x and y. However I am puzzled by the 'return new_x, new_y'. How does the program know that those are new values for the x and y variables that were passed in. Does that statement just put the new values in x and y because they were the only variables passed in?
The x and y position of the marble are passed into the function. Then two new variables are created that are equal to x and y, called new_x and new_y. Later on in the resource this function is modified further so that depending on the pitch and roll of the SenseHAT, these variables are increased or decreased. They now represent the new position of the marble. The old x and y coordinates are needed to relocate the marble back to it's original position if a collision with a wall is detected, which is why the original x and y coordinates are not directly incremented themselves.
Trebor37 wrote:The instruction 'if maze[new_y][new_x] != r:' seems strange to me in that the y-coordinate is listed before the x-coordinate. All of the other instructions list the x-coordingate first. At first I thought that was a typo.
Here the y coordinate precedes the x coordinate because of the way the 2 dimensional list is read. Imagine a simple representation of a noughts and crosses board after a game has been played.

Code: Select all

[[X,O,X],
[O,X,O],
[O,X,X]]
x represents the horizontal positions and y represents the vertical positions with the top left square being at x=0 and y=0. If I wanted to look at the top right square I would write something like:

Code: Select all

board[0][2]
because I'm looking at the 0th element of the 2D list and then the 2nd element of that list. If I was to ask you to provide the x,y coordinates of the top right square you would right 2,0 though if we're indexing from 0.

Hope this helps.

Trebor37
Posts: 42
Joined: Thu Nov 19, 2015 1:39 am

Re: SenseHat Problems

Wed Jul 06, 2016 1:40 pm

Thanks! Your explanation not only answered my question about the maze[y][x] code it also gave me insight as to why the Sense Hat numbers down for the y-coord. As a retired math teacher my orientation would have been to put the (0,0) coordinate in the lower left instead of the upper left. I will save your explanation and share it with my 8th grade kids when school starts again.

MarcScott
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 82
Joined: Sat Aug 08, 2015 11:30 am

Re: SenseHat Problems

Wed Jul 06, 2016 2:29 pm

When I first got into computing the whole 0,0 being in the top left was very confusing, as I have a Science background.

Most games still work this way, as the original games were all made for the terminal and worked on line and column numbers. The first character to be rendered would therefore always be in the top left.

Return to “Astro Pi”