#### Archived

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

# Scrolling background algorithm?

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

## Recommended Posts

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

##### Share on other sites
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.

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633405
• Total Posts
3011681
• ### Who's Online (See full list)

There are no registered users currently online

×