Discord
Login
Community
DARK THEME

Scoring system issues

I was wanting to add a timer and a score onto my project and display them in opposite sides of the screen. They are not working how I want them to however. For the score I want it to add one point every time you touch an enemy. For that I've created a score variable and then added a distance check in the init function to see how close the player is to the enemy.

if distance(player.x, player.y, enemy.x, enemy.y) < 10 then score += 1 end

the issue is that this doesn't seem to actually change the score variable.. My full code is below, but if anyone has any suggestions it would be appreciated.

init = function()

score =0 timer = 10

player = object end player.x = 0 player.y = 0 player.speed = 2

enemy = object end enemy.x = randRange (-160,160) enemy.y = 100 enemy.speed = 1.5 end

update = function()

if keyboard.W then player.y += player.speed end

if keyboard.S then player.y -= player.speed end

if keyboard.A then player.x -= player.speed end

if keyboard.D then player.x += player.speed end

enemy.y -= enemy.speed if enemy.y < -110 then enemy.y = 110 end

if timer > 0 then timer -= 1/60 else if timer < 0 then timer = 10 end end

if distance(player.x, player.y, enemy.x, enemy.y) < 10 then score += 1 end

if distance(player.x, player.y, enemy.x, enemy.y) < 10 then init () end

end

draw = function() screen.clear() screen.setFont("RetroGaming") screen.drawSprite("player", player.x, player.y, 30, 30) screen.drawSprite("enemy",enemy.x, enemy.y, 30, 30) screen.drawText("Time: " + timer, 200, 80, 15, "rgb(250,100, 200)") screen.drawText("Score: " + score,-200, 80, 15, "rgb(250, 100, 200)")

end

I've actually just figured this out myself, I've removed the init() function which was there to respawn the enemy when they left the screen and replaced it by simply moving the enemy back to the top, outside the screen frame. This allowed me then to add 1 to the score variable. See below.

if distance(player.x, player.y, enemy.x, enemy.y) < 10 then enemy.y = 110 enemy.x = randRange (-160,160) score += 1 end

Nice job! :)

As a small thing for next time, use triple backquotes around code to make it more readable. Like this but with three backquotes:
''' if distance(player.x, player.y, enemy.x, enemy.y) < 10 then enemy.y = 110 enemy.x = randRange (-160,160) score += 1 end '''

And once I use triple backquotes, this is the result: if distance(player.x, player.y, enemy.x, enemy.y) < 10 then enemy.y = 110 enemy.x = randRange (-160,160) score += 1 end

You can see @gilles markup post for extra info!

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community