Skip to main content
gamedev.net
🔒 Locked

Scrolling background algorithm?

Started by Ekim_Gram Mar 6, 2004 at 4:46 PM 0 replies 700+ views
Original Post
Ekim_Gram
Ekim_Gram
Alright, I''ve been working on a 2D space shooter for the past 3 weeks, and now I''ve come to the point where I have to initialize the images. As you all know...static backgrounds just to bode well in the gaming community and I was wondering if you guys could help me with one.
R.I.P. Mark Osback Solo Pa Mi Gente VG-Force Ekim Gram Productions
peter_b
peter_b
I assume its scrolling tiles you want to make, since scrolling a image is simply a matter of increasing its screen coordinates.

In any case, if it is tiles you want, that is easy.
First you make a map, like this.

int tilemap[60][20];

assuming that each tile is 50 pixels wide, it will be about three screens "big" when scrollin along the x-axis.

So whats next? Well you need to fill your tilemap with something, you probebly want to load that info from a file, but for now we just generate one. Like this:

for(int x = 0; x < 60; x++)
for(int y = 0; y < 20; y++)
tilemap[x][y] = rand()%3;

this will fill the map with the numbers 0-2, which gets to represent diffrent tile-images you have loaded.

So whats next now then? Well we need to draw the thing. Here goes.

for(int x = 0; x < 20; x++) // or as wide as you need to fill your screen
for(int y = 0; y < 20; y++)
Blit(tileimage(tilemap[xoff+x][yoff+y], x*50, y*50, x*50+50, y*50+50); // assuming a tile is 50 pixels wide.

That will draw the tiles, should cover most of the screen in normal resolutions, you get to fintune the number of rows and columns yourself.

And now, how do we scroll this monster?
Maybe you noticed those strange xoff and yoff variables above? Thats right, we will use them.
Like this:
If( PressedLeftArrow() )
xoff--;
If( PressedRightArrow() )
xoff++;


And there you have it! I think, its writte out of my head and probebly not correct but you get the idea.

Shields up! Rrrrred alert!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.