# WiFiBoy Gyro example (BlitEngine/WiFiBoyMini)
# 2018.10.24, tklua@wifiboy.org

from machine import I2C, Pin
from mpu9250 import MPU9250
i2c = I2C(scl=Pin(22), sda=Pin(23))
sensor = MPU9250(i2c)

wb.blitpal()
star=[]; starspeed=[]
for i in range(0,20):star.append(wb.rand(16384));starspeed.append(wb.rand(4)+1)
x=y=50.0
btx=[0,0,0,0,0]; bty=[0,0,0,0,0]
sht=0
while True:
    wb.blitbuf()
    for j in range(0,20):
        wb.blitbuf(star[j],255)
        star[j] = star[j]+128*starspeed[j]
        if (star[j]>16384): star[j] = star[j] - 16384
    x+=sensor.acceleration[0]
    if x<0: x=0
    if x>112: x=112
    y+=sensor.acceleration[1]
    if y<0: y=0 
    if y>112: y=112
    if wb.getkey()&1==1 and sht==0:
        for i in range(len(bty)): 
            if bty[i]<=0: bty[i]=y; btx[i]=x+6; sht=1; break;
    if wb.getkey()&1==0 and sht==1: sht=0
    for i in range(len(bty)):
        if bty[i]>0: wb.blitimg(1, int(btx[i]), int(bty[i])); bty[i] -= 2        
    wb.blitimg(115, int(x), int(y), 16) 
    wb.blit()
    