Arcade style score function

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
sheepmanbuddy
Prole
Posts: 7
Joined: Sun Sep 08, 2013 5:31 pm

Arcade style score function

Post by sheepmanbuddy »

Hello! I have here a function that formats scores from this:
3459
to this:
0003459



To use just add this code:
whateveriwanttocallthefunction = require("score")

The function has 2 parameters the total score and how many digits are in the final string.

EXAMPLE:
score(1456,6) would return the string "001456"

Edit:
I modified this so that it uses string.format. 3 params now! The first 2 are the same as before, the last one is optional and is maxscore.
2 return values the string of score, and the score itself.

Happy coding
-Sheepman :crazy:
Last edited by sheepmanbuddy on Sun Jan 26, 2014 8:43 pm, edited 1 time in total.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Arcade style score function

Post by DaedalusYoung »

You can do this with string.sub("00000" .. score, -6), although of course you do need to keep score under maxscore.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Arcade style score function

Post by bartbes »

How about string.format("%07d", 1456.6)?
User avatar
sheepmanbuddy
Prole
Posts: 7
Joined: Sun Sep 08, 2013 5:31 pm

Re: Arcade style score function

Post by sheepmanbuddy »

I didn't know you could do that with string.format! I feel stupid... :crazy:

Edit: Next time I need to do something similar to this, I will just play around with string.format. :megagrin:
Last edited by sheepmanbuddy on Sat Jan 25, 2014 4:09 pm, edited 1 time in total.
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Arcade style score function

Post by Jeeper »

sheepmanbuddy wrote:I didn't know you could do that with string.format! I feel stupid... :crazy:
Don't feel stupid, you can do things in many ways and you might be able to use this method for something else :).
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Arcade style score function

Post by Jasoco »

How about if you want to put commas in?

Code: Select all

function formatNumber(number)
	return (string.format("%d", number):reverse():gsub( "(%d%d%d)" , "%1," ):reverse():gsub("^(-?),","%1"))
end
Returns a number formatted like 1,000,000.

I made one that formats a number to hh:mm:ss but I'm not sure if it's optimal enough:

Code: Select all

function formatTime(t, m)
	local r = math.floor(math.mod(t/60/60,60)) .. ":" .. string.sub("0" .. math.floor(math.mod(t/60,60)),-2) .. ":" .. string.sub("0" .. math.floor(math.mod(t,60)),-2)
	if m then r = r .. (t - math.floor(t)) end
	return r
end
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Arcade style score function

Post by DaedalusYoung »

I'm sure you can do that with os.date or os.time too :)
katekerillf
Prole
Posts: 4
Joined: Sat Jan 25, 2014 9:02 pm

Re: Arcade style score function

Post by katekerillf »

Looks like a good find. I will download this to try.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests