Howdy, Stranger!

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

Help with making sprites appear in random places

edited June 2014 in Questions Posts: 3

Hi guys,

I'm attempting to make a little game where you have a character, which is a sprite, and by using the accelerometer you tilt the iPad to collect little stars that appear, which will also be sprites. Once you've touched the star it disappears.

There are 5 different coloured stars that I want to appear in random places, when you collect them you get points and more start appearing.

I think I need to use a table to hold the different stars and have them appear on the screen, but I can't find any really easy to understand tutorials or examples on how to use tables in this way.

Could any of you link me to a really good tutorial, or quickly write up an example table that does this?

Please say if you need any more information, I'd post all my current code but I don't know if it would really be relevant.

Thanks

Comments

  • dave1707dave1707 Mod
    edited June 2014 Posts: 7,605

    @joshlong93 This help.

    EDIT: Code modified from the original.


    displayMode(FULLSCREEN) function setup()     tab={}     for z=1,5 do         table.insert(tab,vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50)))     end     sp={"Planet Cute:Character Boy",         "Planet Cute:Character Cat Girl",         "Planet Cute:Character Horn Girl",         "Planet Cute:Character Pink Girl",         "Planet Cute:Character Princess Girl"} end function draw()     background(40,40,50)     for a,b in pairs(tab) do         sprite(sp[a],b.x,b.y)     end end
  • dave1707dave1707 Mod
    Posts: 7,605

    @joshlong93 See this ling to a game I made. It sounds similar to what you're doing. Maybe it will help.

    http://codea.io/talk/discussion/4839/starter-game#Item_9
    
  • edited June 2014 Posts: 3

    Thanks @dave1707 , I think I get most of that.
    What do the z, a and b stand for? Also, how would I make each of the 5 stars a different sprite?

    Thanks, for the quick answer!

  • dave1707dave1707 Mod
    Posts: 7,605

    @joshlong93 The z,a,b are just variables that I use. z is a variable in the "for" loop that goes from 1 to 5. The a and b are variables in another "for" loop. The "a" is a counter that goes from 1 to the number of entries in the table "tab". "b" is the values in the table. Since I created the entries with a vec2, that means there are 2 values per entry. The way to access them is with b.x and b.y . You could put the different sprites in a table and use the "a" variable to access that table. I'll give you an example soon.

  • dave1707dave1707 Mod
    Posts: 7,605

    @joshlong93 I modified the original code to show 5 different sprites.

  • that's been really helpful thank you @dave1707! If I could just ask one more question, how would I randomly make more than one of a particular star show up?

  • dave1707dave1707 Mod
    Posts: 7,605

    @joshlong93 I'm not sure if this is what you're after. I show 4 sprites and the 5th one will be a duplicate of one of the 4.


    displayMode(FULLSCREEN) function setup() tab={} for z=1,5 do table.insert(tab,vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50))) end sp={"Planet Cute:Character Boy", "Planet Cute:Character Cat Girl", "Planet Cute:Character Horn Girl", "Planet Cute:Character Pink Girl"} r=math.random(4) end function draw() background(40,40,50) for a,b in pairs(tab) do if a==5 then sprite(sp[r],b.x,b.y) else sprite(sp[a],b.x,b.y) end end end
  • Posts: 425

    This code allows for didpfferent types of stars with different colors, values and sizes etc.

    -- Game
    displayMode(OVERLAY)
    supportedOrientations(CurrentOrientation)
    function setup()
        player={x=WIDTH/2,y=HEIGHT/2,radius=50,speed=10}
        stars={}
        starTypes={{value=1,col=color(255),radius=20},{value=2,col=color(255,100,0),radius=30}}
        spawnStar()
        score=0
    end
    
    function draw()
        background(0)
        player.x = player.x + Gravity.x*player.speed--move the player in both axis
        player.y = player.y + Gravity.y*player.speed
    
        for i=#stars,1,-1 do
            local playerPos=vec2(player.x,player.y)
            local starPos=vec2(stars[i].x,stars[i].y)
            local distance=playerPos:dist(starPos)--distance between star and player
    
            if distance<player.radius+starTypes[stars[i].Type].radius then--if the distance between player and star is less than the sum if their radii then they have collided
                score = score + starTypes[stars[i].Type].value--update cpscore based on type of star
                print("you've got "..score.." points\nWell done")
                table.remove(stars,i)--delete this star
                spawnStar(1)--spawn 1 new star
                --make a sound ir something here:
    
            end
        end
    
        noTint()
        spriteMode(RADIUS)
        sprite("Planet Cute:Character Boy",player.x,player.y,player.radius)--draw the player
    
        for k,s in ipairs(stars) do
            tint(starTypes[s.Type].col)
            sprite("Planet Cute:Star",s.x,s.y,starTypes[s.Type].radius)--draw the star
        end
    end
    
    function spawnStar(n)
        local num
        if n then num=n else num=1 end
        for i=1,num do
            local star={x=math.random(50,WIDTH-50),y=math.random(50,HEIGHT-50),num=#stars+1,
                        Type=math.random(1,2)}
            table.insert(stars,star)
        end
    end
    
  • edited June 2014 Posts: 1,976

    @Coder

    local num
    if n then
        num = n
    else
        num = 1
    end
    

    can be compacted to

    local num = n or 1
    

    (which could also be merged into the for loop)

    for i = 1, n or 1 do
        ...
    end
    
  • edited June 2014 Posts: 425

    @SkyTheCoder yeah that makes the code shorter

  • edited June 2014 Posts: 223
    The user and all related content has been deleted.
Sign In or Register to comment.