BAIT Community Project (thread 2)

Miscellaneous Forums/Blitz Showcase/BAIT Community Project (thread 2)

The all new and improved Clock.bb:

Note that I'm still a little unsure as to how you use the drawtext function, so I'ld be most grateful if you could make the necessary amendments. Also, I obviously didn't indent the code as it's displayed here, it goes funny when I copy and paste from the IDE to a posting window and I don't know what to do about it.

; Data declarations

Const ClockX = 350
Const ClockY = 30
Const ClockTicksInASecond  = 50

Global ClockMinutesLeft    = 2
Global ClockSecondsLeft    = 0
Global ClockLastTimeChange = 0
Global ClockTicksElapsed   = 0

Function ClockTimeUpdate()

	; Purpose:		Counts the clock down to 0
	; Parameters:	None
	; Return:		None

	ClockTicksElapsed = ClockTicksElapsed + 1

	If ClockLastTimeChange + ClockTicksInASecond < ClockTicksElapsed
	
		If ClockSecondsLeft > 0
		
			ClockSecondsLeft = ClockSecondsLeft - 1
		
		Else
		
			If ClockMinutesLeft > 0
			
				ClockMinutesLeft = ClockMinutesLeft - 1
				ClockSecondsLeft = 59
			
			EndIf
		
		EndIf
		
		ClockLastTimeChange = ClockTicksElapsed
	
	EndIf

End Function

Function ClockTimeDisplay()

	; Purpose:		Displays the clock at the set position
	; Parameters:	None
	; Return:		None

	If ClockSecondsLeft >= 10
	
		Text ClockX, ClockY, ClockMinutesLeft + ":"  + ClockSecondsLeft 
	
	Else
	
		Text ClockX, ClockY, ClockMinutesLeft + ":0" + ClockSecondsLeft
	
	EndIf

End Function

; Test Harness

SetBuffer BackBuffer()

While Not KeyHit(1)

	Cls
	
		ClockTimeUpdate()
		ClockTimeDisplay()

	Flip

Wend


The league table code will soon follow...

Edit: And here it is:

; Data Declarations

Const LTableTopTextY   			= 100
Const LTablePositionX  			= 100
Const LTableNameX      			= 250
Const LTablePointsX	 			= 450
Const LTableTextHeight 			= 30
Const LTableBreakBetweenLines   = 20
Const LTableNumberOfBots        = 6

Dim LTableNames$ (LTableNumberOfBots - 1)
Dim LTablePoints (LTableNumberOfBots - 1)

Function LTableArraysInitialise()

	; Purpose:		Reads in the names and the points of the
	;				competitors into the corresponding arrays
	; Parameters:	Bot names and scores given in the data block
	; Return:		None

	Local index

	Restore LTableRobotData
	
	For index = 0 To LTableNumberOfBots - 1
		
		Read LTableNames  (index)
		Read LTablePoints (index)
	
	Next

End Function

Function LTableDisplay()

	; Purpose:		Displays the league table in descending order of points
	; Parameters:	None
	; Return:		None
	; Notes: 		Tied scores are not treated differently 

	Local textY
	Local index 

	Text LTablePositionX, LTableTopTextY, "POSITION"
	Text LTableNameX,     LTableTopTextY, "NAME"
	Text LTablePointsX,   LTableTopTextY, "POINTS"
	
	For index = 0 To LTableNumberOfBots - 1
	
		textY = LTableTopTextY + (index + 1) * (LTableTextHeight + LTableBreakBetweenLines)
		
		Text LTablePositionX, textY, (index + 1) + LTablePositionSuffixReturn (index + 1)
		Text LTableNameX,     textY, LTableNames  (index)
		Text LTablePointsX,   textY, LTablePoints (index)
		
	Next 	 

End Function

Function LTablePositionSuffixReturn$( number )

	; Purpose:		Returns the suffix for a given position number
	; Parameters:	A position number ( positive integer )
	; Return:		The suffix for the given position number

	Select number
	
		Case 1  Return "st"
		Case 2  Return "nd"
		Case 3  Return "rd"
		Default Return "th"
	
	End Select	

End Function

Function LTableSort()

	; Purpose:		Sorts the competitors scores into descending order of points
	; Parameters:	None
	; Return:		None
	; Notes:		Uses a bubble sort to achieve this
	
	Local tempName$
	Local tempPoints
	Local passNumber
	Local pairNumber

	For passNumber = 0 To LTableNumberOfBots - 2

		For pairNumber = 0 To LTableNumberOfBots - 2

			If LTablePoints (pairNumber) < LTablePoints (pairNumber + 1)
			
				tempName   = LTableNames  (pairNumber + 1)
				tempPoints = LTablePoints (pairNumber + 1)
				
				LTableNames  (pairNumber + 1) = LTableNames  (pairNumber)
				LTablePoints (pairNumber + 1) = LTablePoints (pairNumber)
				
				LTableNames  (pairNumber) = tempName
				LTablePoints (pairNumber) = tempPoints
			
			EndIf

		Next
	
	Next

End Function

; Test Harness

LTableArraysInitialise()
LTableSort()

Graphics 800,600

LTableDisplay()

WaitKey()

End

.LTableRobotData
Data "BotA", 200
Data "BotB", 400
Data "BotC", 100
Data "BotD", 500
Data "BotE", 300
Data "BotF", 600


As I said before WolRon, if you can think of any small 2D algorithms I can do in my spare time, tell me and I'll be glad to help.

Ryan