Junkpilepunk
Posts: 6
Joined: Tue Aug 16, 2016 3:37 pm

Python and the senseHAT Joystick

Thu Oct 12, 2017 6:42 pm

Hello,
i hope this is the right forum for my problem.

My problem is that i wrote a little "game" to get used to the SenseHat but the joystick and its functions aren't working as i thought they would do.
The Problem seems to be in this little function:

Code: Select all

def players_turn(array):
    win = True
    step = 1

    while win == True and step <= level:

        direction = 0
        step_taken = False
        while step_taken == False:
            for event in sense.stick.get_events():

                if event.direction == "up":
                    direction = 1
                    step_taken = True
                    sense.load_image("up.png", redraw = True)
                    sleep(1)
                    sense.clear()

                elif event.direction == "down":
                    direction = 2
                    step_taken = True
                    sense.load_image("down.png", redraw = True)
                    sleep(1)
                    sense.clear()

                elif event.direction == "left":
                    direction = 3
                    step_taken = True
                    sense.load_image("left.png", redraw = True)
                    sleep (1)
                    sense.clear()

                elif event.direction == "right":
                    direction = 4
                    step_taken = True
                    sense.load_image("right.png", redraw = True)
                    sleep(1)
                    sense.clear()

        if direction == array[step-1]:
            step = step + 1
        else:
            win = False

    return win
The function gets an array of "directions" which the player has to press in the right order. if he does it, the function returns win = 1.
the function itself seems to work until it gets called the second time (in the second level). Then it seems to "save" the pressed direction from the first call and the player is not able to input something before the function stops.
my guess is that the get_events() function has some stored events that it gives out instead of getting new ones.
my guess of solution would be to clear that store events out of some kind of puffer where they got saved, but i found no way to do that.

I am not a pro at python so i would appreciate any suggestions to solve the problem and i would appreciate even more if you could explain what you do :D

thanks in advance

Junkpilepunk

bensimmo
Posts: 1923
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Python and the senseHAT Joystick

Thu Oct 12, 2017 7:53 pm

Have you cleared the variable direction (without all the code I don't know).
EDIT I see it, cleared at the start.

Use Thonny and step through to debug and see what is set to what.

User avatar
Davespice
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 1634
Joined: Fri Oct 14, 2011 8:06 pm
Location: London, United Kingdom
Contact: Website Twitter

Re: Python and the senseHAT Joystick

Fri Oct 13, 2017 8:53 am

I think I see what's happening. When the joystick is pressed you get two events, one for pressed and another for released. If you're programming a game you might want the player to say keep firing while a button is held down. You wouldn't be able to code that if the event system only told you about button presses alone. You would need to keep firing until you get the button released event.

So what's happening is your code is processing the pressed AND released events as though they're the same, which is making it look like it's saving the previous event.

So I think you need to add an extra IF statement inside your for loop to test the event action, like this:

Code: Select all

for event in sense.stick.get_events():
    if event.action == "pressed":
        [rest of your code goes here]
Good luck

Junkpilepunk
Posts: 6
Joined: Tue Aug 16, 2016 3:37 pm

Re: Python and the senseHAT Joystick

Mon Oct 16, 2017 4:43 pm

Hey,
thank you for your fast Help.

I'll try the solution suggested by you..

Junkpilepunk

Return to “Astro Pi”

Who is online

Users browsing this forum: No registered users and 1 guest