TGC Codebase Backup



Digital Clock by Trek

26th May 2005 12:49
Summary

A 100% accurate minutes:seconds formatted digital clock



Description

This is a well remarked program that demonstrates how to put a simple digital clock in your games that require exact timing. Using your computer's timer, it steadily increases the seconds at the correct intervals no matter what the user's processor speed is (unlike variable controlled clocks). Not only do the remarks explain what the program technically does (e.g. this divides this variable by 2) but it also explains why it does what it does. It also implements a pause system that pauses the time and returns to the exact time as it was when paused. This may even be helpful for people just looking for a good pause system since it has a simple method for preventing it from instantly switching back and forth between paused and unpaused.

I'm currently using a method similar to this one for my upcoming release, Ice Hockey II. To track this program's progress and download my other games, check out my website at http://roborangers.home.comcast.net/Trek.html

Thanks for stopping by.

Trek Manager D3



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `.................................................
`.                                               .
`.                 Digital Clock                 .
`.                      by                       .
`.                 Trek Software                 .
`.             Programming done by D3            .
`.                                               .
`. http://roborangers.home.comcast.net/Trek.html .
`.                                               .
`.................................................  

` Set the screen to a higher resolution just for fun

set display mode 800,600,16 

` Enable manual syncing and limit it to 40 frames per second

sync on 
sync rate 40

` Hide the mouse and keep the camera from automatically aligning with
` the newest object.

hide mouse
autocam off

` Set the variables to 0 so that if you run it again it will
` reset the time.

CurrentTime=0
Minutes=0
Seconds=0

NumberOfMinutes=1

` Initialize a dimmension for the minutes. If you want to have it time more
` than 100 minutes than just increase the dimmension.

DIM Minute(100)

` Set the initial time to the computer's timer.

InitialTime=timer()

` Make a grey/blue background 

Make Object Plain 1,10,10
Color Object 1,rgb(150,150,200)
position object 1,0,0,5

` Put the camera at 0,0,0 and point it at the background

Position Camera 0,0,0
Point Camera 0,0,5

` Set the text size and font

Set text size 25
Set text font "Edda"

` Make a color variable and set the Ink to it so that the text will be black

black=rgb(20,20,20)

Ink black,black

` Start the loop

Do

` Give text instructions for pausing the loop

text 300,5,"Press 'P' to Pause"

` Check to see if the small 'p' or capital 'P' are being pressed

if inkey$()="P" or inkey$()="p" then gosub Pause

` Every 1000 units on the computer's timer is 1 second. Therefore, substract
` the Initial time from the computer's time and divide it by 1000. 
` For example, if the computer's time was 53,000 when we started the loop,
` and it currently is 65,000, then we would subtract 53,000 from 65,000 
` getting 12,000 and then divide it by 1000 getting 12. This means that 
` 12 seconds have gone by.

CurrentTime=(timer()-InitialTime)/1000

` The NumberOfMinutes variable starts out as 1. Therefore, when we multiply
` it by 60 we get 60. Therefore, when we have 60 seconds, we set the minutes
` variable to 1 and add 1 to the NumberOfMinutes variable. Therefore, it 
` waits until there are 120 seconds (60 * 2) and then adds 1 to the minutes
` again.

if CurrentTime=NumberOfMinutes*60 and Minute(NumberOfMinutes)=0 or CurrentTime=(NumberOfMinutes*60)+1 and Minute(NumberOfMinutes)=0 

	Minutes=Minutes+1 
	Minute(NumberOfMinutes)=1 
	NumberOfMinutes=NumberOfMinutes+1

endif

` Since digital clocks go up to 60 seconds and then add 1 to the minutes 
` resetting the seconds to 0, we must subtract the minutes times 60 from
` the total number of seconds to get the actual seconds.

Seconds=CurrentTime-(Minutes*60)

` This puts a 0 before the seconds if they are only one digit large

If Seconds<10 then Seconds$="0" + STR$(Seconds)
If Seconds>=10 then Seconds$=STR$(Seconds)

` This does the same thing for the minutes.

if Minutes<10 then Minutes$="0" + STR$(Minutes)
if Minutes>10 then Minutes$=STR$(Minutes)

` This constructs the string for displaying the time in 
` Minutes:Seconds format.

CurrentTime$=Minutes$ + " : " + Seconds$

` Displays the time.

text 360,290,CurrentTime$

` Refreshes the screen.

sync

` Loops back to the [Do] command

Loop

` Pause subroutine intialized

Pause:

` Sets the text size to a little larger

set text size 40

` Checks to see what the time is when paused

IPauseTime=timer()

` Spends a little time not accepting input so that it doesn't instantly
` exit the pause loop

for n=1 to 30
	text 320,270,"Paused"
	sync
next n

` Prints "Pause" on the screen while waiting for keyboard input

While Inkey$()=""
	text 320,270,"Paused"
	sync
EndWhile

` Waits a while so that the screen doesn't instantly switch back to pause

wait 200

` Sets the text size a little smaller

set text size 25

` Determines how much time was spent paused and adds that on to the 
` InitialTime variable so that it will start right where it left off

InitialTime=InitialTime+(timer()-IPauseTime)

` Returns to the main loop

return