Blitz BASIC - Sample Code

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) Forever

BlitzMax 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 Forever

Read more about this topic:  Blitz BASIC

Famous quotes containing the words sample and/or code:

    As a rule they will refuse even to sample a foreign dish, they regard such things as garlic and olive oil with disgust, life is unliveable to them unless they have tea and puddings.
    George Orwell (1903–1950)

    Wise Draco comes, deep in the midnight roll
    Of black artillery; he comes, though late;
    In code corroborating Calvin’s creed
    And cynic tyrannies of honest kings;
    He comes, nor parlies; and the Town, redeemed,
    Gives thanks devout; nor, being thankful, heeds
    The grimy slur on the Republic’s faith implied,
    Which holds that Man is naturally good,
    And—more—is Nature’s Roman, never to be
    scourged.
    Herman Melville (1819–1891)