News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Type List Example

Started by kevin, December 27, 2006, 10:02:30 PM

Previous topic - Next topic

kevin

 This example requires PB1.61.  it shows how you can create native linked lists through typed variables.  Lists can not only store the parent type but any inherited children also. So it's linked collection.. :)
 

PlayBASIC Code: [Select]
   ; Limit the program to a max of 60 frames per second or less
SetFPs 60


; Declare a type to hold all the properties of our BALL object
Type Ball
x#,y#
angle#
speed#
Radius
Colour
Endtype

; Dimension the Typed Variable OBJECT with LIST support
Dim Object as Ball list


; Start of main DO/LOOp
Do

; Clear Screen to rgb(0,0,0) (black)
Cls rgb(0,0,0)
print Fps()

; Check if the space key as pressed, if so, create a new ball
if Spacekey() and FireTimer<Timer()
Addball()
FireTimer=Timer()+20
endif

; PreLock the current buffer for a batch of 2D drawing
Lockbuffer

; Draw and move all the objects in the list
For Each Object()

; Move Object
x#=CosNewValue(Object.x,Object.angle,object.speed)
y#=SinNewValue(Object.y,Object.angle,object.speed)

; Check if the move balls position is inside the default screen size
if POintInBox(x#,y#,-50,-50,850,650)=false
; if the point it outside this area, then it's left the screen
; so we can delete it

Object = NULL ; Delete the current object
Continue ; Jump to the NEXT, so we avoid drawing a dead object

endif

; Store it's New position
Object.x#=x#
Object.Y#=y#

; Draw this ball as a filled coloured circle
CircleC X#,Y#,Object.Radius,true,Object.COlour

next

; unlock the screen buffer
unlockbuffer

Sync
loop



Function AddBall()

; Add a New Ball to the list
Object = New Ball

Object.X# =Rnd(GetScreenWidth())
Object.y# =Rnd(GetScreenHeight())
Object.Angle# =rnd(360)
Object.Speed# =rndrange(2,10)
Object.Radius =rndrange(10,50)
Object.Colour =rndrgb()

EndFunction







kevin

#1
 This is a variation of the above code, however this time we're dynamically managing sprites to be the graphical representations of our game characters.


PlayBASIC Code: [Select]
; Grab chunk of screen for as image for our sprites latter on. 
CircleC 16,16,16,true,rgb(255,255,255) ; drab a circle in white to the screen
GetImage 1,0,0,32,32 ; grab the circle block from the screen as an image
preparefximage 1 ; convert the image to an FX version, so it can be more easily rotated/tinted.




; Limit the program to a max of 60 frames per second or less
SetFPs 60.7


; Declare a type to hold all of the properties of each object
Type GameObject
Sprite ; sprite this object uses
angle# ; direction it's moving in
speed# ; speed it's moving at
Colour ; colour
Endtype

; Dimension the Typed Variable OBJECT with LIST support
Dim Object as GameObject list


; Start of main DO/LOOp
Do

; Clear Screen to rgb(0,0,0) (black)
Cls rgb(0,0,0)

Print "Press Space to Add Object to Scene"


; Check if the space key was pressed, if so, add a new character to the object list
if Spacekey() and FireTimer<Timer()
Addball()
FireTimer=Timer()+20
endif


; Run through and move all the objects in the list
For Each Object()

; Get the sprite this object is using
ThisSprite=Object.Sprite

; Move Object
x#=CosNewValue(GetSpriteX(ThisSprite),Object.angle,object.speed)
y#=SinNewValue(GetSpriteY(ThisSprite),Object.angle,object.speed)

; Check if the moved position is inside the default screen size ?
if POintInBox(x#,y#,-50,-50,850,650)=false

; if the point it outside this area, then it's left the screen
; so we can delete the sprite and the remove it's object from the list

DeleteSprite Sprite

; Delete the object in the list
Object = NULL ; Delete the current object
Continue ; Jump to the NEXT, so we avoid trying to position the deleted sprite bellow

endif

; Set the sprites new position.
PositionSprite ThisSprite,x#,y#

next


; Draw All Sprites
DrawAllSprites


; show the screen to the player
Sync


; loop back to the DO statement to keep this program running
loop






Function AddBall()

; Add a New Ball to the list
Object = New GameObject


Object.Angle# =rnd(360) ; Get the direction this object is moving
Object.Speed# =rndrange(2,10) ; Get the speed it'll be moving at
Object.Colour =rndrgb()


x#=Rnd(GetScreenWidth())
Y#=Rnd(GetScreenHeight())

// Create sprite to represent this object visually
ThisSprite=NewSprite(x#,y#,1)
SpriteDrawMode ThisSprite,2
SpriteTint ThisSprite,Object.Colour


Object.Sprite =ThisSprite

EndFunction






frenchgui

With Object=NULL line, the object still exists, is there a way to really delete it?

The function I use to count objects:

function countball()
   local i
   i=0
   for each object()
      inc i
   next
Endfunction i

kevin


QuoteWith Object=NULL line, the object still exists, is there a way to really delete it?

Object=Null does delete it.

You could also use FreeCell.

frenchgui

OOOoops! Sorry you're right. My error was that I've typed object()=null instead of object=null. Thanks for your fast reply...