Very Basic Noob Sprite Demo

BlitzMax Forums/BlitzMax Beginners Area/Very Basic Noob Sprite Demo

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I know it's always good for noobs playing around with BMax to have something like this, so I threw it together. Nothing fancy, probably would have been better to use a TList, but I thought noobs might be more comfortable at the start with arrays. :)

Features:
Array of objects for sprite handling.
Basic delta time movement.
Frames per second displayed.

SuperStrict
'
' Basic Blitzmax Noob Sprite Demo By "MGE"
'
Const howmany:Int = 1000 ' <<<<<< Change this value for how many
'
Global sprites:TSprite[howmany]  ' Create an array of the type "TSprite"
Global MyImage:Timage = LoadImage("tile.png") ' any image file you want here
Global fps:Int        ' used to display the frames per second
Global fpstotal:Int   ' counts how many frames rendered in 1000ms (1 second)
Global ms:Float       ' var to keep track if 1000ms (1 sec) has passed
Global ms_start:Float ' var holds the beginning ms value at top of loop
Global delta:Float    ' simple delta time var. Difference between start and end in loop.
'
Type TSprite
 Field x:Float,y:Float
 Field xdir:Float,ydir:Float
 Field alfa:Float
 Field blendtype:Int
 '
 Method init()
  x = Rand(0,800)
  y = Rand(0,600)
  xdir = Rnd(-.2,.2)
  ydir = Rnd(-.2,.2)
  If Rand(0,100)>50
   blendtype = ALPHABLEND
  Else
   blendtype = LIGHTBLEND
  EndIf
  alfa = Rnd(.1,1)
 End Method
 '
 Method update()
  x=x+(xdir*delta)
  y=y+(ydir*delta)
  If x>800 Or x<0
   xdir=-xdir
   x=x+xdir
  EndIf
  If y>600 Or y<0
   ydir=-ydir
   y=y+ydir
  EndIf
  '
 End Method
 '
 Method render()
  SetAlpha alfa
  SetBlend blendtype
  DrawImage MyImage:Timage,x,y
 End Method
 '
End Type
'
Graphics(800,600,0,60) ' width, height, depth (16,32, 0=windowed), hz
'
For Local c:Int = 0 To howmany-1 ' arrays start at 0 so we go to total - 1
 sprites[c] = New TSprite            ' fill our array with our object type
 sprites[c].init                   ' call the object's init routine to set up the sprite
Next
'
resetstates()                    ' reset render states
ms = MilliSecs()                 ' get the current 1000ms clock
'
Repeat
 ' 
 ms_start=MilliSecs()
 '
 Cls
 '
 For Local obj:TSprite = EachIn sprites ' cycle through all objects in array
  obj.update          ' Call the "update" method to update sprite's position.
  obj.render          ' Call the "render" method to display the sprite.
 Next
 '
 resetstates() 
 DrawText "FPS: "+fps,0,0           ' Display how fast the program is running.
 Flip(0)                            ' Flip(1) for vsync, Flip(0) for full speed
 delta = MilliSecs() - ms_start     ' Computer delta, start time - end time of loop
 '
 DoFPS()                            ' Keep track of how many frames per second
 ' 
Until KeyHit(KEY_ESCAPE)            ' Press ESC to exit
'
EndGraphics()                       ' Close down graphics device
End                                 ' Still need docs on this command.
'
Function resetstates()              ' Reset render states
 SetColor(255,255,255)
 SetAlpha(1)
 SetBlend(ALPHABLEND)
 SetTransform 0,1,1
End Function
'
Function DoFPS()                    ' Inc frames, and track if 1 sec has passed
 fpstotal = fpstotal + 1
 If MilliSecs()>ms
  fps = fpstotal
  fpstotal = 0
  ms = MilliSecs() + 1000
 EndIf
End Function