Ballz (Animated Ball using sprite strip)

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by DarkUni.

Image strip sample using ClipSprite. This requires the ball.bmp available here: http://www.monroeworld.com/ball.bmp ...

This is quick, sloppy first shot code I did with PB. But it works :)

Code: Select all

 
; Fast Bouncing Balls (Image Strip style)
; Shane R. Monroe ([url]mailto:webmaster@darkunicornproductions.com[/url])

; Init sprites
If InitSprite() = 0
  MessageRequester("Error", "Can't open DirectX 7 Or later", 0)
  End
EndIf

; Init Keyboard
If InitKeyboard() = 0
  MessageRequester("Error", "Can't open DirectX 7 Or later", 0)
  End
EndIf

; build a structure for the balls
Structure balls
  x.w            ; Ball x
  y.w            ; Ball y 
  xv.b           ; X velocity
  yv.b           ; Y velocity
  frame.b        ; What frame is displayed
  frdelay.w      ; Frame Delay
EndStructure

; Create a list of balls
NewList ball.balls()

; Create 500 balls in a struct list
For t = 1 To 500
    AddElement(ball())          ; This adds a ball to the list
    ball()\x = 50+Random(700)   ; Assign the ball a random X location
    ball()\y = 50+Random(500)   ; Assign the ball a random Y location
    ball()\xv= 1 + Random(5)    ; Assign the ball a random X velocity
    ball()\yv= 1 + Random(5)    ; Assign the ball a random X velocity
    ball()\frame = 0            ; Start on the first ball in the image strip
    ball()\frdelay = 0          ; A rough counter to see if we should change frames.
Next

; Open up our screen
OpenScreen(800, 600, 16,"ballz")

LoadSprite(0, "ball.bmp",0)     ; Load our sprite strip

Repeat

ClearScreen(0,0,0)              ; start standard double buffering clear screen
FirstElement(ball())        ; Move the struct list to the first item
   While NextElement(ball())       ; As long as there are balls in the list ... do this loop

     ; This clips what area of the sprite shall be viewed
     ; 50*frame will put it at the X at the right 'frame' of the image strip
     ClipSprite(0, ball()\frame*50,0, 50, 50)
     DisplayTransparentSprite(0, ball()\x, ball()\y) ; Display the sprite with masking
     ball()\x = ball()\x + ball()\xv                 ; Move the sprite X velocity
     ball()\y = ball()\y + ball()\yv                 ; Move the sprite Y velocity

       ; Are we hitting a wall on X?  Change direction!
       If ball()\x  750
          ball()\xv=-ball()\xv
       EndIf

       ; Are we hitting a wall on Y?  Change direction!
       If ball()\y  550
          ball()\yv=-ball()\yv
       EndIf

        ; Increment the frdelay
        ball()\frdelay = ball()\frdelay + 1

        If ball()\frdelay > 10           ; We are greater than 10 loops?  Increment the frame of image strip
           ball()\frdelay =0             ; Reset the counter
           ball()\frame=ball()\frame + 1 ; Increment the frame counter

           ; No more frames?  reset to 0
           If ball()\frame=4
              ball()\frame=0
           EndIf

       EndIf
   Wend

      FlipBuffers()         ; Standard double buffer flip
      ExamineKeyboard()     ; Must check the keyboard for changes

  Until KeyboardPushed(#PB_Key_Escape)  ; Do the main loop until ESC is pressed.
End
Shane R. Monroe
Dark Unicorn Productions
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Whow, that's energic ! :). Nice example for a first program.. BTW, all your

ball()\x = ball()\x+1

like commands can be changed to:

ball()\x + 1

This facility works for any expression:

a = a*2+1

can be changed to

a*2+1


Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by DarkUni.

Wow! Short cuts!

Glad you liked it...

Shane R. Monroe
Dark Unicorn Productions
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by mystik.

Very nice,

I had 2000 of them moving around and no slow down at all.

My eyes are a little sore though :)

Steve.
Post Reply