News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

2d Stars Example

Started by kevin, August 29, 2023, 06:23:42 AM

Previous topic - Next topic

kevin

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.  

 



PlayBASIC Code: [Select]
; 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






 

   
   Learn to Code:

   https://playbasic.com


    Also see:  Electric 2D Stars