• Welcome to Dark Bit Factory & Gravity.
 

2D Starfield going up?

Started by Gamer2007, October 14, 2008

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Gamer2007

hello,

I want 2D Starfield going up but I dont know how to do it  ???


stormbringer

just draw dots (point primitive in your graphics framework)

keep an array of your stars. Each star should have the following properties

position x
position y
speed x
speed y
color

at each redraw pass, add speed x and y to the position of the star, the draw it. use the color of the star

now if you keep an array of "star" structures, a simple loop through all the stars to increment the x,y position by the speed x,y will give you a nice starfield.

if you initialize your stars with different speeds and colors (especially if the color is darker when the speed is smaller) then you get a nice 2d starfield. the speed x and y will definie the direction of the motion.

So in your case, the x speed should be zero. y speed can be positive or negative to move your stars up or down
We once had a passion
It all seemed so right
So young and so eager
No end in sight
But now we are prisoners
In our own hearts
Nothing seems real
It's all torn apart

Clyde

Hello there Newbie, :hi: aboard.

Good one Stormbringer.

How much coding knowledge do you have Newbie dude? Do you understand what variables are?

Please dont get me wrong, if you know quite a bit allready, just that going by your username, it looks as if you're just starting out. Post back, if you are in any doubts, and some of us here will put you on the right track.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Gamer2007

It wont go up  ???


Global num_stars = 200

Graphics 640,480,16,2
SetBuffer BackBuffer()

; Main menu arrays.
Dim y(num_stars,3)

; Generate the stars
For i=1 To num_stars
y(i,1)=Rnd(639)+1
y(i,2)=Rnd(479)+ 1
y(i,3)=Rnd(5)+1
Next


While Not KeyDown(1)

      update_stars()

      Flip:Cls

Wend



Function update_stars()
For i = 1 To num_stars
    y(i,1) = y(i,1) + y(i,3)

    ; if Starfield go pass Y= 0 then
            ; Restart the starfield at the bottom
If y(i,1) =<0
y(i,1) = 480
y(i,2) = Rnd(479) +1
EndIf

col = y(i,3) * 30
Color col,col,col

Rect y(i,1),y(i,2),2,2
Next
End Function

Clyde

#4
Just going through the code you posted dude and so far I've gathered that:

y(i,1) is the X Position.
y(i,2) is the Y Position.
y(i,3) is the speed.

And currently it's going left to right; which is the x position; y(i,1) is the X Position and your looking to do stuff to the Y position ;)

I'd highly recommend that you use more suitable names for your variables dude, that way you wont confuse yourself for one thing.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Gamer2007

thank you Clyde  ;D

FIXED!  8)


Global num_stars = 200

Graphics 640,480,16,2
SetBuffer BackBuffer()

; Main menu arrays.
Dim X(num_stars,2)
Dim y(num_stars,3)

; Generate the stars
For i=1 To num_stars
x(i,1)=Rnd(639)+1  ; X
y(i,2)=Rnd(479)+ 1 ; Y
y(i,3)=Rnd(5)+1    ; Speed
Next


While Not KeyDown(1)

      update_stars()

      Flip:Cls

Wend



Function update_stars()
For i = 1 To num_stars
    y(i,2)= y(i,2)-y(i,3)

    ; if Starfield go pass Y= 0 then
        ; Restart the starfield at the bottom
If y(i,2)=<0
y(i,2) = 480
;(i,2) = Rnd(479) +1
EndIf

col = y(i,3) * 30
Color col,col,col

Rect x(i,1),y(i,2),2,2
Next
End Function