• Welcome to Dark Bit Factory & Gravity.
 

Blitz Basic - Starfield Tutorial[BB2D]

Started by lilw4t3rdr0p, December 19, 2006

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

lilw4t3rdr0p

I was wondering if someone could post or walk me though a rolling script and star field in Basic. Thanks!

Jim

#1
Start with the starfield.  I'll post some hints and a little code, then see how much you can put together.
First you need to know about arrays.  You've just learned about variables, like X and Y for the position of your text, and how to move things around by adding to X and Y.  What you need to do a starfield is a lot of X and Ys.  One for each star.  You could do X1,Y1 for star 1 and X2,Y2 for star 2, etc, but that would be an awful lot of variables.

What you can do instead is create an array, like this

Dim starx%(20)
Dim stary%(20)

That will give you 20 stars that you can work with.  Each entry in the array is a separate variable.  Instead of naming them all something different, you're numbering them, so you can access them easily.

You use a For loop to run through all the stars, like this

For star%=1 to 20
  Print starx(star)
Next

That will print the x position of each of the stars.

You almost have enough information to do a starfield now.

To draw a graphics dot, use this command

WritePixel x,y,colour


So in your For loop, it'll look like this

For star%=1 to 20
  WritePixel starx(star),stary(star),$ffffffff
Next

That will draw a white dot for each star.

You need to give the stars a random initial position, which you can do before your main loop, like this

For star%=1 to 20
  starx(star)=Rand(640)
  stary(star)=Rand(480)
Next


And then you can scroll all the stars to the left like this, inside your loop

For star%=1 to 20
  starx(star)=starx(star)-1
Next

Each of the entries in the array acts just like a variable.

Finally, you can check for the stars going off the left hand edge and put them back on the right hand side

For star%=1 to 20
  If starx(star)<0 Then
    starx(star)=639
  EndIf
Next


So you have something like this

Graphics 640,480,0,2
SetBuffer BackBuffer()
create the stars (dim, set random positions)
start loop
  Cls
  draw stars
  move stars to left
  check stars off left side and move them to right side
  Flip
loop while key not pressed
End


See if you can put that together into a starfield.

Jim



Challenge Trophies Won:

lilw4t3rdr0p


lilw4t3rdr0p



lilw4t3rdr0p

Cute.... very cute!!!

Shhh don't tell   :whisper:  I gave it to you! ;)

Jim

Please post what you got - then we can work on some improvements!

Jim
Challenge Trophies Won: