How would I add random coins to the cloud jump game?

i want to add coins on random clouds in the cloud jump game. What would be the best way to do this? Also I want them to give points, so could I just add a collide function to the random coin generator and just say score = score + 1 when they collide?

Also, I want to add random moving obstacles to the game that you have to dodge. I have no clue how to make that happen. I appreciate any help I can get with this. I’m learning so much here already. :slight_smile:

If you want to add a random score every time you jump on a cloud, try using math.random(0, 2), it’ll make it so whenever you jump on a cloud you will get a bonus of 0, 1, or 2. You can also use math.random for making moving obstacles.

I want it to be more like a goal. Like being able to see the coin and trying to get it.

@shisma Then, in the Cloud class, just add a new variable:

self.score = math.random(0, 10)

In the Cloud’s draw, something like this:

if self.score >= 5 then
    sprite("Platformer Art:Coin", 0, 100)
end

In the CloudLevels, change its isColliding function:

function CloudLevels:isColliding(pos)
    for i,v in ipairs(self.clouds) do
        if v:isColliding(pos) then
            return v -- changed from true to v
        end
    end
    
    return false
end

In the if statement in draw (looks like this:

if clouds:isColliding(girl.position) and
       girl:isFalling() then

)

Just do something like

score = score + clouds:isColliding(girl.position).score

I see. So it basically gives every cloud a 50% chance (or how ever many out of 10 I choose) to spawn a coin, then give’s points when I collide with the coin.

@shisma Actually, it would give it a score from 0 to 10, and if that score is over 5, it would have a coin above it (but it’ll still give you that same score from 0 to 10 when you land on it). You could tweak the if self.score >=5 then and the self.score = math.random(0, 10) to make it have a 50% chance of spawning a coin, such as changing the self.score = ... part to:

self.score = math.random(0, 1) * 10

So the clouds with coins above them give you 10 points, other clouds give you nothing, and there is a 50% chance for each cloud to have a coin. Or maybe you could do something like this:

self.score = math.random(0, 1) * 9 + 1

which will be the same as above, except clouds without coins give you +1 score.

Hi, thanks for your response. I understand most of the code and the reason you chose it, but I don’t understand why I’m getting this error:

http://imgur.com/R4AntBR

You normally get that error when you try to do something with a nil variable. Look thru your code and make sure that ‘score’ is initialized to some value or you’re not setting it to nil someplace.

Okay here’s my classes

Cloud = class()


function Cloud:init()
    -- you can accept and set parameters here
    self.shapes = {}
    self.position = vec2(0,0)
    self.score = math.random(0, 1) * 9 + 1

    
    -- Generate random cloud
    numCircles = math.random(4, 5)
    spacing = 30
    
    for i = 1,numCircles do
        x = i * spacing - ((numCircles/2)*spacing)
        y = (math.random() - 0.5) * 30
        rad = math.random(spacing, 2*spacing)     
        table.insert(self.shapes, {x=x, y=y, r=rad})
    end
    
    self.width = numCircles * spacing + spacing
end

function Cloud:isColliding(pos)
    startp = self.position.x - self.width/2
    endp = self.position.x + self.width/2
    
    if pos.x < endp and pos.x > startp and
       pos.y < (self.position.y + 30) and
       pos.y > (self.position.y + 10) then
        return true
    end
    
    return false
end

function Cloud:draw()
    pushStyle()
    pushMatrix()
    
    translate(self.position.x, self.position.y)
    
    noStroke()
    ellipseMode(RADIUS)
    fill(167, 190, 221, 255)
    
    for i,s in ipairs(self.shapes) do
        ellipse(s.x, s.y - 5, s.r)
    end
    
    fill(255, 255, 255, 255)
    
    for i,s in ipairs(self.shapes) do
        ellipse(s.x, s.y + 5, s.r)
    end
    
    popMatrix()
    popStyle()
    
    if self.score >= 5 then sprite("Platformer Art:Coin", 0, 100) end
    

end

And Main



-- Use this function to perform your initial setup
function setup()
    print("TAP to JUMP")
    print("Tilt your device to land on the clouds!")
    print("Press RESET to restart the level")
    
    clouds = CloudLevels()
    
    girl = Girl()
    girl.position = vec2(0, 0)
    score = Cloud().score
end

-- This function gets called once every frame
function draw()
    background(47, 145, 216, 255)
    
    -- Center the camera on the girl character
    camPos = vec2(WIDTH/2, 
                  math.min(HEIGHT/2 - girl.position.y, 140))
    translate(camPos.x,camPos.y)
    
    -- Draw ground
    for i = 1,8 do
        sprite("Planet Cute:Grass Block", -WIDTH/2 -70 + 101*i, -60)
    end
    
    clouds:update(camPos)
    
    clouds:draw()
    girl:draw()
    
    if clouds:isColliding(girl.position) and
       girl:isFalling() then
        girl:jump(math.random(30,60))
        score = score + clouds:isColliding(girl.position).score
    end
    -- Draw scores
    score = score.score * 1
    fill(255)
    font("Futura-CondensedExtraBold")
    fontSize(30)
    textMode(CORNER)
    text("Score: "..score, 10, HEIGHT - 50)
end

function touched(touch)
    if touch.tapCount == 1 and 
       girl.position.y == 0 then
        girl:jump(40)
    end
end

The score just flashes back and forth from 0 to 1. No errors are given, however it doesn’t spawn coins.

Try changing the line in setup from

score = Cloud().score

to

score = 0

and in draw, remove the line

score = score.score * 1

Okay, now it just has a coin floating in the bottom of the world. Can’t bE picked up or anything, and it isn’t on the clouds. However, some clouds give an extra point, so I just need those clouds to have a coin.

http://imgur.com/kDYaOEC

@shisma Put the line

if self.score >= 5 then sprite()...

ABOVE the code

popMatrix()
popStyle()

So would I be able to use a collide function to make the coin disappear? I’ll try it out. Also, does this support animated gif images? I would love to have spinning coins.

@shisma To make the coins disappear when you jump on them, add this line after the score = score + clouds:isColliding...:

clouds:isColliding(girl.position).score = 0

And sorry, but Codea doesn’t support GIFs (@Simeon poke poke). You can use sprite sheets though, but they’re a bit complicated to code.

Okay last question I swear lol, what if I wanted to make the coin disappear when someone just passes over it, not necessarily jumping ON the cloud.

@shisma Then you would have to make another collision detection function with a bigger height on its collision box, and use that one when testing for collisions too.

Okay I’ll try that

How would I specify another collide just for the coin and not the cloud that it’s on.

@shisma Just start by copying the cloud’s collision code as a different function of the cloud, and tweak the Y values so it detects a little higher up.

@SkyTheCoder something like this?

Cloud = class()


function Cloud:init()
    -- you can accept and set parameters here
    self.shapes = {}
    self.position = vec2(0,0)
    self.score = math.random(0, 1) + 1
    

    
    -- Generate random cloud
    numCircles = math.random(4, 5)
    spacing = 30
    
    for i = 1,numCircles do
        x = i * spacing - ((numCircles/2)*spacing)
        y = (math.random() - 0.5) * 30
        rad = math.random(spacing, 2*spacing)     
        table.insert(self.shapes, {x=x, y=y, r=rad})
    end
    
    self.width = numCircles * spacing + spacing
end

function Cloud:isColliding(pos)
    startp = self.position.x - self.width/2
    endp = self.position.x + self.width/2
    
    if pos.x < endp and pos.x > startp and
       pos.y < (self.position.y + 30) and
       pos.y > (self.position.y + 10) then
        return true
    end
    
    return false
end

function Cloud:isColliding(pos)
    startp = self.position.x - self.width/2
    endp = self.position.x + self.width/2
    
    if pos.x < endp and pos.x > startp and
       pos.y < (self.position.y + 35) and
       pos.y > (self.position.y + 15) then

        return true
    end
    
    return false
end

function Cloud:draw()
    pushStyle()
    pushMatrix()
    
    translate(self.position.x, self.position.y)
    
    
    
    sprite("Platformer Art:Crate")
    
    if self.score >= 2 then sprite("Platformer Art:Coin", 0, 100) end

    

    popMatrix()
    popStyle()
    

end