Jump to content
  • Advertisement

Archived

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

Ekim_Gram

Scrolling background algorithm?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!