Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been building a game using Visual Studio 2012 ultimate. The aim of the game is to click as many coins as you can which fly from the bottom of the screen to the top. I want to have a timer displayed on the screen so when it reaches zero the game is over.

So far i have achieved in making a countdown timer which counts to zero but then loops back to its starting time and counts down again.


I was wondering how i could stop the looping of this timer, and also when the timer reaches to zero i would like to have a game over screen pop up.

If anyone has any code that achieves a count down timer with game over that would be awesome.

What I have tried:

-This is the HTML-

<p id="demo"></p>

-This is the CSS-

#demo {
    font-family: 'Arizonia-Regular';
    position: absolute;
    font-size: 35px;
    color: #ff0000;
    bottom: 50px;
    right: 70px;
    background-color: black;
    width: 135px;
    border: 4px solid red;
    border-left-color: black;
    padding: 5px;
    margin: 5px;
}


-This is the script-

// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2018 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function () {

// Get todays date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = countDownDate - now;

// Time calculations for minutes and seconds
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = minutes + "m " + seconds + "s ";

// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
Posted
Comments
David Crow 18-Sep-17 10:24am    
Just curious, but where's the Android part of this? You've shown a style sheet and some JavaScript, but nothing that looks like it will run on an Android device.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900