starter game 20

Here’s another spinoff from something else I was doing. I figure someone can add more things to it. A low score can be saved in local data and any bugs that show up can be fixed. The object of this is to slide a Sprite right/left/up/down to a matching Sprite to eliminate as many sprites as you can. A Sprite can be moved 1 square at a time. Any sprites above an empty square will slide down. Each time a match is made, 1 is subtracted from the score. The best I got was 46. A perfect score would be 5 since there are 5 different sprites.

EDIT: Other things that can be added is code to check if there are any valid moves left, if not the show End of Game. Also, a way to play a new game instead of just pressing the replay icon.

supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)

function setup()
    score=13*19
    spr={"Planet Cute:Character Pink Girl",
         "Planet Cute:Character Horn Girl",
         "Planet Cute:Character Cat Girl",
         "Planet Cute:Character Princess Girl",
         "Planet Cute:Character Boy"}
    selected=0
    tab={}
    for x=1,13 do       
        tab[x]={}
        for y=1,19 do
            tab[x][y]=math.random(5)
        end
    end
    size=50
end

function draw()
    background(0)
    stroke(255)
    strokeWidth(2)
    fill(128)
    for x=1,13 do
        for y=1,19 do
            rect(x*size,y*size,size,size)
            if tab[x][y]>0 then
                sprite(spr[tab[x][y]],x*size+25,y*size+35,50)
            end
        end
    end
    fill(255)
    text("Score  "..score,WIDTH/2,HEIGHT-10)
end

function touched(t)
    x=math.floor(t.x/size)
    y=math.floor(t.y/size)
    if x<1 or x>13 or y<1 or y>19 then
        fillGap()
        return
    end
    if t.state==BEGAN then
        tx,ty=x,y
        if tab[x][y]>0 then
            selected=tab[x][y]
            selX=x
            selY=y
        end
    end
    if t.state==MOVING and selected>0 then
        if x~=selX or y~=selY then
            if selected==tab[x][y] then
                tx,ty=t.x,t.y
                x=math.floor(tx/size)
                y=math.floor(ty/size)
                tab[x][y]=selected
                selected=0
                tab[selX][selY]=0
                score=score-1
            else
                selected=0
            end 
        end          
    end
    if t.state==ENDED then
        fillGap()
    end
end

function fillGap()
    for x=1,13 do
        for y=1,19 do
            if tab[x][y]==0 then
                for z=1,19-y do
                    if tab[x][y+z]~=0 then
                        tab[x][y]=tab[x][y+z]
                        tab[x][y+z]=0
                        break
                    end                   
                end
            end
        end
    end
end