Author Topic: Blitz Basic - Starfield Tutorial[BB2D]  (Read 5351 times)

0 Members and 1 Guest are viewing this topic.

lilw4t3rdr0p

  • Guest
Blitz Basic - Starfield Tutorial[BB2D]
« on: December 19, 2006 »
I was wondering if someone could post or walk me though a rolling script and star field in Basic. Thanks!
« Last Edit: July 21, 2007 by Shockwave »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Blitz Basic - Starfield Tutorial
« Reply #1 on: December 19, 2006 »
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
Code: [Select]
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
Code: [Select]
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
Code: [Select]
WritePixel x,y,colour

So in your For loop, it'll look like this
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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
Code: [Select]
For star%=1 to 20
  If starx(star)<0 Then
    starx(star)=639
  EndIf
Next

So you have something like this
Code: [Select]
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



« Last Edit: December 19, 2006 by Jim »
Challenge Trophies Won:

lilw4t3rdr0p

  • Guest
Re: Blitz Basic - Starfield Tutorial
« Reply #2 on: December 19, 2006 »
THANKS SO MUCH JIM. KARMA!!

lilw4t3rdr0p

  • Guest
Re: Blitz Basic - Starfield Tutorial
« Reply #3 on: December 20, 2006 »
I got it!  :inspired:

Offline CoDeMaN

  • Atari ST
  • ***
  • Posts: 127
  • Karma: 38
    • View Profile
Re: Blitz Basic - Starfield Tutorial
« Reply #4 on: December 20, 2006 »
I got it!  :inspired:

Hope its not catching :P

lilw4t3rdr0p

  • Guest
Re: Blitz Basic - Starfield Tutorial
« Reply #5 on: December 20, 2006 »
Cute.... very cute!!!

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

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Blitz Basic - Starfield Tutorial
« Reply #6 on: December 21, 2006 »
Please post what you got - then we can work on some improvements!

Jim
Challenge Trophies Won: