Search code examples
actionscript-3flashscoring

How to give a score based on time?


In the game I am making, I need to make a scoring system based on time.

If you get a match in the first second of gameplay. That match is worth more points, than if I get a match on the last second of gameplay.

So in other words, the faster you do the level, the more points you get. Does anyone know how to achieve this? I know the total time for each level.

The game is real time so it needs to be seen to show high scores early in the game with a decreasing score as time goes on.

Thanks.


Solution

  • Define a maximum time per level then do some math tricks:

    score = Math.max(0, levelMaxTime - timeSpent) * levelScore;
    

    Example:

    timeSpent = 12; // The player completed the level in 12 seconds
    levelMaxTime = 20; // The player has to completed the level within 30 seconds
    levelScore = 50; // The player will be awarded of 50 points per remaining second
    
    // Compute the final score
    score = Math.max(0, levelMaxTime - timeSpent) * levelScore; // 400 points