Ecstatic newbie's first working prototype

Many thanks to those who helped me onto the first rung…@dave1707,@Ignatz,@Thwapp, @Stevon8tor , @Jordan and others whose comments have been invaluable…
I have managed to write this one from scratch without having to ask for help…PROGRESS!!!(?) [though those who have assisted in the past will no doubt recognise their presense in the code]

Substitute 2 of your own sprites in



--# GetSpritesFunction
function getsprites()
    tile1=readImage("Documents:cobblestone")
    tile2=readImage("Documents:shader")
    end
    
--# Main
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
Border=30
dx,dy=0,0
makemesh()
getsprites()
TileSelected=tile1
end

function draw()
background(40, 40, 50)  
fill(255, 255, 255, 255)
-- this section deals with available tiles
textMode(CORNER)
text("Select a tile...and double tap to place",600,730)
if TileSelected == tile1 then
  rect(595,595,100,100)
else
  rect(705,595,100,100)
end
spriteMode(CORNER)
sprite(tile1,600,600,90,90)
sprite(tile2,710,600,90,90)

-- this section deals with the l.h.s. selection window
clip(Border,Border,480,720)
        for x=1,m do
        for y=1,n do
              prebasetab1[x][y]:draw()
        end
    end
clip()

-- this section deals with the lower r.h.s. overview
    translate(600,Border)
    scale(scl,scl)
    for x=1,m do
        for y=1,n do
              prebasetab2[x][y]:draw()
        end
    end
    rectMode(CORNER)
    fill(108, 81, 81, 73)
    rect(0-dx,0-dy,480,720)

end

--# MeshFunction
function makemesh()
m,n,sl,scl = 30,30,60,0.2
    prebasetab1={}
    for x=1,m do
        prebasetab1[x]={}
            for y=1,n do
                cell=mesh()
                prebasetab1[x][y]=0
                cell:addRect(30+(x*sl)-sl/2,30+(y*sl)-sl/2,sl,sl)
                cell:setRectColor(1,x*y*255/(m*n),255-(x*y*255/(m*n)),x*y*255/(m*n))
                prebasetab1[x][y]=cell
            end
    end
    prebasetab2={}   
    for x=1,m do
        prebasetab2[x]={}
            for y=1,n do
                cell=mesh()
                prebasetab2[x][y]=0
                cell:addRect((x*sl)-sl/2,(y*sl)-sl/2,sl,sl)
                cell:setRectColor(1,x*y*255/(m*n),255-(x*y*255/(m*n)),x*y*255/(m*n))
                prebasetab2[x][y]=cell
            end
    end
end    
 
--# MoveMeshFunction
function movemesh(dx,dy)
     
for x=1,m do
    for y=1,n do  
prebasetab1[x][y]:setRect(1,30+((x*sl)-sl/2)+dx,30+((y*sl)-sl/2)+dy,sl,sl)
end
end
end

--# TouchFunction
function touched(t)
-- this deals with the r.h.s. selection window swipe action
    if t.x>=Border and t.x<=510 then
        if t.y>=Border and t.y<=750 then
            if t.state==MOVING then
                dx=dx+(t.deltaX*2)
                dy=dy+(t.deltaY*2)
                if dx>=0 then
                dx=0
                end
            if dx<=-(m*sl)+480 then
                dx=-(m*sl)+480 
                end
            if dy>=0 then
                dy=0
                end
            if dy<=-(n*sl)+720 then
                dy=-n*sl+720
                end
            movemesh(dx,dy)    
            end
            -- this deals with the r.h.s tile placement double tap
             if t.state==BEGAN and t.tapCount==2 then
             xsel=math.floor((30+t.x-dx)/sl)
             ysel=math.floor((30+t.y-dy)/sl)
             prebasetab1[xsel][ysel]:setColors(255,255,255,255) 
             prebasetab1[xsel][ysel].texture = TileSelected
             prebasetab2[xsel][ysel]:setColors(255,255,255,255)
             prebasetab2[xsel][ysel].texture = TileSelected
             end 
           
    end
end

-- this deals with the tile selection
if t.x>=600 and t.x<=690 then
        if t.y>=600 and t.y<=690 then
            TileSelected=tile1
            end
            end
if t.x>=710 and t.x<=800 then
        if t.y>=600 and t.y<=690 then
            TileSelected=tile2
            end
            end     
end

Next steps

additional scrollable images for more choice,
choice of background colour mesh via mixer so it can be converted into a sprite drawing program,
file saving…

Haven’t felt this happy in ages!

Comments regarding elegance and alternate ways of coding are always welcomed and much appreciated…

@TheAbstractMan - well done!

I suggest you indent your code properly, ie add an indent when you start a new function, if…then , for…next, etc - and do it carefully and consistently. It makes debugging way, way easier.

Imagine if the function below were longer, how hard it would be to figure out whether an error was because you had too many or too few “end” statements!

function movemesh(dx,dy)

for x=1,m do
    for y=1,n do  
prebasetab1[x][y]:setRect(1,30+((x*sl)-sl/2)+dx,30+((y*sl)-sl/2)+dy,sl,sl)
end
end
end

It should look like this

function movemesh(dx,dy)
    for x=1,m do
        for y=1,n do  
            prebasetab1[x][y]:setRect(1,30+((x*sl)-sl/2)+dx,
                                         30+((y*sl)-sl/2)+dy,
                                                       sl,sl)
        end
    end
end

@Ignatz. Cheers, shall do.

@TheAbstractMan - I was trying to break up the long line in that function so the two repetitive parts were under each other, making it easy to see if there is a mistake, and what is different between them (but the forum editor refuses to line them up properly!). Anyway, breaking lines up is optional, but indenting is really important.

@Ignatz. Yeah, I get you. I can see that splitting would be really useful for lines like that - it does make it a lot easier to read for the old grey matter. My indentation should get better and more consistent when my keyboard arrives…hopefully tomorrow… In the mean time, still trying to get my spade hands to work to a finer precision than they are capable of - that’s my excuse anyway!

That’s a nice start!

I can see that we are learning similar things at the same time. I’ve been getting to grips with meshes, moving their rectangles by their stored index values, etc. I’ll study your code to see if you have done anything in a different way to me.

You don’t have to use setRect to move the tiles around you can use translate also. I like setRect because it is easier for me to keep track of. In addition you could make a list of created tiles (for player, objects , and what not) to avoid looping through all the m,n values if they are empty. I know they are not empty now but latter. The cells could hold the x,y coordinates. This will come in to play when you are adding things not part of background tiles. Looks like you are making good progress.