#### Archived

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

# Levels in side scrollers

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

## Recommended Posts

Ok, for the past month and a half I''ve been working on my side scrolling shooter ''Space Blasters''. I''ve got all the basic stuff done, enemies, collision, sprites, etc. But now, I''m having a bit of trouble with loading different levels. Each level would include - The enemies - Music - A boss (sub-bosses maybe) - A different background - Different bonuses - A set distance (the distance is used to calculate when the [sub]boss appears What would be the best way to load all those for the next usable level? I was thinking of using some sort of struct to hold the data and have a function to initialize them all but I figured I''d ask here first to get some confirmation.
VG-Force |Ekim Gram Productions

##### Share on other sites
Yes, a struct of some sort would definately be the best way to go, and you should probably load the data from a file so you don't have to recompile every time you want to change one little thing. Of course, that makes the data more vulnerable to change by users, which can be a good or a bad thing, depending on what you want. For example:
struct Level{	int numEnemies;	Enemy enemies[MAXENEMIES];	int music;	int boss;	int background;	Bonus bonuses[MAXBONUSES];	int distance;};vector<Level> g_levels

And then you might have a data file that looks like:

NUMLEVELS=5

LEVEL 1
NUMENEMIES=5
ENEMY1=3
...

Of course you can get rid of all the text (making it much easier to read in) and leave it as raw numbers. You could also use a binary file instead. If you were so inclined, you could even encrypt it.

[edited by - aprosenf on March 22, 2004 10:57:20 PM]

##### Share on other sites
I''m really curious; how do you write and read binary files as oppose to ascii files?

##### Share on other sites
@Ekim_Gram - I''d say that a struct or class to hold the level data would be a good idea. If you make a level a class, then it could have methods to load the new level in and could even affect things based on conditions. Like the irritating thing that steals your options in Gradius III. It only shows up if you have 4 options. Your level class could, for example, keep track of the player''s progress and if certain criteria are met, it could make changes. This could be used to adjust the difficulty to the player''s ability or could be used to have branching levels rather than linear progress through the game. Maybe secret levels?

This is off the topic, but I''m hoping to do a side-scrolling shooter in the near-ish future. I was wondering if you could maybe tell me how your levels are loaded and such? For example, how do you store the background image(s)? Is it one long image or is it broken up into smaller images? How do you store the spawn points of your enemies?

Also, how do you do collision detection with the ground (assuming that you HAVE a ground)? For example, in Gradius III, you can crash into mountains above and below you. If you have levels like that, how do you handle the collision detection?

@Unsuspected - I''ve not managed to do it myself yet, but I think it has something to do with how your file reading objects are set up. Your file stream "thingies" have to be set to work in binary. I think. I''d like to know more about that myself...

##### Share on other sites
quote:
Original post by Unsuspected
I''m really curious; how do you write and read binary files as oppose to ascii files?

##### Share on other sites
Well, that link was for C and I see that there''s more than one curious person. Note, for starters, that the notions of ASCII and binary do not exist under Unix: in that case, a file is a file. (Can''t help you with Mac.)

Rather than using the stream insertion and extraction operators (or getline), use the std::ostream:write and std::istream::read methods. They take the address of the destination buffer (a pointer, usually a pointer to char because char is the system byte size) and the size/length of data to read:
#include <fstream>#include <iostream> int main(){  using namespace std;   ifstream fin("input.bin");  ofstream fout("output.bin");  char data[1024];  fin.read(&data[0], 1024 * sizeof(char));  fin.close();  fout.write(data, 1024 * sizeof(char));  fout.close();   return 0;}

##### Share on other sites
Thanks a lot! Can''t wait to try it out.