I need some help on how to make some art code....

im a new coder and i need some help…
I’m trying to make some art code that has shapes that can be dragged and dropped so i can make mandalas.
with a bar on the left with circles, squares, hexagons…etc

this is what i have so far…


function setup()
    pos = vec2(200,200)
    size = vec2(202,342)
end

function draw()
    background(173, 181, 216, 255)
    spriteMode(CORNER)
    sprite("Documents:square",pos.x,pos.y,size.x,size.x)

end

function touched(t)
    tpos = vec2(t.x,t.y)
    if tpos.x>=pos.x and tpos.x<=pos.x+size.x and tpos.y>=pos.y and tpos.y<=pos.y+size.y then
        if t.state == BEGAN then
            anchor = tpos - pos
        end
    end

    if anchor then pos = tpos - anchor end
    if t.state == ENDED then anchor = nil end
end

@roro When you post code on the forum, put 3 ~'s on a line before and after your code so it displays properly. I added them to your code above. Also, any sprites that you use from your Documents folder aren’t useable by other forum members, so if we try to run your code, we don’t see what you see which makes it hard to give you help.

ok thanks! I’m really new at this so I don’t know that much
The sprites don’t really matter. Only the shapes matter.

@roro Are you saying you want different shapes on the left of the screen so you can touch one of them and drag and drop it somewhere on the screen. The original shapes remain on the left so you can continue to drag and drop as many as you want.

yes exactly

@roro That should be easy to do. Just create the different shapes along the left side. When you touch one of them, create that shape and put its values into a table. As you drag it around, change its x,y values in the table. By using a table, you can have as many shapes on the screen as you want.

ok thanks so much!

@roro Here’s a simple example of what I think you’re after. Select an object from the left side and drag it where you want it. You can select any object and move it where you want. If this is what you’re after, you can expand on this to add more objects and colors.


displayMode(FULLSCREEN)

function setup()
    rectMode(CENTER)
    obj={}
end    
    
function draw()
    background(0)
    fill(255)
    -- draw objects along left side
    rect(50,100,50,50)
    ellipse(50,200,50,50)
    ellipse(50,300,100,50)
    -- draw objects from table
    for a,b in pairs(obj) do
        if b.z==1 then
            rect(b.x,b.y,50,50)
        end
        if b.z==2 then
            ellipse(b.x,b.y,50,50)
        end
        if b.z==3 then
            ellipse(b.x,b.y,100,50)
        end
    end
end  

function touched(t)
    -- determine which object is selected
    if t.state==BEGAN then
        if t.x>25 and t.x<75 and t.y>75 and t.y<125 then
            table.insert(obj,vec3(t.x,t.y,1))
            sel=#obj
        end
        if t.x>25 and t.x<75 and t.y>175 and t.y<225 then
            table.insert(obj,vec3(t.x,t.y,2))
            sel=#obj
        end
        if t.x>0 and t.x<100 and t.y>275 and t.y<325 then
            table.insert(obj,vec3(t.x,t.y,3))
            sel=#obj
        end
        for a,b in pairs(obj) do
            if t.x>b.x-25 and t.x<b.x+25 and t.y>b.y-25 and t.y<b.y+25 then
                sel=a
            end
        end
    end
    -- move selected object
    if t.state==MOVING then
        if sel>0 then
            obj[sel].x=t.x
            obj[sel].y=t.y
        end       
    end
    -- set selected to 0
    if t.state==ENDED then
        sel=0
    end
end

oh wow thanks!!! i really needed the help!!!

@roro - it might be prettier if you used pictures instead of shapes.

In this case, load them into a table, and then when the user selects them, draw the selected item. Note you only need to store each picture once, no matter how many times you sprite it.

--in setup
pix = {readImage("Dropbox:pic1"), readImage("Dropbox:pic2), etc etc}
--table for user selections
selected={}

--in touched, put user selections into a table
--suppose pic 3 has been touched and put in square (2,5)
--store little table of selection and location
table.insert(selected, {3,2,5}) 

--in draw, just draw pix down the left side
for i=1, #pix do
    sprite(pix[i], 50, HEIGHT-50*i, 40)
end
--then draw selections
for i=1, # selected do
    sprite(pix[selected[i][1]), ....--wherever you want to put them
end