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