-
Notifications
You must be signed in to change notification settings - Fork 36
Sense HAT(4)
This page is a tutorial to playing Marble Maze game on Sense HAT. BlockPi v1.1.1+ is required.
Video demo:
https://video.zhihu.com/video/1226591380837199872
First of all, initialize the Sense HAT and enable gyroscope and accelerometer. Design some maze maps and save them in a list, red pixels are walls and green pixel is the exit.
Build an initialize function, choose a maze by random and initialize the position variables of the marble. x, y are current coordinates and new_x, new_y are next position's coordinates.
Continue coding the main program: call initialize function and main loop. The marble's position in the maze list is pixel number 8y+x+1. When moving the marble, turn current pixel to black, then call move_marble function and turn new pixel to white. And refresh the maze. If the marble moves to green, end the game and show winning message, then wait for joystick action to restart.
The most important part is the move_marble function. Sense HAT gets orientation data in aircraft principle axes of roll, pitch and yaw, and returns a dictionary.
Choose the direction which the marble goes in according to pitch and roll angles:
Detect red wall. Move the marble only when the new position is not red.
This is the whole move_marble function:
All the program is done, save file is here(right-click and save-as).