Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Math.random + table help

edited April 13 in Questions Posts: 124

I’m trying to set this code up so it displays 1 random image from my table, which holds 8 images ,on screen 1 at a time.
I’m halfway there. I got the images to display randomly but they are stacked on each other. Also I’m trying to set it up so if one image from the table gets displayed it won’t show up twice.

displayMode(FULLSCREEN)
function setup()
      et=ElapsedTime
      count=30     
    tab={   asset.builtin.Planet_Cute.Character_Princess_Girl, 
            asset.builtin.Planet_Cute.Character_Boy,    
            asset.builtin.Planet_Cute.Character_Horn_Girl,              
            asset.builtin.Planet_Cute.Character_Pink_Girl,
            asset.builtin.Space_Art.UFO,      
            asset.builtin.Planet_Cute.Enemy_Bug,     
            asset.builtin.Planet_Cute.Key,       
            asset.builtin.Planet_Cute.Gem_Green, }   
    bTab={}
    for x=-20,20 do
        for y=-20,20 do
            table.insert(bTab,{x=x,y=y,v=math.random(13)})
        end       
    end
    dx,dy=WIDTH/2,HEIGHT/2
end

function draw()
    background(0)
    if ElapsedTime-et>1 then
        count = count - 1
        et=ElapsedTime
    end   
    text(count,WIDTH/2,HEIGHT/2)  

    if count<=29  then
        for a,b in pairs(bTab) do
            if b.v<7 then
            sprite(tab[b.v],dx+200,dy)        
        else
                if count<=28 then
            sprite(tab[b.v-5],dx,dy+200)    
        end
                if count<=27 then
            sprite(tab[b.v],dx,dy-200)    
        end  
    end
    end 
    end
end
«1

Comments

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc You have to be a little more specific with what you want. This shows a random sprite from the table with no duplicates. When all eight are shown, the word done is displayed. Not sure what this will accomplish.

    displayMode(FULLSCREEN)
    function setup()
        et=ElapsedTime
        count=6  
        a=0   
        tab={   asset.builtin.Planet_Cute.Character_Princess_Girl, 
                asset.builtin.Planet_Cute.Character_Boy,    
                asset.builtin.Planet_Cute.Character_Horn_Girl,              
                asset.builtin.Planet_Cute.Character_Pink_Girl,
                asset.builtin.Space_Art.UFO,      
                asset.builtin.Planet_Cute.Enemy_Bug,     
                asset.builtin.Planet_Cute.Key,       
                asset.builtin.Planet_Cute.Gem_Green, }      
    end
    
    function draw()
        background(0)
        if ElapsedTime-et>1 then
            count = count + 1
            et=ElapsedTime
        end
        if count>4 and #tab>0 then
            count=0
            if a>0 then
                table.remove(tab,a)
            end
            if #tab>0 then
                a=math.random(#tab)
            end
        end
        if #tab>0 then
            text(count,WIDTH/2,HEIGHT/2+100)
            sprite(tab[a],WIDTH/2,HEIGHT/2)
        else
            text("done",WIDTH/2,HEIGHT/2)
        end
    end
    
  • Posts: 124

    @dave1707 I want to display them all on the screen, in different positions. Basically I’m going for a shuffling cards effect then the screen deals them 1 by 1. What’s in the table are the cards, math.random is the shuffle. Going to use Elapsed time to deal. In a card deck you don’t get the same card dealt twice, so that’s why I don’t want duplicates.

  • dave1707dave1707 Mod
    edited April 13 Posts: 8,398

    Here’s a card shuffle program. It just prints the cards. You can add the card images if you want. Try doing a forum search for things. Probably everything you’re looking for has been done and there’s probably an example I’ve written for it.

    function setup()
        suit={"Diamonds","Hearts","Spades","Clubs"}
        value={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"}
    
        Shuffled=shuffleCards()
    
        for z=1,52 do    -- print the shuffled deck
            s=math.ceil(Shuffled[z]/13)   -- get suit 
            v=Shuffled[z]%13+1    -- get value
            print(value[v].." of "..suit[s])
        end
    end
    
    function shuffleCards()
        d={}
        for z=1,52 do
            d[z]=z
        end  
        for z=52,2,-1 do
            r=math.random(z)
            h=d[z]
            d[z]=d[r]
            d[r]=h
        end
        return d    -- return shuffled deck
    end
    
  • Posts: 124

    @dave1707 also want them to stay on screen after appearing.

  • edited April 13 Posts: 124

    @dave1707 My bad I didn’t see that you responded. I’ll check the code out, Thanks.

  • dave1707dave1707 Mod
    Posts: 8,398

    Here’s another version.

    displayMode(FULLSCREEN)
    
    function setup()
        textMode(CORNER)
        fontSize(30)
        fill(255)
        su={"♦","♥","♠","♣"}
        value={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
        Shuffled=shuffleCards()
    end
    
    function draw()
        background(227, 183, 114)
        c=0
        for x=1,4 do
            for y=1,13 do
                c=c+1
                s=math.ceil(Shuffled[c]/13)   -- get suit 
                v=Shuffled[c]%13+1    -- get value
                text(value[v].." of  "..su[s],x*200-120,HEIGHT-80-y*50)
            end
        end
    end
    
    function shuffleCards()
        d={}
        for z=1,52 do
            d[z]=z
        end  
        for z=52,2,-1 do
            r=math.random(z)
            h=d[z]
            d[z]=d[r]
            d[r]=h
        end
        return d    -- return shuffled deck
    end
    
  • Posts: 124

    @dave1707 I shouldn’t have brought up playing cards, bad example on my part. I feel off subject. I want the code to display exactly like this but I want the sprites to be random & no duplicates.

    Been trying to incorporate the shuffling program for hours but I feel like it’s tedious.

    displayMode(FULLSCREEN)
    function setup()
          et=ElapsedTime
          count=30     
        tab={   asset.builtin.Planet_Cute.Character_Princess_Girl, 
                asset.builtin.Planet_Cute.Character_Boy,    
                asset.builtin.Planet_Cute.Character_Horn_Girl,              
                asset.builtin.Planet_Cute.Character_Pink_Girl,
                asset.builtin.Space_Art.UFO,      
                asset.builtin.Planet_Cute.Enemy_Bug,     
                asset.builtin.Planet_Cute.Key,       
                asset.builtin.Planet_Cute.Gem_Green }   
        bTab={}
        for x=-20,20 do
            for y=-20,20 do
                table.insert(bTab,{x=x,y=y,v=math.random(13)})
            end       
        end
        dx,dy=WIDTH/2,HEIGHT/2
    end
    
    function draw()
        background(0)
        if ElapsedTime-et>1 then
            count = count - 1
            et=ElapsedTime
        end   
        text(count,WIDTH/2,HEIGHT/2)  
    
        if count<=29  then
            for a,b in pairs(bTab) do
                if b.v<7 then
                sprite(tab[2],dx+200,dy)        
            else
                    if count<=28 then
                sprite(tab[3],dx,dy+200)    
            end
                    if count<=27 then
                sprite(tab[1],dx,dy-200)   
    
                    if count<=26 then
                sprite(tab[4],dx-200,dy)   
        end  
        end
        end 
        end
        end
        end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    This displays random sprites in a circle. You can have as many sprites as you want. You will have to scale them if you have a lot so the don’t overlap.

    displayMode(FULLSCREEN)
    function setup()
          et=ElapsedTime
          count=1     
        tab={   asset.builtin.Planet_Cute.Character_Princess_Girl, 
                asset.builtin.Planet_Cute.Character_Boy,    
                asset.builtin.Planet_Cute.Character_Horn_Girl,              
                asset.builtin.Planet_Cute.Character_Pink_Girl,
                asset.builtin.Space_Art.UFO,      
                asset.builtin.Planet_Cute.Enemy_Bug,     
                asset.builtin.Planet_Cute.Key,       
                asset.builtin.Planet_Cute.Gem_Green,
                asset.builtin.Planet_Cute.Gem_Orange,
                asset.builtin.Planet_Cute.Chest_Closed,
                asset.builtin.Planet_Cute.Grass_Block,
                asset.builtin.Planet_Cute.Heart }  
        tab1={} 
        for z=1,#tab do
            a=math.random(#tab)
            table.insert(tab1,tab[a])
            table.remove(tab,a)
        end
        deg=360/#tab1
    end
    
    function draw()
        background(0)
        if ElapsedTime-et>2 then
            if count<#tab1 then
                count=count +1
                et=ElapsedTime
            end
        end   
        for z=0,count-1 do
            x=math.cos(math.rad(deg*z))*300
            y=math.sin(math.rad(deg*z))*300
            sprite(tab1[z+1],WIDTH/2+x,HEIGHT/2+y)
        end
    end
    
  • Posts: 124

    @dave1707 Perfect! Thanks!

  • Posts: 124

    @dave1707 how would I go about giving each sprite a specific x,y coordinate to display at?

    I think it’s here but not sure how to set up

     for z=0,count-1 do
            x=math.cos(math.rad(deg*z))*300
            y=math.sin(math.rad(deg*z))*300
            sprite(tab1[z+1],WIDTH/2+x,HEIGHT/2+y)
        end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    In the first table, change all the x=200,y=200 to what you want them to be.

    displayMode(FULLSCREEN)
    function setup()
        et=ElapsedTime
        count=1     
        tab={   {as=asset.builtin.Planet_Cute.Character_Princess_Girl,x=200,y=200},
                {as=asset.builtin.Planet_Cute.Character_Boy,x=200,y=200},    
                {as=asset.builtin.Planet_Cute.Character_Horn_Girl,x=200,y=200},              
                {as=asset.builtin.Planet_Cute.Character_Pink_Girl,x=200,y=200},
                {as=asset.builtin.Space_Art.UFO,x=200,y=200},      
                {as=asset.builtin.Planet_Cute.Enemy_Bug,x=200,y=200},     
                {as=asset.builtin.Planet_Cute.Key,x=200,y=200},       
                {as=asset.builtin.Planet_Cute.Gem_Green,x=200,y=200},
                {as=asset.builtin.Planet_Cute.Gem_Orange,x=200,y=200},
                {as=asset.builtin.Planet_Cute.Chest_Closed,x=200,y=200},
                {as=asset.builtin.Planet_Cute.Grass_Block,x=200,y=200},
                {as=asset.builtin.Planet_Cute.Heart,x=200,y=200} }     
        tab1={} 
        for z=1,#tab do
            a=math.random(#tab)
            table.insert(tab1,tab[a])
            table.remove(tab,a)
        end
    end
    
    function draw()
        background(0)
        if ElapsedTime-et>2 then
            if count<#tab1 then
                count=count +1
                et=ElapsedTime
            end
        end   
        for z=1,count do
            sprite(tab1[z].as,WIDTH/2+tab1[z].x,HEIGHT/2+tab1[z].y)
        end
    end
    
  • Posts: 124

    @dave1707 It’s not working. Right now the same sprites end up at the same locations every time. I want those specific locations and I want which sprite that shows in those specific locations to be random. To clarify I want a random sprite to appear in a random order in those specific locations every time I run the code.

    Example pic below, I get this every time I run code. Pretty much I want everything to be random but the locations.

    displayMode(FULLSCREEN)
    function setup()
        et=ElapsedTime
        count=1     
        tab={   {as=asset.builtin.Planet_Cute.Character_Princess_Girl,x=200,y=200},
                {as=asset.builtin.Planet_Cute.Character_Boy,x=-200,y=200},    
                {as=asset.builtin.Planet_Cute.Character_Horn_Girl,x=-300,y=200},              
                {as=asset.builtin.Planet_Cute.Character_Pink_Girl,x=-400,y=200},
                {as=asset.builtin.Space_Art.UFO,x=300,y=200},      
            }     
        tab1={} 
        for z=1,#tab do
            a=math.random(#tab)
            table.insert(tab1,tab[a])
            table.remove(tab,a)
        end
    end
    
    function draw()
        background(0)
        if ElapsedTime-et>2 then
            if count<#tab1 then
                count=count +1
                et=ElapsedTime
            end
        end   
        for z=1,count do
            sprite(tab1[z].as,WIDTH/2+tab1[z].x,HEIGHT/2+tab1[z].y)
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    Sorry, I was in a hurry this morning and didn’t check what I was doing.

    displayMode(FULLSCREEN)
    
    function setup()
        et=ElapsedTime
        count=1
        tab={   asset.builtin.Planet_Cute.Character_Princess_Girl,
                asset.builtin.Planet_Cute.Character_Boy,x=-200,
                asset.builtin.Planet_Cute.Character_Horn_Girl,
                asset.builtin.Planet_Cute.Character_Pink_Girl,
                asset.builtin.Space_Art.UFO }
        loc={vec2(200,200),vec2(-200,200),vec2(-300,200),vec2(-400,200),vec2(300,200)}
        tab1={}
        for z=1,#tab do
            a=math.random(#tab)
            table.insert(tab1,tab[a])
            table.remove(tab,a)
        end
    end
    
    function draw()
        background(0)
        if ElapsedTime-et>2 then
            if count<#tab1 then
                count=count+1
                et=ElapsedTime
            end
        end
        for z=1,count do
            sprite(tab1[z],WIDTH/2+loc[z].x,HEIGHT/2+loc[z].y)
        end
    end
    
  • Posts: 124

    @dave1707 Works great! Thanks I appreciate it.

  • Posts: 124

    @dave1707 I’m trying to combine the code above with this code, that you previously helped me with.

    The code above randomizes sprites then puts in specific locations.
    The code below makes the sprites moveable and scale from small circle to big circle.

    function setup()
        spriteMode(CENTER)
        ellipseMode(CENTER)
        displayMode(FULLSCREEN)
        tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
        tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
        tab3={vec3(800,100,.5),vec3(800,250,.5),vec3(800,400,.5),vec3(800,550,.5)}  -- position of sprite and scaler value
        tab4={
                asset.builtin.Planet_Cute.Character_Princess_Girl,
                asset.builtin.Planet_Cute.Character_Boy,
                asset.builtin.Planet_Cute.Character_Cat_Girl,
                asset.builtin.Planet_Cute.Character_Horn_Girl
            }
        imageSize = spriteSize(tab4[1])
        scaler = .50
    end
    function draw()
        background(98)
        strokeWidth(5)
        fill(128)
        for a,b in pairs(tab1) do
            ellipse(b.x, b.y, 100)
        end
        for a,b in pairs(tab2) do
            ellipse(b.x, b.y, 50)
        end
        for a,b in pairs(tab3) do
            sprite(tab4[a],b.x,b.y,imageSize*b.z)
        end
    end
    function touched(touch)    
        if touch.state==BEGAN then
            pos=0
            for a,b in pairs(tab3) do
                if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                            touch.y<b.y+40 then
                    b.z=1   -- scaler
                    pos=a
                    for c,d in pairs(tab1) do
                        if b.x==d.x and b.y==d.y then
                            d.z=0
                        end
                    end
                end
            end
    
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            tab3[pos].x=tab3[pos].x+touch.deltaX
            tab3[pos].y=tab3[pos].y+touch.deltaY
        end
    
        if touch.state==ENDED then  
            for a,b in pairs(tab1) do
                if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                        tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                        b.z==0 then
                    tab3[pos].x=b.x
                    tab3[pos].y=b.y
                    b.z=1
                    return
                end            
            end
            tab3[pos].x=tab2[pos].x
            tab3[pos].y=tab2[pos].y
            tab3[pos].z=.5
        end
    end
    

    Posting my progress below

  • edited April 15 Posts: 124

    @dave1707 Here is my progress but I think my for, do for the sprite in draw is written wrong. I’ve been trying to make this work for hours.

        function setup()
        spriteMode(CENTER)
        ellipseMode(CENTER)
        displayMode(FULLSCREEN)
    
        et=ElapsedTime
        count=1
    
        tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
        tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
        tab3={vec3(290,-120,.5),vec3(290,-280,.5),vec3(290,190,.5),vec3(290,35,.5)}  -- position of sprite and scaler value
        tab4={
                asset.builtin.Planet_Cute.Character_Princess_Girl,
                asset.builtin.Planet_Cute.Character_Boy,
                asset.builtin.Planet_Cute.Character_Cat_Girl,
                asset.builtin.Planet_Cute.Character_Horn_Girl }
        imageSize = spriteSize(tab4[1])
        scaler = .50
    
       tab5={}
        for z=1,#tab4 do
            a=math.random(#tab4)
            table.insert(tab5,tab4[a])
            table.remove(tab4,a)
    end   
    
    end
    function draw()
        background(98)
        strokeWidth(5)
        fill(128)
            for a,b in pairs(tab1) do
            ellipse(b.x, b.y, 100)
        end
        for a,b in pairs(tab2) do
            ellipse(b.x, b.y, 50)
        end
        --for a,b in pairs(tab3) do
            --sprite(tab4[a],b.x,b.y,imageSize*b.z)
        --end
            if ElapsedTime-et>.50 then
            if count<#tab5 then
                count=count+1
                et=ElapsedTime
            end
            end
        for z=1,count do
            sprite(tab5[z],WIDTH/2+tab3[z].x,HEIGHT/2+tab3[z].y,imageSize*scaler)
        end
    
    end
    function touched(touch) 
        if touch.state==BEGAN then
            pos=0
            for a,b in pairs(tab3) do
                if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                            touch.y<b.y+40 then
                    b.z=1   -- scaler
                    pos=a
                    for c,d in pairs(tab1) do
                        if b.x==d.x and b.y==d.y then
                            d.z=0
                        end
                    end
                end
            end
    
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            tab3[pos].x=tab3[pos].x+touch.deltaX
            tab3[pos].y=tab3[pos].y+touch.deltaY
        end
    
        if touch.state==ENDED then  
            for a,b in pairs(tab1) do
                if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                        tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                        b.z==0 then
                    tab3[pos].x=b.x
                    tab3[pos].y=b.y
                    b.z=1
                    return
                end            
            end
            tab3[pos].x=tab2[pos].x
            tab3[pos].y=tab2[pos].y
            tab3[pos].z=.5
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Here’s the other code with random added.

    displayMode(FULLSCREEN)    
    
    function setup()
        spriteMode(CENTER)
        ellipseMode(CENTER)
    
        tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
        tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
        tab3={vec3(800,100,.5),vec3(800,250,.5),vec3(800,400,.5),vec3(800,550,.5)}  -- position of sprite and scaler value
        tab4={
                asset.builtin.Planet_Cute.Character_Princess_Girl,
                asset.builtin.Planet_Cute.Character_Boy,
                asset.builtin.Planet_Cute.Character_Cat_Girl,
                asset.builtin.Planet_Cute.Character_Horn_Girl
            }
        imageSize = spriteSize(tab4[1])
        tab5={}
        for z=1,#tab4 do
            a=math.random(#tab4)
            table.insert(tab5,tab4[a])
            table.remove(tab4,a)
        end
        scaler = .50
    end
    
    function draw()
        background(98)
        strokeWidth(5)
        fill(128)
        for a,b in pairs(tab1) do
            ellipse(b.x, b.y, 100)
        end
        for a,b in pairs(tab2) do
            ellipse(b.x, b.y, 50)
        end
        for a,b in pairs(tab3) do
            sprite(tab5[a],b.x,b.y,imageSize*b.z)
        end
    end
    
    function touched(touch)    
        if touch.state==BEGAN then
            pos=0
            for a,b in pairs(tab3) do
                if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                            touch.y<b.y+40 then
                    b.z=1   -- scaler
                    pos=a
                    for c,d in pairs(tab1) do
                        if b.x==d.x and b.y==d.y then
                            d.z=0
                        end
                    end
                end
            end
    
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            tab3[pos].x=tab3[pos].x+touch.deltaX
            tab3[pos].y=tab3[pos].y+touch.deltaY
        end
    
        if touch.state==ENDED then  
            for a,b in pairs(tab1) do
                if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                        tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                        b.z==0 then
                    tab3[pos].x=b.x
                    tab3[pos].y=b.y
                    b.z=1
                    return
                end            
            end
            tab3[pos].x=tab2[pos].x
            tab3[pos].y=tab2[pos].y
            tab3[pos].z=.5
        end
    end
    
  • edited April 15 Posts: 124

    @dave1707 I already had the random added in my code progress. The problem I’m having is making the sprites appear 1 at a time randomly in those specific locations then making the sprites moveable and scale from small circle to big circle.

  • edited April 15 Posts: 124

    @dave1707 I have them all appearing after 1sec + random + moveable and scale to circles. All I need to do now is make them appear 1 by 1 every second. Is there a easy way?

    displayMode(FULLSCREEN)    
    
    function setup()
        spriteMode(CENTER)
        ellipseMode(CENTER)
    
        et=ElapsedTime
        count=30
    
        tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
        tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
        tab3={vec3(800,100,.5),vec3(800,250,.5),vec3(800,400,.5),vec3(800,550,.5)}  -- position of sprite and scaler value
        tab4={
                asset.builtin.Planet_Cute.Character_Princess_Girl,
                asset.builtin.Planet_Cute.Character_Boy,
                asset.builtin.Planet_Cute.Character_Cat_Girl,
                asset.builtin.Planet_Cute.Character_Horn_Girl
            }
        imageSize = spriteSize(tab4[1])
        tab5={}
        for z=1,#tab4 do
            a=math.random(#tab4)
            table.insert(tab5,tab4[a])
            table.remove(tab4,a)
        end
        scaler = .50
    end
    
    function draw()
        background(98)
        strokeWidth(5)
        fill(128)
        for a,b in pairs(tab1) do
            ellipse(b.x, b.y, 100)
        end
        for a,b in pairs(tab2) do
            ellipse(b.x, b.y, 50)
        end
    
    
           fill(255)
        if ElapsedTime-et>1 then
            count = count - 1
            et=ElapsedTime
        end
    
       if count<=28 then
            for a,b in pairs(tab3) do
        sprite(tab5[a],b.x,b.y,imageSize*b.z)
                end
            end
    
         text(count,WIDTH*.10,HEIGHT*.10)
    end
    
    function touched(touch)    
        if touch.state==BEGAN then
            pos=0
            for a,b in pairs(tab3) do
                if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                            touch.y<b.y+40 then
                    b.z=1   -- scaler
                    pos=a
                    for c,d in pairs(tab1) do
                        if b.x==d.x and b.y==d.y then
                            d.z=0
                        end
                    end
                end
            end
    
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            tab3[pos].x=tab3[pos].x+touch.deltaX
            tab3[pos].y=tab3[pos].y+touch.deltaY
        end
    
        if touch.state==ENDED then  
            for a,b in pairs(tab1) do
                if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                        tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                        b.z==0 then
                    tab3[pos].x=b.x
                    tab3[pos].y=b.y
                    b.z=1
                    return
                end            
            end
            tab3[pos].x=tab2[pos].x
            tab3[pos].y=tab2[pos].y
            tab3[pos].z=.5
        end
    end
    
  • edited April 15 Posts: 124

    @dave1707 I figured it out but I feel like there is a simpler method.

    displayMode(FULLSCREEN)    
    
    function setup()
        spriteMode(CENTER)
        ellipseMode(CENTER)
    
        et=ElapsedTime
        count=30
    
        tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
        tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
        tab6={vec2(800,250),vec2(800,400),vec2(800,550)} 
        tab7={vec2(800,400),vec2(800,550)}
        tab8={vec2(800,550)}
        tab3={vec3(800,108,.5),vec3(800,258,.5),vec3(800,408,.5),vec3(800,558,.5)}  -- position of sprite and scaler value
        tab4={
                asset.builtin.Planet_Cute.Character_Princess_Girl,
                asset.builtin.Planet_Cute.Character_Boy,
                asset.builtin.Planet_Cute.Character_Cat_Girl,
                asset.builtin.Planet_Cute.Character_Horn_Girl
            }
        imageSize = spriteSize(tab4[1])
        tab5={}
        for z=1,#tab4 do
            a=math.random(#tab4)
            table.insert(tab5,tab4[a])
            table.remove(tab4,a)
        end
        scaler = .50
    end
    
    function draw()
        background(98)
        strokeWidth(5)
        fill(128)
        for a,b in pairs(tab1) do
            ellipse(b.x, b.y, 100)
        end
        for a,b in pairs(tab2) do
            ellipse(b.x, b.y, 50)
        end
           fill(255)
        if ElapsedTime-et>1 then
            count = count - 1
            et=ElapsedTime
        end
    
       if count<=28 then
            for a,b in pairs(tab3) do
        sprite(tab5[a],b.x,b.y,imageSize*b.z)
                end
            end
        pushStyle()
        fill(128)
        if count==28 then 
        for a,b in pairs(tab6) do
            ellipse(b.x, b.y, 50)
        end
            end
    
         if count==27 then 
        for a,b in pairs(tab7) do
            ellipse(b.x, b.y, 50)
        end
            end
    
           if count==26 then 
        for a,b in pairs(tab8) do
            ellipse(b.x, b.y, 50)
        end
            end
       popStyle()
    
         text(count,WIDTH*.10,HEIGHT*.10)
    end
    
    function touched(touch)    
        if touch.state==BEGAN then
            pos=0
            for a,b in pairs(tab3) do
                if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                            touch.y<b.y+40 then
                    b.z=1   -- scaler
                    pos=a
                    for c,d in pairs(tab1) do
                        if b.x==d.x and b.y==d.y then
                            d.z=0
                        end
                    end
                end
            end
    
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            tab3[pos].x=tab3[pos].x+touch.deltaX
            tab3[pos].y=tab3[pos].y+touch.deltaY
        end
    
        if touch.state==ENDED then  
            for a,b in pairs(tab1) do
                if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                        tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                        b.z==0 then
                    tab3[pos].x=b.x
                    tab3[pos].y=b.y
                    b.z=1
                    return
                end            
            end
            tab3[pos].x=tab2[pos].x
            tab3[pos].y=tab2[pos].y
            tab3[pos].z=.5
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 8,398
    displayMode(FULLSCREEN)    
    
    function setup()
        spriteMode(CENTER)
        ellipseMode(CENTER)
    
        tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
        tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
        tab3={vec3(800,100,.5),vec3(800,250,.5),vec3(800,400,.5),vec3(800,550,.5)}  -- position of sprite and scaler value
        tab4={
                asset.builtin.Planet_Cute.Character_Princess_Girl,
                asset.builtin.Planet_Cute.Character_Boy,
                asset.builtin.Planet_Cute.Character_Cat_Girl,
                asset.builtin.Planet_Cute.Character_Horn_Girl
            }
        imageSize = spriteSize(tab4[1])
        tab5={}
        for z=1,#tab4 do
            a=math.random(#tab4)
            table.insert(tab5,tab4[a])
            table.remove(tab4,a)
        end
        scaler = .50
        cnt=0
        loop=0
    end
    
    function draw()
        background(98)
        strokeWidth(5)
        fill(128)
        for a,b in pairs(tab1) do
            ellipse(b.x, b.y, 100)
        end
        for a,b in pairs(tab2) do
            ellipse(b.x, b.y, 50)
        end
        delay()
        for z=1,cnt do
            sprite(tab5[z],tab3[z].x,tab3[z].y,imageSize*tab3[z].z)        
        end
    end
    
    function delay()
        loop=loop+1
        if loop>60 and cnt<#tab5 then
            cnt=cnt+1
            loop=0
        end            
    end
    
    function touched(touch)    
        if touch.state==BEGAN then
            pos=0
            for a,b in pairs(tab3) do
                if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                            touch.y<b.y+40 then
                    b.z=1   -- scaler
                    pos=a
                    for c,d in pairs(tab1) do
                        if b.x==d.x and b.y==d.y then
                            d.z=0
                        end
                    end
                end
            end
    
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            tab3[pos].x=tab3[pos].x+touch.deltaX
            tab3[pos].y=tab3[pos].y+touch.deltaY
        end
    
        if touch.state==ENDED then  
            for a,b in pairs(tab1) do
                if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                        tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                        b.z==0 then
                    tab3[pos].x=b.x
                    tab3[pos].y=b.y
                    b.z=1
                    return
                end            
            end
            tab3[pos].x=tab2[pos].x
            tab3[pos].y=tab2[pos].y
            tab3[pos].z=.5
        end
    end
    
  • Posts: 124

    @dave1707 A delay loop function, nice! Thanks for the knowledge.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc There’s a problem in that delay function. Once all the sprites are shown, loop will add 1 to itself as long as the program is running. Even though that’s not a problem, here’s a better one. With the new one, once all the sprites are shown, the if statement is false and doesn’t execute any of the code inside it anymore.

    function delay()
        if cnt<#tab5 then
            loop=loop+1
            if loop>60 then 
                cnt=cnt+1
                loop=0
            end  
        end          
    end
    
  • edited April 21 Posts: 124

    @dave1707 I have some questions.

    I want to add more sprites(3 for example) but keep the same number of small circle slots so once after scoring the first 3(round 1), it will repeat same beginning process but load the rest of the unused sprites so round 2 can began.

    Also this is for visual effect but how do I get the sprites to score 1 by 1, left to right each round. Meaning how do I get the value scores to show up as text near the sprite for about 1 sec and simultaneously while the value text are being displayed on each sprite(1 by 1, left to right) I want it to update a onscreen scoreboard text that starts at 00 when game begins.

    The two bottom ellipses I added are for round 2.

    function setup()
    
        displayMode(FULLSCREEN)
        t={1,2,3}   -- number for each entry in tab4
        -- position of large circles and occupied
        tab1={vec3(320,290,0),vec3(445,290,0),vec3(570,290,0)} --1st round ellipse
        tab7={vec3(320,110,0),vec3(445,110,0)} --2nd round ellipse 
        -- position of small circles
        tab2={vec2(835,335),vec2(835,265),vec2(835,195),}  
        -- name, value, original x,y position, current x,y position, size
        tab4={  {n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                    v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Character_Boy,
                    v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                    v=12,x1=0,y1=0,x2=0,y2=0,s=.5}}
    
        imageSize = spriteSize(tab4[1].n)
        for z=1,#t do
            a=math.random(#t)
            tab4[z].x1=tab2[t[a]].x
            tab4[z].y1=tab2[t[a]].y
            tab4[z].x2=tab2[t[a]].x
            tab4[z].y2=tab2[t[a]].y
            table.remove(t,a)
        end
        cnt=0
        loop=0   
    end
    
    function draw()   
        background(40, 40, 50) 
    
        pushStyle()
        fontSize(30)
        fill(40,40,50) 
        popStyle()  
    
        if cc==3 then
            noFill()
            fontSize(30)
            --ellipse(400,500,100)
            sprite(asset.builtin.Cargo_Bot.Play_Button, 400,500)
            if showScore then
                fill(255,0,0)
                text(score,400,500)
            end
    
            if cc==3 and showScore==true then
                   for z=1,cnt do   
        end 
    
            end
        end
    
        delay()
        strokeWidth(5)
        fill(128)
        for a,b in pairs(tab1) do
            ellipse(b.x, b.y, 100)  
        end 
    
        for a,b in pairs(tab2) do
            ellipse(b.x, b.y, 50)
        end
    
        for a,b in pairs(tab7) do 
            ellipse(b.x, b.y, 100)  
        end 
    
        for z=1,cnt do
            pushMatrix()
            zLevel(.3)
            sprite(tab4[z].n,tab4[z].x2,tab4[z].y2,imageSize*tab4[z].s)  
            popMatrix()    
        end  
    end
    
    function delay()
        if cnt<#tab4 then
            loop=loop+1
            if loop>60 then 
                cnt=cnt+1
                loop=0
            end  
        end          
    end
    
    function touched(touch)    
        if touch.state==BEGAN then
            if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
                showScore=true
            end
            pos=0
            for a,b in pairs(tab4) do
                if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                            touch.y<b.y2+40 then
                    b.s=1   -- scaler
                    pos=a
                    for c,d in pairs(tab1) do
                        if b.x2==d.x and b.y2==d.y then
                            d.z=0
                        end
                    end
                end
            end
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            tab4[pos].x2=tab4[pos].x2+touch.deltaX
            tab4[pos].y2=tab4[pos].y2+touch.deltaY
        end
    
        if touch.state==ENDED then  
            score=0
            cc=0
            showScore=false
            for a,b in pairs(tab1) do
                if tab4[pos].x2>b.x-55 and tab4[pos].x2<b.x+55 and 
                        tab4[pos].y2>b.y-55 and tab4[pos].y2<b.y+55 and
                        b.z==0 then
                    tab4[pos].x2=b.x
                    tab4[pos].y2=b.y
                    b.z=1
                    for c,d in pairs(tab4) do
                        if d.x1~=d.x2 and d.y1~=d.y2 then
                            score=score+d.v
                            cc=cc+1
                        end
                    end            
                    return
                end            
            end
            tab4[pos].x2=tab4[pos].x1
            tab4[pos].y2=tab4[pos].y1
            tab4[pos].s=.5
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc You can create a table that has all your sprites in it. On the first round, you pull 3 random sprites out. On the second round, pull out the rest of them in random order. As for displaying the scores, you just need to have a variable that you set. As the count goes down, you display the value. If it’s between a certain range you display it in one spot, the next range in another spot and the third spot for the remaining range.

  • Posts: 124

    @dave1707 How do I tell the code to stop after 3.

      for z=1,#t do
            a=math.random(#t)
            tab4[z].x1=tab2[t[a]].x
            tab4[z].y1=tab2[t[a]].y
            tab4[z].x2=tab2[t[a]].x
            tab4[z].y2=tab2[t[a]].y
            table.remove(t,a)
        end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc How many programs are you working on.

    Since you’re adding more sprites, you need to change the way you randomly pick the sprites. The above routine isn’t going to work anymore. You still only pick 3 sprites from the full table. When a sprite is selected, remove it from the table. If you’re always picking 3 sprites at a time, the table should contain multiples of 3. Each time you pick a sprite, assign a small circle to it. I believe I mentioned somewhere that it’s easier to write code when you know in advance what you want to do, so you’re not always rewriting the code because you change something you already coded for.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Heres an example of picking a random name from a table of 9 and assigning it 1 of 3 random small circles. This shows 3 rounds.

    function setup()    
        names={"name1","name2","name3","name4","name5",
                  "name6","name7","name8","name9"}
        circles={"circ1","circ2","circ3"}
        print("round 1========")
        pick()  
    
        print("round 2========")
        pick() 
    
        print("round 3========")
        pick()  
    
    end
    
    function pick()
        show={}
        temp={}
        for z=1,3 do    -- temp table of small circles positions
            table.insert(temp,circles[z])        
        end
        for z=1,3 do    -- pick random name and random circle
            a=math.random(#names)
            b=math.random(#temp)
            table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
            table.remove(names,a)
            table.remove(temp,b)       
        end
        for a,b in pairs(show) do
            print(b.name,b.pos)
        end
    end
    
  • Posts: 124

    @dave1707 Hey, I added a coroutine to this. Basically every round after 1 has a time limit or touch event that starts the next. For some reason I can’t get round 3’s time limit to work no matter what I try. Can u take a look?


    function setup() co3 = coroutine.create(CoRoutineTest) parameter.watch("coroutine.status(co3)") co = coroutine.create(function() for i = 1, 3, 1 do print(i) print("round ========".. i) pick() print(coroutine.status(co)) if i == 1 then coroutine.yield() coroutine.resume(co3) end if i == 2 then coroutine.yield() coroutine.yield(co3) end end if i == 3 then end end) print(coroutine.status(co)) names={"name1","name2","name3","name4","name5", "name6","name7","name8","name9"} circles={"circ1","circ2","circ3"} coroutine.resume(co) end function pick() show={} temp={} for z=1,3 do -- temp table of small circles positions table.insert(temp,circles[z]) end for z=1,3 do -- pick random name and random circle a=math.random(#names) b=math.random(#temp) table.insert(show,{name=names[a],pos=temp[b]}) -- update show table table.remove(names,a) table.remove(temp,b) end for a,b in pairs(show) do print(b.name,b.pos) end end function touched(t) if t.state==BEGAN then coroutine.resume(co) end end function CoRoutineTest() print("time test started") local stime = os.clock() while os.clock() < stime + 5 do coroutine.yield(os.clock()) end print("time test ended") coroutine.resume(co) return os.clock() end function draw() background(40,40,50) local th = coroutine.status(co3) if th ~= "dead" then local _,t = coroutine.resume(co3) text("running at " .. t,WIDTH/2,HEIGHT/2) else text("ended",WIDTH/2,HEIGHT/2) end end
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc I don’t know why you’re using a coroutine for a timer. You can just use a simple timer. As far as as I know, a coroutine is mostly used when you have a routine that takes a long time to complete. You yield that routine frequently to allow the other code to run while the coroutine takes however long it needs to run.

  • Posts: 124

    @dave1707 I feel like a coroutine would give me more control over each round especially when I later add more code to it. Not sure how a simple timer could give me as much control and be less code.

  • dave1707dave1707 Mod
    edited May 6 Posts: 8,398

    @Jarc I don’t know what you want to do with the code you show, but here’s some of you code with a simple timer.

    Code removed. Updated code below.
    
  • edited May 6 Posts: 124

    @dave1707 basically that but can you add if the timer goes out the next round starts.

    Here’s my version with coroutines but I’ll use yours. It does the same as yours but if the timer goes out the next round starts. (Btw the os clock in this doesn’t reset unless you close Codea :| )


    function setup() r1 = coroutine.create(round1timer) parameter.watch("coroutine.status(r1)") r2 = coroutine.create(round2timer) parameter.watch("coroutine.status(r2)") co = coroutine.create(function() for i = 1, 3, 1 do print(i) print("round ========".. i) pick() print(coroutine.status(co)) if i == 1 then coroutine.yield() coroutine.resume(r1) end if i == 2 then coroutine.yield() coroutine.yield(r1) end end if i == 3 then end end) print(coroutine.status(co)) names={"name1","name2","name3","name4","name5", "name6","name7","name8","name9"} circles={"circ1","circ2","circ3"} coroutine.resume(co) end function pick() show={} temp={} for z=1,3 do -- temp table of small circles positions table.insert(temp,circles[z]) end for z=1,3 do -- pick random name and random circle a=math.random(#names) b=math.random(#temp) table.insert(show,{name=names[a],pos=temp[b]}) -- update show table table.remove(names,a) table.remove(temp,b) end for a,b in pairs(show) do print(b.name,b.pos) end end function touched(t) if t.state==BEGAN then coroutine.resume(co) round1 = false end end function round1timer() print("round 1 timer started") local stime = os.clock() while os.clock() < stime + 5 do coroutine.yield(os.clock()) end print("round 1 timer ended") coroutine.resume(co) return os.clock() end function round2timer() print("round 2 timer started") local stime = os.clock() while os.clock() < stime + 5 do coroutine.yield(os.clock()) end coroutine.resume(co) print("round 2 timer ended") coroutine.resume(co) return os.clock() end function draw() background(40,40,50) local th = coroutine.status(r1) if round1 == false then th = "dead" end if th ~= "dead" then local _,t = coroutine.resume(r1) text("running at " .. t,WIDTH/2,HEIGHT/2) else text("round 1 ended",WIDTH/2,HEIGHT/2) end --local th2 = coroutine.status(r2) if th == "dead" then local _,t = coroutine.resume(r2) text("running at " .. t,WIDTH/2+300,HEIGHT/2) else text("round 2 ended",WIDTH/2+300,HEIGHT/2) end end
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Heres the updated code.

    displayMode(STANDARD)
    
    function setup()
        fill(255)
        round=0
        names={"name1","name2","name3","name4","name5",
                  "name6","name7","name8","name9"}
        circles={"circ1","circ2","circ3"}    
        s=require("socket")
    end
    
    function pick()
        st=s:gettime()
        show={}
        temp={}
        for z=1,3 do    -- temp table of small circles positions
            table.insert(temp,circles[z])        
        end
        for z=1,3 do    -- pick random name and random circle
            a=math.random(#names)
            b=math.random(#temp)
            table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
            table.remove(names,a)
            table.remove(temp,b)       
        end
        round=round+1
        print("Round -----------  "..round)
        for a,b in pairs(show) do
            print(b.name,b.pos)
        end
    end
    
    function touched(t)
        if t.state==BEGAN and #names>0 and not done then
            pick()
        end
    end
    
    function draw()
        background(40,40,50)
        text("Tap screen to start.",WIDTH/2,HEIGHT-50)
        timer(10)
    end
    
    function timer(max)
        if st==nil then
            return
        end
        if #names>0 then
            curr=s:gettime()-st
            text(string.format("Time remaining  %4.1f",max-curr),WIDTH/2,HEIGHT/2)
            if curr>max then
                pick()
            end
        end    
    end
    
  • edited May 6 Posts: 124

    @dave1707 Thanks! This is a little of a backtrack but I was working on the visual for the game. Combining the two codes you helped me with. The problem I was having is the rest of the sprites loading in the far left corner. I fixed it by doing this

    function delay()
        if cnt<#names-3 then
    

    but not sure if that was the best way to fix it

    Here’s the full code without the fix.


    function setup() names={"name1","name2","name3","name4","name5", "name6","name7","name8","name9"} circles={"circ1","circ2","circ3"} t={1,2,3} -- number for each entry in tab4 tab1={vec3(320,290,0),vec3(445,290,0),vec3(570,290,0)} tab2={vec2(835,335),vec2(835,265),vec2(835,195),} tab4={ {n=asset.builtin.Planet_Cute.Character_Princess_Girl, v=23,x1=0,y1=0,x2=0,y2=0,s=.5}, {n=asset.builtin.Planet_Cute.Character_Boy, v=28,x1=0,y1=0,x2=0,y2=0,s=.5}, {n=asset.builtin.Planet_Cute.Character_Cat_Girl, v=12,x1=0,y1=0,x2=0,y2=0,s=.5}, {n=asset.builtin.Planet_Cute.Gem_Green, v=23,x1=0,y1=0,x2=0,y2=0,s=.5}, {n=asset.builtin.Planet_Cute.Gem_Orange, v=28,x1=0,y1=0,x2=0,y2=0,s=.5}, {n=asset.builtin.Planet_Cute.Gem_Blue, v=12,x1=0,y1=0,x2=0,y2=0,s=.5}, {n=asset.builtin.Planet_Cute.Star, v=23,x1=0,y1=0,x2=0,y2=0,s=.5}, {n=asset.builtin.Planet_Cute.Tree_Short, v=28,x1=0,y1=0,x2=0,y2=0,s=.5}, {n=asset.builtin.Planet_Cute.Heart, v=12,x1=0,y1=0,x2=0,y2=0,s=.5},} table.move(tab4,1,9,1,names) table.move(tab2,1,9,1,circles) print("round 1========") pick() imageSize = spriteSize(names[1].n) for z=1,#t do a=math.random(#t) names[z].x1=circles[t[a]].x names[z].y1=circles[t[a]].y names[z].x2=circles[t[a]].x names[z].y2=circles[t[a]].y table.remove(t,a) end cnt=0 loop=0 end function pick() show={} temp={} for z=1,#t do -- temp table of small circles positions table.insert(temp,circles[z]) end for z=1,#t do -- pick random name and random circle a=math.random(#names) b=math.random(#temp) table.insert(show,{name=names[a],pos=temp[b]}) -- update show table table.remove(names,a) table.remove(temp,b) end for a,b in pairs(show) do print(b.name,b.pos) end end function draw() background(0) pushStyle() fontSize(30) fill(40,40,50) popStyle() fontSize(30) if cc==3 then noFill() ellipse(400,500,100) if showScore then fill(255,0,0) text(score,400,500) end end delay() strokeWidth(5) fill(128) for a,b in pairs(tab1) do ellipse(b.x, b.y, 100) end for a,b in pairs(circles) do ellipse(b.x, b.y, 50) end for z=1,cnt do pushMatrix() zLevel(.3) sprite(names[z].n,names[z].x2,names[z].y2,imageSize*names[z].s) popMatrix() end end function delay() if cnt<#names then loop=loop+1 if loop>60 then cnt=cnt+1 loop=0 end end end function touched(touch) if touch.state==BEGAN then if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then showScore=true end pos=0 for a,b in pairs(tab4) do if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and touch.y<b.y2+40 then b.s=1 -- scaler pos=a for c,d in pairs(tab1) do if b.x2==d.x and b.y2==d.y then d.z=0 end end end end end if pos==0 then return end if touch.state==MOVING then tab4[pos].x2=tab4[pos].x2+touch.deltaX tab4[pos].y2=tab4[pos].y2+touch.deltaY end if touch.state==ENDED then score=0 cc=0 showScore=false for a,b in pairs(tab1) do if tab4[pos].x2>b.x-55 and tab4[pos].x2<b.x+55 and tab4[pos].y2>b.y-55 and tab4[pos].y2<b.y+55 and b.z==0 then tab4[pos].x2=b.x tab4[pos].y2=b.y b.z=1 for c,d in pairs(tab4) do if d.x1~=d.x2 and d.y1~=d.y2 then score=score+d.v cc=cc+1 end end return end end tab4[pos].x2=tab4[pos].x1 tab4[pos].y2=tab4[pos].y1 tab4[pos].s=.5 end end
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Sorry, but I don’t know what you’re trying to do. I don’t see any difference between adding your fix or leaving it out.

  • edited May 6 Posts: 124

    (Code updated)
    @dave1707 Sorry, here’s a better example with timer included.

    How do I
    1) make 3 sprites load every round from table, no duplicates. (Round 1 works)
    2)Get the delay function to work every round (I had to delete to get this code to work)
    3)Stop timer and next button from producing rounds after round 3
    4)Keep the value score and update it every round (each round it adds)
    5) make the sprites in the big circles stay every round.

    function setup()  
        fill(255)
        round=0  
        names={"name1","name2","name3","name4","name5",
                  "name6","name7","name8","name9"}
        circles={"circ1","circ2","circ3"}
        s=require("socket")
    
        buttonSize=vec2(500,600)
    
        t={1,2,3}   -- number for each entry in tab4
    
        tab1={vec3(320,290,0),vec3(445,290,0),vec3(570,290,0),vec3(380,190,0),vec3(510,190,0),vec3(445,90,0)}
        tab2={vec2(835,335),vec2(835,265),vec2(835,195),} 
        tab4={  {n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                    v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Character_Boy,
                    v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                    v=12,x1=0,y1=0,x2=0,y2=0,s=.5}, 
                {n=asset.builtin.Planet_Cute.Gem_Green,
                    v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Gem_Orange,
                    v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Gem_Blue,
                    v=12,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Star,
                    v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Tree_Short,
                    v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Heart,
                    v=12,x1=0,y1=0,x2=0,y2=0,s=.5},}
    
        table.move(tab4,1,9,1,names)
        table.move(tab2,1,9,1,circles)
        print("round 1========")
        pick() 
    
    
        imageSize = spriteSize(names[1].n)
    
        for z=1,#t do
            a=math.random(#t)
            names[z].x1=circles[t[a]].x
            names[z].y1=circles[t[a]].y
            names[z].x2=circles[t[a]].x
            names[z].y2=circles[t[a]].y
            table.remove(t,a)        
        end
        cnt=0
        loop=0
    end
    
    function pick()
        st=s:gettime()
        show={}
        temp={}
        for z=1,3 do    -- temp table of small circles positions
            table.insert(temp,circles[z])        
        end
        for z=1,3 do    -- pick random name and random circle
            a=math.random(#names)
            b=math.random(#temp)
            table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
            table.remove(names,a)
            table.remove(temp,b)  
            end
        round=round+1
        print("Round -----------  "..round)
        for a,b in pairs(show) do
            print(b.name,b.pos)
        end
    end
    
    function draw()
        background(0)
    
        text("round 1", 200, 300)
        text("round 2", 250, 200)
        text("round 3", 320, 100)
    
          text("Tap next to start.",WIDTH/2,HEIGHT-50)
        timer(10)
    
        pushStyle()
        fontSize(30)
        fill(40,40,50) 
        popStyle()  
    
        fontSize(30)
    
        if cc==3 then
            noFill()
            ellipse(400,500,100)
            if showScore then
                fill(255,0,0)
                text(score,400,500)
            end
        end
    
        sprite(asset.builtin.Cargo_Bot.Next_Button,buttonSize.x,buttonSize.y)
    
        strokeWidth(5)
        fill(128)
         for a,b in pairs(tab1) do
            ellipse(b.x, b.y, 100)  
        end 
    
        for a,b in pairs(circles) do
            ellipse(b.x, b.y, 50)
        end
    
            for z=1,3 do
            pushMatrix()
            zLevel(.3) 
            sprite(names[z].n,names[z].x2,names[z].y2,imageSize*names[z].s) 
            popMatrix()    
        end  
    end
    
    function touched(touch)   
    
                 if touch.state==BEGAN and
    touch.x>buttonSize.x-75 and touch.x<buttonSize.x+75 and touch.y>buttonSize.y-50 and touch.y<buttonSize.y+50 and #names>0 and not done then
            pick()
        end
    
        if touch.state==BEGAN then
            if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
                showScore=true
            end
            pos=0
            for a,b in pairs(tab4) do
                if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                            touch.y<b.y2+40 then
                    b.s=1   -- scaler
                    pos=a
                    for c,d in pairs(tab1) do
                        if b.x2==d.x and b.y2==d.y then
                            d.z=0
                            end
                    end
                end
            end
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            tab4[pos].x2=tab4[pos].x2+touch.deltaX
            tab4[pos].y2=tab4[pos].y2+touch.deltaY
        end
    
        if touch.state==ENDED then  
            score=0
            cc=0
            showScore=false
            for a,b in pairs(tab1) do
                if tab4[pos].x2>b.x-55 and tab4[pos].x2<b.x+55 and 
                        tab4[pos].y2>b.y-55 and tab4[pos].y2<b.y+55 and
                        b.z==0 then
                    tab4[pos].x2=b.x
                    tab4[pos].y2=b.y
                    b.z=1
                    for c,d in pairs(tab4) do
                        if d.x1~=d.x2 and d.y1~=d.y2 then
                            score=score+d.v
                            cc=cc+1
                        end
                    end            
                    return
                end            
            end
            tab4[pos].x2=tab4[pos].x1
            tab4[pos].y2=tab4[pos].y1
            tab4[pos].s=.5
        end
    end
    
    function timer(max)
        if st==nil then
            return
        end
        if #names>0 then
            curr=s:gettime()-st
            text(string.format("Time remaining  %4.1f",max-curr),WIDTH/2,HEIGHT/2)
            if curr>max then
                pick()
            end
        end   
    end 
    
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Your code seems to change each time you post it. You’re last code had 3 large circles. This code has six large circles. A lot of what you want fixed was working at some point in previous code. You seem to be going in circles as you change things. I don’t know if you’re trying to add more things on the fly or if this was all planned up front.

  • Posts: 124

    @dave1707 It was a lot to ask of you, sorry. I figured most of it out but I have one question. I have the code set to when I load 4 sprites into the first row slots and press ready the score appears. My question is how can I get just the sprites in that 1st row to move into another table(when I press ready). I know about table.move but I’m not sure how to only pick those sprites.

    function setup()
    
        score=0
        round=0 
        s=require("socket")
    
        readybuttonSize=vec2(500,500)
    
        buttonSize=vec2(500,600)
    
        slot = { vec2(835,335), vec2(835,265), vec2(835,195), vec2(900,335), vec2(900,265), vec2(900, 195) }
    
        gameslot = { vec3(320,290,0), vec3(445,290,0), vec3(570,290,0), vec3(695,290,0), vec3(380,180,0), vec3(505,180,0), vec3(630,180,0) }
    
        deck = {{n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                    v=5,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Character_Boy,
                    v=6,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                    v=8,x1=0,y1=0,x2=0,y2=0,s=.5}, 
                {n=asset.builtin.Planet_Cute.Gem_Green,
                    v=9,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Gem_Orange,
                    v=10,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Gem_Blue,
                    v=2,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Star,
                    v=3,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Tree_Short,
                    v=4,x1=0,y1=0,x2=0,y2=0,s=.5}, 
                {n=asset.builtin.Planet_Cute.Heart,
                    v=5,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Chest_Closed,
                    v=1,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Enemy_Bug,
                    v=7,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Ramp_South,
                    v=3,x1=0,y1=0,x2=0,y2=0,s=.5}}
    
                pick()
    
                imageSize = spriteSize(deck[1].n)
    
    end
    
    function pick()
    
        st=s:gettime()
        show={}
        temp={}
    
        for z=1,6 do    -- temp table of small circles positions
            table.insert(temp,slot[z])        
        end
    
        for z=1,6 do    -- pick random name and random circle
            a=math.random(#deck)
            b=math.random(#temp)
            table.insert(show,{name=deck[a],pos=temp[b]})  -- update show table
            table.remove(deck,a)
            table.remove(temp,b)  
        end
    
        round=round+1
        print("Round -----------  "..round)
    
        for k,v in pairs(show) do
            print(v.name,v.pos)   
        end
    
         for z=1,6 do
            show[z].x1=show[z].pos.x
            show[z].y1=show[z].pos.y
            show[z].x2=show[z].pos.x
            show[z].y2=show[z].pos.y
            show[z].v=show[z].name.v
            show[z].s=show[z].name.s
            show[z].n=show[z].name.n
        end
    end
    
    function draw()   
        background(0)     
    
        text("round 1", 200, 300)
        text("round 2", 250, 200)
    
        text("Tap next to start.",WIDTH/2,HEIGHT-50)
        timer(30)
        pushStyle()
        fontSize(30)
        fill(40,40,50) 
        popStyle()  
        fontSize(30)
        if cc==4 then 
            rectMode(CENTER)
            pushStyle()
            noFill()
            rect(readybuttonSize.x,readybuttonSize.y,100)
            popStyle()
            text("Ready",500,500)
            if sc==true then
                scoring()
            end
        end
    
        sprite(asset.builtin.Cargo_Bot.Next_Button,buttonSize.x,buttonSize.y)   
    
        fill(128)
    
        strokeWidth(5)
    
        for k,v in pairs(slot) do
            ellipse(v.x, v.y, 50)  
        end 
    
        for k,v in pairs(gameslot) do
            ellipse(v.x, v.y, 100)
        end
    
        for z=1,6 do
            pushMatrix()
            zLevel(.3) 
            sprite(show[z].n,show[z].x2,show[z].y2,imageSize*show[z].s)
            popMatrix()    
        end  
    end
    
    function touched(touch)    
    
            if touch.state==BEGAN and
    touch.x>readybuttonSize.x-75 and touch.x<readybuttonSize.x+75 and touch.y>readybuttonSize.y-50 and touch.y<readybuttonSize.y+50 and cc==4 then
            sc=true
            end
    
            if touch.state==BEGAN and
    touch.x>buttonSize.x-75 and touch.x<buttonSize.x+75 and touch.y>buttonSize.y-50 and touch.y<buttonSize.y+50         and #deck>0 and not done then
            pick()
            end
    
        if touch.state==BEGAN then
            if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
                showScore=true
            end
            pos=0
            for a,b in pairs(show) do
    
                if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                            touch.y<b.y2+40 then
                    b.s=1   -- scaler
                    pos=a
                    for c,d in pairs(gameslot) do
                        if b.x2==d.x and b.y2==d.y then
                            d.z=0
                        end
                    end
                end
            end
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            show[pos].x2=show[pos].x2+touch.deltaX
            show[pos].y2=show[pos].y2+touch.deltaY
        end
    
        if touch.state==ENDED then  
            --score=0
            cc=0
            showScore=false
            for a,b in pairs(gameslot) do
                if show[pos].x2>b.x-55 and show[pos].x2<b.x+55 and 
                        show[pos].y2>b.y-55 and show[pos].y2<b.y+55 and
                        b.z==0 then
                    show[pos].x2=b.x
                    show[pos].y2=b.y
                    b.z=1
                    for c,d in pairs(show) do
                        if d.x1~=d.x2 and d.y1~=d.y2 then
                            score=score+d.v
                            cc=cc+1
                        end
                    end            
                    return
                end            
            end
            show[pos].x2=show[pos].x1
            show[pos].y2=show[pos].y1
            show[pos].s=.5
        end
    end
    
    function timer(max)
        if st==nil then
            return
        end
        if #deck>0 then
            curr=s:gettime()-st
            text(string.format("Time remaining  %4.1f",max-curr),WIDTH/2,HEIGHT/2)
            if curr>max then
                pick()
            end
        end
    end
    
    function scoring()
        text(score,150,500)
    end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Are you talking about the sprite information or do you want to move the sprites to a different position on the screen.

  • Posts: 124

    @dave1707 the sprites and the sprites info into a different table but just the sprites in the 1st row.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc You can have something like this when you press the Ready button. Any sprite that’s in the top row ( has a y2 value of 290 ), move that table entry into another table. Just make sure you set something so you’ll only do it once or you’ll move the info again.

    This code goes in the touched function for the ready button.

            for z=1,#show do
                if show[z].y2==290 then
                    table.insert(tabA,show[z])
                end
            end
    
  • Posts: 124

    @dave1707 Thanks!

  • Posts: 124

    @dave1707 how do I get the score value from the 1st row to appear 1 at time, left to right instead of the total amount when the ready button is pressed.

    Like if the values in the 1st row were 1,3,4,5
    Then the score text would display 1 then 4 then 8 then 13 (because it’s adding each slot)

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Instead of adding all the values, move each value into a table. Instead of displaying the total, display the sum as you go through the table. Use a counter to delay the adding.

  • Posts: 124

    @dave1707 Thanks, I figured it out but how do I get it to score in order. From left to right.

    function setup()
    
        et=ElapsedTime
        count=0
    
        score=0
        round=0 
        s=require("socket")
    
        readybuttonSize=vec2(500,500)
    
        buttonSize=vec2(500,600)
    
        slot = { vec2(835,335), vec2(835,265), vec2(835,195), vec2(900,335), vec2(900,265), vec2(900, 195) }
    
        gameslot = { vec3(320,290,0), vec3(445,290,0), vec3(570,290,0), vec3(695,290,0), vec3(380,180,0), vec3(505,180,0), vec3(630,180,0) }
    
        deck = {{n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                    v=5,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Character_Boy,
                    v=6,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                    v=8,x1=0,y1=0,x2=0,y2=0,s=.5}, 
                {n=asset.builtin.Planet_Cute.Gem_Green,
                    v=9,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Gem_Orange,
                    v=10,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Gem_Blue,
                    v=2,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Star,
                    v=3,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Tree_Short,
                    v=4,x1=0,y1=0,x2=0,y2=0,s=.5}, 
                {n=asset.builtin.Planet_Cute.Heart,
                    v=5,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Chest_Closed,
                    v=1,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Enemy_Bug,
                    v=7,x1=0,y1=0,x2=0,y2=0,s=.5},
                {n=asset.builtin.Planet_Cute.Ramp_South,
                    v=3,x1=0,y1=0,x2=0,y2=0,s=.5}}
    
                pick()
    
                imageSize = spriteSize(deck[1].n)
    
                tabB={}
    end
    
    function pick()
    
        st=s:gettime()
        show={}
        temp={}
    
        for z=1,6 do    -- temp table of small circles positions
            table.insert(temp,slot[z])        
        end
    
        for z=1,6 do    -- pick random name and random circle
            a=math.random(#deck)
            b=math.random(#temp)
            table.insert(show,{name=deck[a],pos=temp[b]})  -- update show table
            table.remove(deck,a)
            table.remove(temp,b)  
        end
    
        round=round+1
        print("Round -----------  "..round)
    
        for k,v in pairs(show) do
            print(v.name,v.pos)   
        end
    
         for z=1,6 do
            show[z].x1=show[z].pos.x
            show[z].y1=show[z].pos.y
            show[z].x2=show[z].pos.x
            show[z].y2=show[z].pos.y
            show[z].v=show[z].name.v
            show[z].s=show[z].name.s
            show[z].n=show[z].name.n
        end
    end
    
    function draw()   
        background(0)     
    
        text("round 1", 200, 300)
        text("round 2", 250, 200)
    
        text("Tap next to start.",WIDTH/2,HEIGHT-50)
        timer(30)
        pushStyle()
        fontSize(30)
        fill(40,40,50) 
        popStyle()  
        fontSize(30)
        if cc==4 then 
            rectMode(CENTER)
            pushStyle()
            noFill()
            rect(readybuttonSize.x,readybuttonSize.y,100)
            popStyle()
            text("Ready",500,500)
            if sc==true then
                scoring()
            end
        end
    
        sprite(asset.builtin.Cargo_Bot.Next_Button,buttonSize.x,buttonSize.y)   
    
        fill(128)
    
        strokeWidth(5)
    
        for k,v in pairs(slot) do
            ellipse(v.x, v.y, 50)  
        end 
    
        for k,v in pairs(gameslot) do
            ellipse(v.x, v.y, 100)
        end
    
        for z=1,6 do
            pushMatrix()
            zLevel(.3) 
            sprite(show[z].n,show[z].x2,show[z].y2,imageSize*show[z].s)
            popMatrix()    
        end  
    
    end
    
    function touched(touch)    
    
            if touch.state==BEGAN and
    touch.x>readybuttonSize.x-75 and touch.x<readybuttonSize.x+75 and touch.y>readybuttonSize.y-50 and touch.y<readybuttonSize.y+50 and cc==4 then
            sc=true
              end 
    
            if touch.state==BEGAN and
    touch.x>buttonSize.x-75 and touch.x<buttonSize.x+75 and touch.y>buttonSize.y-50 and touch.y<buttonSize.y+50         and #deck>0 and not done then
            pick()
            end
    
        if touch.state==BEGAN then
            if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
                showScore=true
            end
            pos=0
            for a,b in pairs(show) do
    
                if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                            touch.y<b.y2+40 then
                    b.s=1   -- scaler
                    pos=a
                    for c,d in pairs(gameslot) do
                        if b.x2==d.x and b.y2==d.y then
                            d.z=0
                        end
                    end
                end
            end
        end
    
        if pos==0 then 
            return
        end
    
        if touch.state==MOVING then
            show[pos].x2=show[pos].x2+touch.deltaX
            show[pos].y2=show[pos].y2+touch.deltaY
        end
    
        if touch.state==ENDED then  
            --score=0
            cc=0
            showScore=false
            for a,b in pairs(gameslot) do
                if show[pos].x2>b.x-55 and show[pos].x2<b.x+55 and 
                        show[pos].y2>b.y-55 and show[pos].y2<b.y+55 and
                        b.z==0 then
                    show[pos].x2=b.x
                    show[pos].y2=b.y
                    b.z=1
                    for c,d in pairs(show) do
                        if d.x1~=d.x2 and d.y1~=d.y2 then
                            score=score+d.v
                            cc=cc+1
                        end
                    end            
                    return
                end            
            end
            show[pos].x2=show[pos].x1
            show[pos].y2=show[pos].y1
            show[pos].s=.5
        end
    end
    
    function timer(max)
        if st==nil then
            return
        end
        if #deck>0 then
            curr=s:gettime()-st
            text(string.format("Time remaining  %4.1f",max-curr),WIDTH/2,HEIGHT/2)
            if curr>max then
                pick()
            end
        end
    end
    
    function scoring()
    
        if ElapsedTime-et>1 then
            count = count + 1
            et=ElapsedTime
        end 
    
        for z=1,#show do
            if show[z].y2==290 then
                table.insert(tabB,show[z].v)
            end
        end
    
        if count==1 then
              text(tabB[1],150,500)
        end
        if count==2 then
              text(tabB[1]+tabB[2],150,500)
        end
        if count==3 then
              text(tabB[1]+tabB[2]+tabB[3],150,500)
        end
        if count==4 then
              text(tabB[1]+tabB[2]+tabB[3]+tabB[4],150,500)
        end
    
    end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc I don’t know what you want to go from left to right. All you’re doing is displaying the sum of the values one at a time.

  • Posts: 124

    @dave1707 I know but I need the values to display from left to right in order. If the Sprites are positioned chest, bug, heart, star
    And the values of the sprites are chest=1, bug=7, heart=5, star=3
    I need it to show 1 then 8 then 13 then 16 so it’s in order with the positions.
    Right now it’s displaying the values one at a time in a random order.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc To get them in order, you need to check for the x2,y2 value when you put them in tabB. Check x2,y2 for 320,290 then 445,290 then 570,290 then 695,290. You might have to loop through it a couple of times.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Maybe something like this.

        for z=1,#show do
            if show[z].y2==290 then
                if show[z].x2==320 then
                    tabB[1]=show[z].v
                elseif show[z].x2==445 then
                    tabB[2]=show[z].v
                elseif show[z].x2==570 then
                    tabB[3]=show[z].v
                elseif show[z].x2==695 then
                    tabB[4]=show[z].v
                end
            end
        end
    
Sign In or Register to comment.