A first attempt with BMax - Feedback Appreciated

BlitzMax Forums/BlitzMax Beginners Area/A first attempt with BMax - Feedback Appreciated

Well this is pretty much the first working program I've attempted since perhaps my TI-99/4a basic days, or something else similar long ago.

It's a little simulation of radioactive atoms decaying (this was just the first idea I came up with at the weekend), which draws a graph as the decay occurs. I'd consider it a work in progress, i've plenty of ideas for extra stuff to add, but i'd really appreciate any constructive comments regarding my code. e.g. Is it as poor as I expect? What could I improve in it? In which ways would my code be considered bad practice? etc... (I really have not grasped OOP concepts, and made no attempt to use them here).

If anybody is willing to take a look through my work and/or try it out then your feedback would be appreciated. Here goes....

See Below


Note: I notice that pressing space to reset the atoms/graph after it has run through doesn't always work first time, I'm sure this is something simple if anybody could point that out too! Thanks.

Sorry for the incorrect use of the code tags again :-\ One day i'll get them right, this time i remembered to use lowercase, but used the wrong brackets i guess...

Please ignore this post, My posting became a comedy of errors... and I only just noticed the 'edit' option...

for long sources replace [ code ] with [ codebox ]

Not sure about 'BlitzMax Beginners Area', i need a 'BlitzMax Forum Beginners Area' to hone my posting skills ;) Here goes for the third time, hopefully with a codebox, a couple more comments and better formatting, preview post was a good idea this time too...
'----------------------------------------
' Matt's little radioactive decay program
'----------------------------------------

'--------------------------------
' Setup variables
'--------------------------------
Strict
Global atoms[24,24] 'an array to hold the states of all the atoms, 1 = active, 0 = decayed
Global undecayed[100] 'an array to store the number of undecayed atoms after each second
Global time:Int = 0
Global graphx:Int = 390 'position of graph on screen
Global graphy:Int = 110 'position of graph on screen
Global atomx:Int = 100 'position of atoms diagram on screen
Global atomy:Int = 100 'position of atoms diagram on screen
Global probofdecay:Float = 0.05 'probability of atom decaying each second
Global height:Int = 600
Global width:Int = 800
Const hdivision:Int = 600/20 'for drawing gradient background
SeedRnd MilliSecs()

' --------------
' Start graphics
' --------------

'SetGraphicsDriver GLMax2DDriver()
Graphics width,height,24

'-----------
' initialise
'-----------

newstart()

' ---------
' main loop
' ---------

While Not KeyHit(KEY_ESCAPE)


		If KeyHit(KEY_SPACE) And time < 99
			For Local t = 1 To 99
				time:+1
				decayatoms()
				drawatoms()
				countatoms()
				drawscreen()
			Next
		EndIf
		
		If KeyHit(KEY_SPACE) And time = 99
					newstart()
		EndIf
				
		If KeyHit(KEY_ESCAPE)
					FlushMem
					End
		EndIf


Wend 

End



'========== FUNCTIONS ================


'-------------------------------------------------------
' populate atoms - sets every atom in array atoms() to 1
'-------------------------------------------------------

Function populateatoms()
For Local x:Int = 0 To 23
	For Local y:Int = 0 To 23
		
		atoms[x,y] = 1
	Next
Next
EndFunction


'------------------------------------------------------------------------
' draw atoms - draw the square of atoms, colour coded, to the back buffer
'------------------------------------------------------------------------

Function drawatoms()

For Local x:Int = 0 To 23
	For Local y:Int = 0 To 23
		If atoms[x,y] = 1
			SetColor ((atoms[x,y] * 255),0,0)
		Else
			SetColor(0,0,128)
		EndIf		
		DrawText "o",(10 + (x * 10))+atomx,(10 + (y * 10))+atomy
	Next
Next

EndFunction

'----------------------------------------------------
' count atoms - counts the number of atoms remaining (i.e. equal to 1)
'----------------------------------------------------

Function countatoms()

For Local x:Int = 0 To 23
	For Local y:Int = 0 To 23
		
		undecayed[time] = undecayed[time] + atoms[x,y]
	
	Next
Next
EndFunction

'-----------------------------------------------------------------
' decay atoms - change array value to 0 for each atom which decays
'-----------------------------------------------------------------

Function decayatoms()
' decay atoms
For Local x:Int = 0 To 23
	For Local y:Int = 0 To 23
		
		If atoms[x,y] = 1 And Rnd() < probofdecay
			atoms[x,y] = 0
		EndIf
	
	Next
Next
EndFunction

'--------------------------
' draw graph to back buffer
'--------------------------

Function drawgraph()

	SetColor(255,255,255)
	DrawRect(graphx,graphy,305,305)
	For Local x = 0 To time
		SetColor(0,0,0)
		DrawOval((x * 3)+graphx,(Int(((Float(undecayed[0]) - Float(undecayed[x]))/Float(undecayed[0]))*300))+graphy,5,5)
			If x>0
			 	SetColor(0,0,255)
				DrawOval((x*3)+graphx,(300-Int(((Float(undecayed[x-1]) - Float(undecayed[x]))/Float(undecayed[0]))*300))+graphy,5,5)
			EndIf
	Next
	
SetColor(0,0,0)
SetClsColor(0,0,0)
EndFunction

'----------------------------------------------
' drawscreen - draws the screen image and flips
'----------------------------------------------

Function drawscreen()
	SetClsColor(0,0,64)
	Cls
	drawbg()
	SetColor(255,255,0)
	DrawText("Probability of Decay = "+Int(probofdecay*100)+"%",70,430)
	DrawText("Atoms Remaining = "+undecayed[time],70,370)
	DrawText("Time = "+time+" seconds (99 max)",70,400)
	If time < 99 
		DrawText("Press <SPACE> to start",70,460)
	Else
		DrawText("Press <ESCAPE> to exit or <SPACE> to reset.",70,460)
	EndIf
	SetColor(255,255,255)
	DrawText "MemUsage="+MemUsage(),0,0
	drawatoms()
	drawgraph()
	Flip
	FlushMem
EndFunction



'-------------------------------------------------------
' drawbg - draws the background & non-changing text etc.
'-------------------------------------------------------

Function drawbg()
	Local intensity:Int = 0

	For Local h:Int = 0 To height Step hdivision
		intensity:+1
		SetColor(0,0,intensity*5)
		DrawRect(0,h,width,hdivision)
	Next
	SetColor(255,255,255)
	DrawRect(graphx-10,graphy-10,325,360)
	SetColor(0,0,0)
	DrawRect(graphx-5,graphy-5,315,350)
	SetColor(255,255,255)
	DrawRect(graphx,graphy+310,305,30)
	SetColor(0,0,0)
	DrawText("Atoms Remaining",graphx+20,graphy+315)
	SetColor(0,0,255)
	DrawText("Activity",graphx+220,graphy+315)
	SetBlend(ALPHABLEND)
	SetAlpha(0.2)
	SetColor(255,255,255)
	DrawRect(atomx+5,atomy+8,250,250)
	SetScale(6,6)
	DrawText("ATOMS",atomx+10,atomy+100)
	SetAlpha(1)
	SetBlend(MASKBLEND)
	SetScale(1,1)
EndFunction


'----------------------------------------
' newstart - reset to original conditions
'----------------------------------------

Function newstart()
	time = 0
	populateatoms()
	countatoms()
	drawscreen()

EndFunction


Anyway, thanks for bearing with me, any comments (about my code, rather than my lack of forum skill) would still be appreciated :)

A couple of things I've noticed:

The following is unnecessary since you are already checking for KEY_ESCAPE in your while loop
		If KeyHit(KEY_ESCAPE)
					FlushMem
					End
		EndIf



You're not using flushmem in your main loop, this will cause slowdown and a memory leak. (flushmem has scope)

Perturbio:
Thanks for replying.... I added the extra check for KEY_ESCAPE in the for-next loop whilst drawing the graph 'cause on my laptop it runs slowly and the check in the while loop only lets you exit once the graph is drawn.

Also flushmem is in the drawscreen function, which is part of the main loop.

What do you mean 'flushmem has scope'?

http://www.blitzwiki.org/index.php/FlushMem

Excellent, thank you... thats a real help and explains a problem I had with an earlier iteration of the program that quickly used several 100meg for no reason I could work out...
Cheers :)