500 balloons demo , as in PB 500 bubble demo

Started by micky4fun, March 27, 2010, 04:14:41 PM

Previous topic - Next topic

micky4fun

hi all

just quickly making a birthday welcome for my niece for putting on her computer in the start up , as she turns it on for her 18th birthday ,
i started with a few balloons darting around a happy birthday background , i will be adding to this with pics of her over the years and some differant wipes with the balloons
but this post is just a quick re-write of the 500 bubble demo in PB , think this maybe a little easier to follow for newbies , and it was the 500 bubble demo that got me into PB in the first
place

enjoy , change the how_many to as many as your pc can handle , im on 1500 no probs
mick ;D

kevin

#1
  Here's a slightly tweaked version of the code that rotates the sprites, or does it ? :)  

PlayBASIC Code: [Select]
   screenvsync on

LoadfxImage "balloon1.bmp",1
LoadfxImage "balloon2.bmp",2
LoadfxImage "balloon3.bmp",3
LoadImage "back.jpg",8


CacheImageSize=256+128
CreateIMage 11,CacheImageSize,CacheImageSize
CreateIMage 12,CacheImageSize,CacheImageSize
CreateIMage 13,CacheImageSize,CacheImageSize


how_many = 50

Type Bubble
x#,y#,XSpeed#,Yspeed#
EndType
Dim balloons(how_many) As Bubble

; set up how_many balloons , there start x,y position and there xspeed and yspeed
For amount=1 To how_many
balloons(amount).x# = Rnd(1000)
balloons(amount).y# = Rnd(700)
balloons(amount).xspeed# = RndRange#(1,4)
balloons(amount).yspeed# = RndRange#(1,4)
CreateSprite amount
SpriteImage amount,10+rndrange(1,3)
centerspritehandle amount
Next amount


// create an extra sprite we'll use to pre-render the balloon images rotating
Spr=NewSprite( CacheImageSize/2, CacheImageSize/2,11)
spritedrawmode spr,2
spritefilter spr,true

; main loop , draw background then the amount of balloons set by how_many
Do

drawimage 8,0,0,0

// draw rotated versions to 3 balloon images,to 3 different images
spritevisible spr,true
rendertoimage 11
cls 0
spriteimage spr,1
centerspritehandle spr
drawsprite spr

rendertoimage 12
cls 0
spriteimage spr,2
centerspritehandle spr
drawsprite spr

// draw the
rendertoimage 13
cls 0
spriteimage spr,3
centerspritehandle spr
drawsprite spr


turnsprite Spr,1

spritevisible spr,false
rendertoscreen

For amount=1 To how_many
balloons(amount).x#=balloons(amount).x#+balloons(amount).xspeed#
balloons(amount).y#=balloons(amount).y#+balloons(amount).Yspeed#
If balloons(amount).x#<-200 Or balloons(amount).x#>1200 then balloons(amount).xspeed#=balloons(amount).xspeed#*-1
If balloons(amount).y#<-200 Or balloons(amount).y#>1000 Then balloons(amount).yspeed#=balloons(amount).yspeed#*-1
PositionSprite amount,balloons(amount).x#,balloons(amount).y#
Next amount

DrawAllSprites
Sync

Loop





micky4fun

#2
hi kevin ,

so your rotating the image of the sprite and not the sprite itself , i did do the sprites rotating at one stage but with 500 it was ok on this fast pc , but on another it was quite slow
but i had control so all the sprites and they could have differant rotating speeds each , but would your way be faster? ,


mick :)

kevin

#3
   There's a great old saying, there's more than one way to skin a cat  - This is very true In programming, as 99.99999% of the time,  the most obvious solution, just happens to also  be the slowest solution !     

  Above, all the code does is rotate/render the 3 separate textures to some cache images.  So these cache hold the rotated state of the image this frame.   It does this, because drawing none rotated images is faster than drawing a rotated image, in particular with filtering    So rather than drawing them all dynamically,  we use a slight of hand trick to cull the work load down dramatically.     So rather than rotating 50 sprites, we're rotating 3 of them.    This type of illusion is very common in games..  

  See Blog ->  Think Better Design (login required)