Sample Code
The following code creates a windowed application that shows the current time in binary and decimal format. This code is written in Blitz Basic, but will compile and run in both Blitz3D and BlitzPlus. See below for the same example written in BlitzMax.
AppTitle "Binary Clock" Graphics 150,80,16,3 ;Copy, modify and redistribute this source with no limit ;##################################################### ; MAIN LOOP ;##################################################### ;create a timer that means the main loop will be ;executed twice a second secondtimer=CreateTimer(2) Repeat Hour = Left(CurrentTime$,2) Minute = Mid(CurrentTime$,4,2) Second = Right(CurrentTime$,2) If Hour >= 12 Then PM = 1 If Hour > 12 Then Hour = Hour - 12 If Hour = 0 Then Hour = 12 ;should do this otherwise the PM dot will be ;left up once the clock rolls past midnight! Cls Color(0,255,0) ;make the text green for the PM part If PM = 1 Then Text 5,5,"PM" ;set the text colour back to white for the rest Color(255,255,255) For bit=0 To 5 xpos=20*(6-bit) binaryMask=2^bit ;do hours If (bit<4) If (hour And binaryMask) Text xpos,5,"1" Else Text xpos,5,"0" EndIf EndIf ;do the minutes If (minute And binaryMask) Text xpos,25,"1" Else Text xpos,25,"0" EndIf ;do the seconds If (second And binaryMask) Text xpos,45,"1" Else Text xpos,45,"0" EndIf Next ;make the text red for the decimal time Color(255,0,0) Text 5,65,"Decimal: " + CurrentTime$ ;set the text back to white for the rest Color(255,255,255) ;will wait half a second WaitTimer(secondTimer) ForeverBlitzMax version of the above clock:
AppTitle = "Binary Clock" Graphics 145,85 secondtimer = CreateTimer(2) Repeat Hour = CurrentTime.ToInt Minute = CurrentTime.ToInt Second = CurrentTime.ToInt If Hour >= 12 Then PM = 1 If Hour > 12 Then Hour = Hour - 12 If Hour = 0 Then Hour = 12 'should do this otherwise the PM dot will be 'Left up once the clock rolls past midnight! Cls SetColor(0,255,0) 'make the text green For the PM part If PM = 1 Then DrawText "PM",5,5 'set the text colour back To white For the rest SetColor(255,255,255) For bit=0 Until 6 xpos=20*(6-bit) binaryMask=2^bit 'do hours If (bit<4) If (hour & binaryMask) DrawText "1",xpos,5 Else DrawText "0",xpos,5 EndIf EndIf 'do the minutes If (minute & binaryMask) DrawText "1", xpos,25 Else DrawText "0", xpos,25 EndIf 'do the seconds If (second & binaryMask) DrawText "1",xpos,45 Else DrawText "0",xpos,45 EndIf Next 'make the text red For the decimal time SetColor(255,0,0) DrawText "Decimal: " + CurrentTime,5,65 'set the text back To white For the rest SetColor(255,255,255) Flip 'will wait half a second WaitTimer(secondTimer) If KeyHit(KEY_ESCAPE) Then Exit ForeverRead more about this topic: Blitz BASIC
Famous quotes containing the words sample and/or code:
“The present war having so long cut off all communication with Great-Britain, we are not able to make a fair estimate of the state of science in that country. The spirit in which she wages war is the only sample before our eyes, and that does not seem the legitimate offspring either of science or of civilization.”
—Thomas Jefferson (17431826)
“Hollywood keeps before its child audiences a string of glorified young heroes, everyone of whom is an unhesitating and violent Anarchist. His one answer to everything that annoys him or disparages his country or his parents or his young lady or his personal code of manly conduct is to give the offender a sock in the jaw.... My observation leads me to believe that it is not the virtuous people who are good at socking jaws.”
—George Bernard Shaw (18561950)