Simple program keeps locking up - why?

Miscellaneous Forums/General Discussion/Simple program keeps locking up - why?

I'm coding a simple text-based board game in Blitz Basic 2D v1.68, and for some reason the following code will after 60+ seconds completely lock up and not allow you to exit the program via the 'Escape Key' and you have to use CTRL+ALT+DEL to manually close it, even if its compiled it still locks up.

At first I thought it might be to do with the fonts, but even if you comment out the use of fonts, it still locks up -- I've put the code below, simply copy/paste -- I don't know why its locking up, it's really frustrating me when I'm so close to finishing the game.


; ##############################################################
; #	
; #  DRAW A LIST OF PLAYERS
; #  @updated: 06-May-07
; #
; ##############################################################

;@app constants
Const	app_name$ 					= "List Players"
Const	app_version$				= "v0.0.1"
Const 	SCREEN_WIDTH				= 800
Const 	SCREEN_HEIGHT				= 600
Global player_on_turn = 1

; ##############################################################

; @application setup
AppTitle App_Title$()

; @graphics setup
Graphics SCREEN_WIDTH, SCREEN_HEIGHT, 16, 2
SetBuffer BackBuffer(): SeedRnd (MilliSecs())

; ##############################################################

Initalise_Players()
Big_Font()

; ##############################################################


While Not KeyHit(1)	
	Cls
	
		Draw_Players()	
	
	Flip
Wend
End

; ##############################################################

Type Player
	Field name$					; string
	Field gender$				; string
 	Field is_ai          		; boolean
	Field face					; pointer to the face image
	Field board_pos				; board position
	
	Field millions				; how many millions player has
	Field billions				; how many billions player has
	
	Field red					; RGB: Red
	Field green					; RGB: Green
	Field blue					; RGB: Blue
	
;	Field myHand[totalcards]	 ; List of cards held by the player
;	Field propHand[totalproperty]; List of property owned by the player	
End Type


Function Initalise_Players()
	; create human player
	Create_Player( "Player 1", "male", False)

	; get a random gender
	; get a random name based upon gender	
	; create a player using random name + gender, and make it AI.
	For i = 1 To 3 
		g$ = Random_Gender$()
 		n$ = "AI " + i 
		Create_Player( n$, g$, True)
	Next
	
	player_on_turn = Rand(1, 4)
End Function


Function Create_Player( name$, gender$="male", is_ai=False, board_pos=1, m=50, b=0 )
	p.player 	= New player	
	p\name$		= name$
	p\gender$	= gender$
	p\is_ai		= is_ai
	If ( p\gender$ = "male"   ) Then p\face	= Rand(0, 8)
	If ( p\gender$ = "female" ) Then p\face	= Rand(9, 17)
	p\board_pos	= board_pos
	p\millions	= m
	p\billions	= b
	p\red		= Rand(1,219)
	p\green		= Rand(1,219)
	p\blue		= Rand(1,219)
End Function

; ##############################################################

Function Random_Gender$() 
	g = Rand(1, 2)
	If g = 1 Then
		Return "male"
	Else 
		If g = 2 Then 
			Return "female"
		Else
			RuntimeError "No gender generated"
		End If
	End If
End Function


Function Draw_Players()
	Local t = 150
	Local v = 250
	Local c	= 0
	Local i = 30
	Local container_width	= 200
	Local container_height 	= 100
		
	; Draw List of Players
	For p.player = Each player
		c = c + 1
		
		If ( player_on_turn = c ) Then
			; Highlighted container
			Color p\red, p\green, p\blue 
			Rect SCREEN_WIDTH-160, i, container_width, container_height, True
			Color 255, 255, 255
			Text SCREEN_WIDTH - t, i , p\name$
		Else
			; Non-highlighted container
			Color p\red, p\green, p\blue
			Text SCREEN_WIDTH - t, i , p\name$
			Color 255, 255, 255		
		End If

;		; Display Cash		
		x = SCREEN_WIDTH - t
		y = i + 25
		Text x,y, p\billions +"b" + " " + p\millions + "m" 
		
		; Display Cards and Properties
		small_font()
		Text x,y+25, "Cards: 0"
		Text x,y+45, "Properties: 0"
		big_font()
		i = i + (container_height + 20)
	Next

End Function


Function Big_Font()
	SetFont LoadFont("Arial",24)
End Function

Function Small_Font()
	SetFont LoadFont("Arial",18)
End Function

Function Custom_Font(fontsize=10)
	SetFont LoadFont("Arial",fontsize)
End Function

; @application title
Function App_Title$()
	Return app_name$ + " " +app_version$
End Function



Why does this code keep locking up for and how do I remedy it?

Your font functions are loading a font every time they're called!

Just load each font once at the start of the code, then change between them using SetFont.

(edit:) exactly:
Function Big_Font()
	SetFont LoadFont("Arial",24)
End Function

You should use LoadFont at the start of the program, and store the font in a global variable:
Global BigFont = LoadFont("Arial", 24)
Later on, use this variable to set the font:
SetFont BigFont

Oh, thank you very much! I knew I was doing something stupid...

I've changed it now and its now not locking up any more!

Code changed to:
;@fonts
Global	font_giant
Global	font_big
Global	font_normal
Global	font_small
Global	font_tiny
font_giant	= LoadFont("Arial", 36)
font_big	= LoadFont("Arial", 24)
font_normal	= LoadFont("Arial", 18)
font_small	= LoadFont("Arial", 16)
font_tiny	= LoadFont("Arial", 12)

Change_Font("big")

Function Change_Font(style$="normal")	
	Select (Lower$(style$)) 
		Case "giant"
			SetFont font_giant
		Case "big"
			SetFont font_big
		Case "normal"
			SetFont font_normal
		Case "small"
			SetFont font_small
		Case "tiny"	
			SetFont font_tiny
		Default
			SetFont font_normal
	End Select
End Function	



Thanks again!

you could potential gain a little speed increase by using Constants and numbers as font identifiers..

pseudo code:
Constant FONT_BIG = 1
Constant FONT_NORMAL = 2
......


Change_Font(FONT_BIG)
......

Case FONT_BIG
SetFont font_big
......

that way it doesn't have to iterate through strings every time its going through the select...case statement.

oh, don't remember if Blitz is case sensitive or not now, you should perhaps choose your own constant names ;)

eh... this maybe offtopic, but how come you did not yet update? v1.98 is already out.

..because this is Blitzbasic not blitzplus or blitz3d or blitzmax..which is no longer supported..officially