Sign in to follow this  

Side-Scroller Tutorial

This topic is 3857 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

Does anyone know a good side-scroller tutorial in C++ and SDL? I know that's a little specific but hey, that's the point ;). If not, what about just one with an open-source code file. Thank you

Share this post


Link to post
Share on other sites
Well i do not know of any tutorials for such a thing but i do not imagin in it would be to difficult!

Just draw your ship on a scrolling Back Ground and have some enemys spawn in on one side of the screen!

You can use srand to give a random starting point to the enemys.

and when the score reachs a certain point double the speed of the enemy.

to do a scrolling back ground can be tricky but basicaly you make your self a back ground image the same size of the screen works!

and then draw it move it back and just draw from however far back forward of the image and the draw the remainder lets see if i can make my point more clear.

|-------------|
|-------------|
|-------------| < Background when first drawen

Move it so your only draw a few pixels less of the image witch you can do with any
Blit function

|----------||-|
|----------||-|
|----------||-|

and then draw the remainder in any case finding a tutorial for a scrolling back ground should not be to hard!

as for Collsion Detection IE: a bull fired at an enemy
you can use Vector math or just use Rect Collison detection

Hope the helps out abit!

Share this post


Link to post
Share on other sites
I will assume that you are referring to a tile based side scrolling game such as Sonic or Mario.

I am currently working on a top down tile based game and my current implementation uses a “Level” class and a vector of vectors of tile objects to represent the 2d array.

For the map editor I decided to use Mappy and for scrolling I implemented smooth scrolling with help from the article here:

Smooth Scrolling a Tile map

The first step I did with this regard was to take your player position (pX and pY) and then from that calculate the display position (dispX and dispY). I know I wanted to bound the display so:

DispX=min(max(dispX-WinWidth/2,0),TileXCount*TileWidth-WinWidth)
DispY=min(max(dispY-WinHeight/2,0),TileYCount*TileHeight-WinHeight)

Where TileXCount and TileYCount are the number of tiles horizontally and vertically in the level, WinWidth and WinHeight are the dimensions of the display area and TileWidth and TileHeight are the width and height of a single tile.

Then the position on the map is equal to:

MapX=DispX/TileWidth
MapY=DispY/TileHeight

The Map x and y offsets are equal to:

MapXOffset=DispX&(TileWidth-1)
MapYOffset=DispY&(TileHeight-1)

Now the number of tiles that can be displayed across the width of the window and the height is equal to:

WinTileXCount=ceil(WinWidth/TileWidth)+1
WinTileYCount=ceil(WinHeight/TileHeight)+1

The number of tiles to actually draw is:

TileDrawXCount=min(WinTileXCount,TileXCount-MapX)
TileDrawYCount=min(WinTileYCount, TileYCount-MapY)

Then you run a for loop for the TileDrawXCount and TileDrawYCount and to get the position at the array you get:
ArrayX=MapX+LoopX
ArrayY=MapY+LoopY

And the position of the tile is equal to :

DrawX=LoopX*TileWidth-MapXOffset+((WinTileXCount-TileDrawXCount)/2)*TileWidth

DrawY=LoopY*TileHeight-MapYOffset+((WinTileYCount-TileDrawYCount)/2)*TileHeight

And to draw an object on the screen all you need to do is:

PlayerDrawX= pX-DispX
PlayerDrawY= pY-DispY

And to convert from screen coordinates to coordinates in the map you do the reverse:

MapMouseX=MouseX+DispX
MapMouseY=MouseY+DispY

of course the actual tile position would be:

TileMouseX=MapMouseX/TileWidth
TileMouseY=MapMouseY/TileHeight

At least these are the formulas that I used. Some of the formulas I developed on my own and there might be much easier ways to do them ;) .

Of course all of these variables are meaningless without a solid framework. It is a good idea to use an OOP based approach and to not hard code things.

For an example project you could check out Dave Gnukem: Dave Gnukem

I hope this helps you on your path and that it can also help other people.

Share this post


Link to post
Share on other sites

This topic is 3857 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this