News:

Function Finder  Find all the functions within source code files

Main Menu

How To Handle More than One Object

Started by kevin, January 02, 2006, 06:55:41 AM

Previous topic - Next topic

kevin

Sorry This tutorial is code only for now.




PlayBASIC Code: [Select]
; Make a ball image
Size=100
CreateImage 1,Size,Size
RenderPhongImage 1,Size/2,Size/2,$ffff8f,size*0.9,2.0


; define a ball type
Type tBall
Status
Xpos,Ypos
SpeedX,SpeedY
Image
LifeTimer
FlashToggle
EndType

; create an array to house our list of balls
dim Balls(1) as tBall



; Main Loop

Do
Cls rgb(20,30,40)
Print "press space to create ball"


UpdateBalls()

; Check for space key
If SPacekey() and KeyPress=false

; Calc a random starting position for this ball
Xpos=Rnd(GetScreenWIdth())
Ypos=Rnd(GetScreenHeight())

; Create (init) a ball at this position and give it a random speed
CreateBall(Xpos,Ypos,NewSpeed(5),NewSpeed(5))

KeyPress=True

else
If SPacekey()=false then KeyPress=false
endif

Sync
loop



; this function finds a random speed for a ball axis.
Function NewSpeed(Max)
Repeat
Speed=RndRange(-max,max)
Until Speed<>0
EndFunction Speed



Function UpdateBalls()
; run through all
ScreenWidth=GetSCreenWidth()
ScreenHeight=GetSCreenHeight()

CurrentTime=Timer()

For lp=1 to GetArrayelements(Balls().tball,1)
if Balls(lp).Status=True

ThisImage=Balls(lp).Image
ImageWidth =GetImageWidth(ThisImage)/2
ImageHeight =GetImageHeight(ThisImage)/2

; Move the ball on the X axis
NewXpos=Balls(lp).Xpos+Balls(lp).SpeedX

; Check for Left Edge redbound
If NewXpos<=ImageWidth
NewXpos=ImageWidth
Balls(lp).SpeedX=Balls(lp).SpeedX*-1
endif

; check for right edge rebound
If NewXpos=>(ScreenWidth-ImageWidth)
NewXpos=ScreenWidth-ImageWidth
Balls(lp).SpeedX=Balls(lp).SpeedX*-1
endif

; MOve the ball along the y axis
NewYpos=Balls(lp).Ypos+Balls(lp).SpeedY
; Check for Left Edge redbound
If NewYpos<ImageHeight
NewYpos=ImageHeight
Balls(lp).SpeedY=-Balls(lp).SpeedY
endif

; check for right edge rebound
If NewYpos>(ScreenHeight-ImageHeight)
NewYpos=ScreenHeight-ImageHeight
Balls(lp).SpeedY=-Balls(lp).SpeedY
endif

; Store this balls position
Balls(lp).Xpos=NewXpos
Balls(lp).Ypos=NewYpos


LifeTimeLeft=Balls(lp).LifeTimer-CurrentTime
if LifeTimeLeft<0
DeleteBall(lp)
else

; Check if the ball has less than a second to live
if LifeTimeLeft<500

Balls(lp).FlashToggle=Mod(Balls(lp).FlashToggle+1,8)

if Balls(lp).FlashToggle>3
drawimage ThisIMage,NewXpos-imagewidth,NewYpos-imageheight,1
endif

CenterText NewXpos,NewYpos,LifeTimeLeft

else
; draw the image to represent this ball
drawimage ThisIMage,NewXpos-imagewidth,NewYpos-imageheight,1
endif

endif


endif
next


EndFunction


; Some Helper Functionsto deals with balls

Function CreateBall(Xpos,Ypos,SpeedX,Speedy)
; Get a free Ball Index
ThisBall=GetFreeBall()

; init it's settimgs
Balls(ThisBall).status=True
Balls(ThisBall).xpos =xpos
Login required to view complete source code








kevin

#1
 Here's an alternative version of the above example.    This one uses  GetArrayCells() for to find free indexs within the Balls() array, then it uses the NULL writing to delete a type within the array.


PlayBASIC Code: [Select]
; Make a ball image
Size=100
CreateImage 1,Size,Size
RenderPhongImage 1,Size/2,Size/2,$ffff8f,size*0.9,2.0


; define a ball type
Type tBall
Status
Xpos,Ypos
SpeedX,SpeedY
Image
LifeTimer
FlashToggle
EndType

; create an array to house our list of balls
dim Balls(1) as tBall


; -----------------------------------------
; Main Loop
; -----------------------------------------

Do
; Clear the Screen
Cls rgb(20,30,40)
; Display Controls
Print "press space to create ball"


; Call the UpdateBalls() function
UpdateBalls()

; Check for space key
If SPacekey()=true and KeyPress=false

; Calc a random starting position for this ball
Xpos=Rnd(GetScreenWIdth())
Ypos=Rnd(GetScreenHeight())

; Create (init) a ball at this position and give it a random speed
CreateBall(Xpos,Ypos,NewSpeed(5),NewSpeed(5))

KeyPress=True

else
If SPacekey()=false then KeyPress=false
endif

Sync
loop



; this function finds a random speed for a ball axis.
Function NewSpeed(Max)
Repeat
Speed=RndRange(-max,max)
Until Speed<>0
EndFunction Speed



Function UpdateBalls()
; run through all
ScreenWidth=GetSCreenWidth()
ScreenHeight=GetSCreenHeight()

CurrentTime=Timer()

For lp=1 to GetArrayelements(Balls().tball,1)
if Balls(lp).Status=True

ThisImage=Balls(lp).Image
ImageWidth =GetImageWidth(ThisImage)/2
ImageHeight =GetImageHeight(ThisImage)/2

; Move the ball on the X axis
NewXpos=Balls(lp).Xpos+Balls(lp).SpeedX

; Check for Left Edge redbound
If NewXpos<=ImageWidth
NewXpos=ImageWidth
Balls(lp).SpeedX=Balls(lp).SpeedX*-1
endif

; check for right edge rebound
If NewXpos=>(ScreenWidth-ImageWidth)
NewXpos=ScreenWidth-ImageWidth
Balls(lp).SpeedX=Balls(lp).SpeedX*-1
endif

; MOve the ball along the y axis
NewYpos=Balls(lp).Ypos+Balls(lp).SpeedY
; Check for Left Edge redbound
If NewYpos<ImageHeight
NewYpos=ImageHeight
Balls(lp).SpeedY=-Balls(lp).SpeedY
endif

; check for right edge rebound
If NewYpos>(ScreenHeight-ImageHeight)
NewYpos=ScreenHeight-ImageHeight
Balls(lp).SpeedY=-Balls(lp).SpeedY
endif

; Store this balls position
Balls(lp).Xpos=NewXpos
Balls(lp).Ypos=NewYpos


LifeTimeLeft=Balls(lp).LifeTimer-CurrentTime
if LifeTimeLeft<0
DeleteBall(lp)
Continue
Endif

; Check if the ball has less than a second to live
if LifeTimeLeft<500

Balls(lp).FlashToggle=Mod(Balls(lp).FlashToggle+1,8)

if Balls(lp).FlashToggle>3
drawimage ThisIMage,NewXpos-imagewidth,NewYpos-imageheight,1
endif

CenterText NewXpos,NewYpos,LifeTimeLeft

else
; draw the image to represent this ball
drawimage ThisIMage,NewXpos-imagewidth,NewYpos-imageheight,1
endif



endif
next


EndFunction


; Some Helper Functionsto deals with balls

Function CreateBall(Xpos,Ypos,SpeedX,Speedy)
; Get a free Array Index within the Balls() array
ThisBall=GetFreeCell(Balls())

Login required to view complete source code



kevin

#2
 Here's a linked list version.

PlayBASIC Code: [Select]
; Make a ball image
Size=100
CreateImage 1,Size,Size
RenderPhongImage 1,Size/2,Size/2,$ffff8f,size*0.9,2.0


; define a ball type
Type tBall
Xpos,Ypos
SpeedX,SpeedY
Image
LifeTimer
FlashToggle
EndType

; create a linked list of called BALL of TBALL
dim Ball as tBall list


; -----------------------------------------
; Main Loop
; -----------------------------------------

Do
; Clear the Screen
Cls rgb(20,30,40)
; Display Controls
Print "press space to create ball"


; Call the UpdateBalls() function
UpdateBalls()

; Check for space key
If SPacekey()=true and KeyPress=false

; Calc a random starting position for this ball
Xpos=Rnd(GetScreenWIdth())
Ypos=Rnd(GetScreenHeight())

; Create (init) a ball at this position and give it a random speed
CreateBall(Xpos,Ypos,NewSpeed(5),NewSpeed(5))

KeyPress=True

else
If SPacekey()=false then KeyPress=false
endif

Sync
loop



; this function finds a random speed for a ball axis.
Function NewSpeed(Max)
Repeat
Speed=RndRange(-max,max)
Until Speed<>0
EndFunction Speed



Function UpdateBalls()
; run through all
ScreenWidth=GetSCreenWidth()
ScreenHeight=GetSCreenHeight()

CurrentTime=Timer()

For Each Ball()

ThisImage=Ball.Image
ImageWidth =GetImageWidth(ThisImage)/2
ImageHeight =GetImageHeight(ThisImage)/2

; Move the ball on the X axis
NewXpos=Ball.Xpos+Ball.SpeedX

; Check for Left Edge redbound
If NewXpos<=ImageWidth
NewXpos=ImageWidth
Ball.SpeedX=Ball.SpeedX*-1
endif

; check for right edge rebound
If NewXpos=>(ScreenWidth-ImageWidth)
NewXpos=ScreenWidth-ImageWidth
Ball.SpeedX=Ball.SpeedX*-1
endif

; MOve the ball along the y axis
NewYpos=Ball.Ypos+Ball.SpeedY
; Check for Left Edge redbound
If NewYpos<ImageHeight
NewYpos=ImageHeight
Ball.SpeedY=-Ball.SpeedY
endif

; check for right edge rebound
If NewYpos>(ScreenHeight-ImageHeight)
NewYpos=ScreenHeight-ImageHeight
Ball.SpeedY=-Ball.SpeedY
endif

; Store this balls position
Ball.Xpos=NewXpos
Ball.Ypos=NewYpos


LifeTimeLeft=Ball.LifeTimer-CurrentTime
if LifeTimeLeft<0
; Kill This object in the list
Ball = NULL
Continue
Endif

; Check if the ball has less than a second to live
if LifeTimeLeft<500

Ball.FlashToggle=Mod(Ball.FlashToggle+1,8)

if Ball.FlashToggle>3
drawimage ThisIMage,NewXpos-imagewidth,NewYpos-imageheight,true
endif

CenterText NewXpos,NewYpos,LifeTimeLeft

else
; draw the image to represent this ball
drawimage ThisIMage,NewXpos-imagewidth,NewYpos-imageheight,true
endif


next


EndFunction



Function CreateBall(Xpos,Ypos,SpeedX,Speedy)
; Get a free Array Index within the Balls() array

Ball= New tBall
; init it's settimgs
Ball.xpos =xpos
Ball.ypos =ypos
Login required to view complete source code