[PYTHON] Games using IMU with SenseHat

For this sense hat game, I tried IMU (Inertial Measurement Unit).

If you tilt the body, the red pixels will move. This clears when you take all the green pixels. The blue pixel in the rightmost column is the time limit.

IMG_5938.jpg

However, it wasn't the IMU that I struggled with this time, but the Python thread that I used for the first time at the same time. The IMU was pretty easy, just like the joystick.

I tried running a thread that counts the time limit in parallel with the main loop, but I didn't know how to control each other (stop the thread if the game is cleared and stop the main loop when the time is over). I learned about the control by threading.Event () and was able to solve it.


from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
from time import sleep
from random import randint
from copy import copy
import threading

sense = SenseHat()
sense.clear()

red = (255, 0, 0)
blue = (0, 0, 255)
yellow=(255,255,0)
purple=(128,0,128)
green=(0,255,0)
indigg=(75,0,130)
orange=(255,128,0)
black=[0,0,0]

sense.set_rotation(0)

ncol = 8
nrow = 8
col_max = ncol - 2
row_max = nrow - 1

event = threading.Event()

def setdisturbs(num_disturbs, initpos):
    disturbs = list()
    for i in range(0,num_disturbs):
        while True:
            candi = (randint(0, col_max), randint(0, row_max))
            if candi == initpos:
                continue

            duplicated = False
            for j in disturbs:
                if candi == j:
                    duplicated = True
                    break
            if duplicated:
                continue
            disturbs.append(candi)
            break

    color_disturb = green
    for i in disturbs:
        sense.set_pixel(i[0], i[1], color_disturb)

def timecount(interval):
    col = ncol - 1
    for row in range(row_max):
        sense.set_pixel(col, row, blue)
        if event.wait(interval):
            break
    event.set()

speed = 0.2
num_disturbs = 10
interval = 3

ballpos = (ncol/2, nrow/2)
sense.set_pixel(ballpos[0], ballpos[1], red)
ballvec = (0, 0)

setdisturbs(num_disturbs, ballpos)

t1 = threading.Thread(target=timecount, args=(interval,))
t1.start()

success = False
while True:
    if event.is_set():
        break
    
    acceleration = sense.get_accelerometer_raw()
    x = acceleration['x']
    y = acceleration['y']
    z = acceleration['z']

    x=int(round(x, 0))
    y=int(round(y, 0))
    z=int(round(z, 0))
    
    #print("x={0}, y={1}, z={2}".format(x, y, z))

    ballvec = (x, y)
    # if x  == -1: #left
    #   ballvec = (-1, 0)
    # elif y == -1: #top
    #   ballvec = (0, -1)
    # elif x == 1: #right
    #   ballvec = (1, 0)
    # elif y == 1: #bottom
    #   ballvec = (0, 1)
    # else:
    #   ballvec = (0, 0)

    sense.set_pixel(ballpos[0], ballpos[1], black)
    ballpos = (max(min(ballpos[0] + ballvec[0], col_max), 0), max(min(ballpos[1] + ballvec[1], row_max), 0))
    sense.set_pixel(ballpos[0], ballpos[1], red)

    #print("col={0}, row={1}".format(ballpos[0], ballpos[1]))
    num_colorpx = 0
    cur_px = sense.get_pixels()
    for px in cur_px:
        if px == [0,252,0]: # green
            num_colorpx = num_colorpx + 1
    if num_colorpx == 0:
        event.set()
        success = True
        break

    sleep(speed)

if success:
    sense.show_letter("O")
else:
    sense.show_letter("X")

sleep(2)
sense.clear()

Recommended Posts

Games using IMU with SenseHat
Create games with Pygame
Using X11 with ubuntu18.04 (C)
When using optparse with iPython
Try using PythonTex with Texpad.
[S3] CRUD with S3 using Python [Python]
Messaging with AMQP using kombu
Try using matplotlib with PyCharm
[Python] Using OpenCV with Python (Basic)
Grouping games with combinatorial optimization
Programming education game with SenseHAT
Using a printer with Debian 10
Try using folium with anaconda
Using OpenCV with Python @Mac
Send using Python with Gmail
Complement python with emacs using company-jedi
[Python] Using OpenCV with Python (Image Filtering)
Using Rstan from Python with PypeR
ROS Lecture 108 Using Database (mongo) with ROS
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Using Sessions and Reflections with SQLAlchemy
Using Lambda with AWS Amplify with Go
Using Japanese with Rodeo's IPython @ Windows
Using cgo with the go command
Notes on using rstrip with python.
Create API using hug with mod_wsgi
Using Chainer with CentOS7 [Environment construction]
Easy with Slack using Bot #NowPlaying
Try using Python's networkx with AtCoder
Using a webcam with Raspberry Pi
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Using Kali Linux Gui with WSL2