Trying to add a score for cloud jumping game.

im trying to make a little game for the first time and I’m really trying to add points to the cloud jumping game. Here’s the code I have. I’m able to put the score text in the game, but it doesn’t stay in the top corner (it stays in the middle of the screen and doesn’t move up when the player does). It also doesn’t change from 0.

Here’s the Main code



-- 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()

    counter = Girl()
    
    girl = Girl()
    girl.position = vec2(0, 0)
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:Plain 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))
    
        end
        
    -- Draw scores
    score = counter.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

Here’s the Girl code.

Girl = class()

function Girl:init()
    -- you can accept and set parameters here
    self.position = vec2(0,0)
    self.velocity = vec2(0,0)
    self.score = 0
end

function Girl:jump(power)
    sound(SOUND_JUMP, 40453)
    self.velocity = self.velocity + vec2(0,power)
    self.score = self.score + 1
    
end

function Girl:computeVelocity()
    gravity = vec2(Gravity.x, math.min(Gravity.y,-1)):normalize()
    gravity = gravity * 15
    friction = math.min(self.position.y, 1)
    return self.velocity + gravity * friction
end

function Girl:update()
    self.position = self.position + self:computeVelocity()
    
    -- Clamp y position
    -- (so you don't go through ground)
    self.position.y = math.max(self.position.y,0)
    
    -- Clamp x position
    self.position.x = math.max(self.position.x,-WIDTH/2)
    self.position.x = math.min(self.position.x,WIDTH/2)
    
    -- Dampen velocity
    self.velocity = self.velocity * 0.98
end

function Girl:isFalling()
    return self:computeVelocity().y < 0
end

function Girl:draw()
    self:update()
    
    pushMatrix()
    
    translate(self.position.x, self.position.y)
    
    sprite("Documents:obamahead", 0, 0)
    
    popMatrix()
end

Remove the whole counter variable and use girl.score instead. You create two instances of the girl, only update the original, and try to get the value from the one that isn’t updating.

@shisma - The problem with your text position is that the “camera” is moving up and down to keep the jumper in the middle, so you need to move the text too.

We know the height of the camera from this

camPos = vec2(WIDTH/2, 
                  math.min(HEIGHT/2 - girl.position.y, 140))

so the middle of the screen must be at camPos.y

You want to put the text at y= HEIGHT - 50

So put it at y= camPos.y + HEIGHT/2 - 50 instead

The score is 0 because

  1. You define counter = Girl() and never use it after that. You use girl = Girl() for the actual player

  2. You set score=counter.score, so it will always be 0 because you aren’t using counter. It should be score=girl.score instead

The height problem is a little tricky (because very few programs move the camera around), but with a little practice, you should be able to find errors like the score yourself. It’s just a case of looking carefully at all the code that affects the thing that is going wrong.

@Ignatz To fix the text position, you could also just call resetMatrix() before you draw the text.