#### Archived

This topic is now archived and is closed to further replies.

# Starfield programming

This topic is 5950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m making a space fighting game similiar to Space Invaders. I want the background to scroll down so it appears that you are moving forward, and the enemies backward. I would use a background image, but I saw something cool on another game similiar to this. There where multiple layers of stars that where not on an image. They were coded and placed on the screen randomly. Each layer traveled at a different speed. So some stars that were close to you travel very fast, and the farther away ones moved slow. I hope that makes sense. Does anyone know how to do this? --Muzlack

##### Share on other sites

http://sunsite.dk/demoscene/code/hornet/tutors/denthor/

although it is written for mode 13h (320*200 resolution for dos), The basic concept isn''t that hard to grasp.

good luck!

##### Share on other sites
What are you using as your rendering API (GDI, Dos, Allegro, OpenGL, DX, D3D,etc)?

Billy

##### Share on other sites
Yeah.

  typedef struct _tagSTAR{ float fX; float fY; float fVelocity;} STAR;#define MAX_STARS 100STAR Field[MAX_STARS];void ScrollStarField(float fElapsedTimeInSeconds){ for (int nStar = 0 ; nStar < MAX_STARS ; nStar ++) { Field[nStar].fY += Field[nStar].fVelocity * fElapsedTimeInSeconds; if (Field[nStar].fY > BOTTOM_OF_THE_SCREEN) { Field[nStar].fX = RandomNumberBetweenZeroAnd(WIDTH_OF_SCREEN); Field[nStar].fY = TOP_OF_SCREEN; Field[nStar].fVelocity = RandomNumberBetweenZeroAnd(MAX_VELOCITY) / 10; } }}void PlotStarField(void ){ COLORREF rgbColor; WORD rgbCOmponent; for (int nStar = 0 ; nStar < MAX_STARS ; nStar ++) { rgbComponent = 256 * (Field[nStar].fVelocity * 10) / MAX_VELOCITY) rgbColor = RGB(rgbComponent,rgbComponent,rgbComponent); Plot(Field[nStar].fX,Field[nStar].fY,rgbColor); }}

something like that.

D.V.

Edited by - DeltaVee on November 7, 2001 2:53:49 PM

##### Share on other sites
Dude, I think you might need a bit more experience programming before you tackle a space-invaders game if you can''t figure this out.

The stars can be either pixels, or small images.

do this
  // init the starsfor(i = 0; i < MAX_NUM_STARS; i++){ // slow stars if (i < MAX_NUM_STARS/3) { Star[i].X = rand()%SCREEN_WIDTH; Star[i].Y = 0; Star[i].Color = BLUE; Star[i].Speed = 1; } // medium speed stars else if (i < MAX_NUM_STARS/2) { Star.X = rand()%SCREEN_WIDTH; Star[i].Y = 0; Star[i].Color = RED; Star.Speed = 3; } // fast stars else { Star[i].X = rand()%SCREEN_WIDTH; Star.Y = 0; Star[i].Color = WHITE; Star[i].Speed = 5; }}...// move each star and check boundaryfor (i = 0; i < MAX_NUM_STARS; i++){ Star[i].Y += Star[i].Speed; if (Star[i].Y > SCREEN_HEIGHT) { if (i < MAX_NUM_STARS/3) { Star[i].X = rand()%SCREEN_WIDTH; Star[i].Y = 0; } // medium speed stars else if (i < MAX_NUM_STARS/2) { Star.X = rand()%SCREEN_WIDTH; Star[i].Y = 0; } else { Star[i].X = rand()%SCREEN_WIDTH; Star.Y = 0; } }}[source]done.

##### Share on other sites
Stupid question, maybe I should relearn HTML but how do you guys get those code boxes( the ones used to show code to others on THIS site) that are formated and color coded?

D.V.

##### Share on other sites
Maybe i''m still a bit too green, but i''ve been using bitmaps to achieve tha same effect. Works fine for me.

Just copy the bitmap image over to the surface, assuming it''s not sitting exactly on the screen (which it''s invariably not) i just copy bits of the image over in 2 steps. first filling the top of the surface (until we reach the bottom of the image) then filling the surface again but starting where we left off before.

each turn the image position is incremented by one creating the scrolling effect.

geddit? It''s like using a cyclic buffer but with a surface.

hope i haven''t confused you further. I''ll post code for it if you want to get a better idea of how it works.

later,
zipless