# Galaxian clone example (BlitEngine/WiFiBoyMini)
# 2018.09.06, tklua@wifiboy.org

wb.blitpal()
star=[]; starspeed=[]
for i in range(0, 20):
    star.append(wb.rand() % 16384)
    starspeed.append((wb.rand() % 4)+1)
x = 50; bt = btx = ct = dx = sc = 0; dir = 2; spn=[]
for i in range(0,18): spn.append(1)
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
    if wb.getkey() & 4 == 4:
        x += 2
        if x > 112: x = 112
    if wb.getkey() & 8 == 8:
        x -= 2
        if x < 0: x = 0
    if wb.getkey() & 1 == 1:
        if bt==0: bt = 112; btx = x+6
    if wb.getkey() & 2 == 2:
        for i in range(0,18): spn[i]=1
    if bt > 0: wb.blitimg(1, btx, bt); bt -= 2
    ct += 1
    if (ct %4)==0:
        dx += dir
        if dx>32: dir=-2; dx=32
        if dx<1: dir=2; dx=1   
    for j in range(32,97,48): wb.blitimg(176, dx+j-16, 12, 16)          
    for i in range(0,18):
        sx = dx+(i%6)*16; sy = (i//6)*16+30
        if (spn[i]==1):
            if ((ct+i)%40<20): wb.blitimg(144, sx, sy, 16)
            else: wb.blitimg(146, sx, sy, 16)
            if (bt-sy>0) and (bt-sy<16) and (btx-sx>0) and (btx-sx<13):
                spn[i]=20; bt = 0; sc+=100
        elif (spn[i]>7): wb.blitimg(121, sx, sy, 16); spn[i]-=1
        elif (spn[i]>2): wb.blitimg(119, sx, sy, 16); spn[i]-=1
    wb.blitimg(115, x, 112, 16) 
    wb.blitstr("SCORE %5s"%(str(sc)), 20, 0)
    wb.blit()
    