UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on August 29, 2023, 06:23:42 AM

Title: 2d Stars Example
Post by: kevin on August 29, 2023, 06:23:42 AM
2D Stars Examples


 This is classic 2D (right to left) star scrolling routine.   This version is using parallel arrays for the stars coordinate and speed. There's a bunch of ways to do this; but here we're create filling our star with a random starting coordinate within the bitmap screen as well as give this star a random speed.

 This is integer only so it has fixed point precision, which just means we scale our values up by some fixed constant and anything bellow this becomes the fractional part and anything above it is the whole number part.  

 



[pbcode]

; PROJECT : Project1
; AUTHOR  :
; CREATED : 29/08/2023
; ---------------------------------------------------------------------

   AccBits = 2^16

   ScreenWidth =800
   ScreenHeight =600
   Count=25000

   Dim StarsX(Count)
   Dim StarsY(Count)
   Dim StarsSpeed(Count)

   For lp =0 to count
      StarsX(lp) = rnd(ScreenWidth*AccBits)
      StarsY(lp) = rnd(ScreenHEight-1)
      StarsSpeed(lp) = rndrange(1*AccBits,5*AccBits)
   next
      
   // -------------------------------------------
   do // Main Loop
   // -------------------------------------------

      cls

      lockbuffer   
         For lp =0 to count
         // Move the star to the left
         x=StarsX(lp)-StarsSpeed(lp)
         
         if X<0
            // wrap the X coordinate
            X=X+(ScreenWidth*AccBits)
            // give this star a new Y and speed
            StarsY(lp) = rnd(ScreenHEight-1)
            StarsSpeed(lp) = rndrange(1*AccBits,5*AccBits)
         endif
         // Store the updated X
         StarsX(lp)=x
         // draw it to the surface
         dot x/AccBits,StarsY(lp)
      next
      unlockbuffer   

      // flip the buffers
      sync
   loop spacekey()=true


[/pbcode]


 

   
   Learn to Code:

   https://PlayBASIC.com


    Also see:  Electric 2D Stars (https://www.youtube.com/watch?v=uRkrxVNUFM8&ab_channel=PlayBasic)