|
-
Dec 10th, 2002, 08:32 AM
#1
Thread Starter
Frenzied Member
C++ Game Question
Hi 
I have just finished programming my Breakout game in C++ using DirectX, but the game only has one level....I need some advice in doing the level loading....Should I just create a funtion called LoadLevel and have it take an integer as parameter?? for instance if the user have finished level 1:
PHP:--------------------------------------------------------------------------------
LoadLevel(2); // Load level 2
--------------------------------------------------------------------------------
And then in an a header file write 1000 lines of code to change the position of the blokcs???
Or is there an easier way??(please say there is )
-
Dec 10th, 2002, 08:34 AM
#2
Frenzied Member
Write the positions of the blocks into a file. You can then load the positions of the blocks that way.
Z.
-
Dec 10th, 2002, 08:40 AM
#3
Thread Starter
Frenzied Member
Could you give an example.....
-
Dec 10th, 2002, 09:17 AM
#4
Guru
Reading the data from a file as Zaei recommended is indeed the recommended solution. It is the most extensible and flexible, and definitely much better than hardcoded levels (you don't have to recompile every time you want to change some level).
First you should think of what kind of file format suits your needs. Binary or text-based?
A binary file format is usually much more efficient and less error-prone. However, it is less human-readable.
A text file format is usually less efficient, has more possibilities of invalid data, but anyone can read and change it.
Let's assume you use a text-based format to start with. Text-based formats are (arguably) easier to program, and definitely easier to understand if you aren't familiar with file formats.
Let's say, as an example, that level 1 has 3 bricks:
1) Red, row 2, column 2, worth 20 points
2) Red, row 2, column 6, worth 20 points
3) Blue, row 1, column 4, worth 40 points
And that level 2 has 2 bricks:
1) Green, row 1, column 1, worth 50 points
2) Green, row 1, column 5, worth 50 points
And let's say you decided to put all the levels in one file (say, levels.dat) and to use a text-based format. It might look like this:
Code:
[level1]
brick 255 0 0 2 2 20
brick 255 0 0 2 6 20
brick 0 0 255 1 4 40
[level2]
brick 0 255 0 1 1 50
brick 0 255 0 1 5 50
Explanation:
Lines in this sample file are formatted as:
Code:
<object type> <red value> <green value> <blue value> <row> <column> <points>
In C, it may be easily read, using code like this:
Code:
Num = fscanf(File, " %s %d %d %d %d %d %d ", ObjectType, &Red, &Green, &Blue, &Row, &Col, &Points);
if(Num != 7)
return false;
Using a text-based file, you can easily create levels using nothing more than notepad.
If you used a binary file format, it would be more efficient programatically, but a bit more confusing conceptually, and you won't be able to write levels as easily (you'd have to use a hex editor and have intimate knowledge of the format, or write your own level editor).
The same line as above could be represented in the file like this, for example:
Code:
(WORD) Object type (value from enumeration)
(BYTE) Red
(BYTE) Green
(BYTE) Blue
(WORD) Row
(WORD) Column
(DWORD) Points
Then you could read it like this:
Code:
// Assume P points to the right position in a buffer that contains data read from the file
Type = *(WORD*)P;
P += sizeof(WORD);
Red = *P++;
Green = *P++;
Blue = *P++;
Row = *(WORD*)P;
P += sizeof(WORD);
Column = *(WORD*)P;
P += sizeof(WORD);
Points = *(DWORD*)P;
P += sizeof(DWORD);
It is up to you to decide on a file format suitable for your game.
-
Dec 10th, 2002, 10:53 AM
#5
Thread Starter
Frenzied Member
Uhhh...I use coords to decide where they should be placed, not rows....Here is the code I use to set the blocks:
I have 24 blocks.
Does your code still work here??
PHP Code:
// set position of blocks
// Set 1.st row
// -------------------------------------------------
for (count = 0; count<6; count++){
Set_Pos_BOB(&blocks[count],blockspace,verticalspace);
blockspace = blockspace + blocks[count].width +2;
}
// --------------------------------------------------
blockspace = 20; // increment space between blockses
verticalspace = verticalspace + blocks[count].height; // increment hegiht
// Set 2.nd row
// --------------------------------------------------
for (count;count<12; count++){
Set_Pos_BOB(&blocks[count],blockspace,verticalspace);
blockspace = blockspace + blocks[count].width +2;
}
// --------------------------------------------------
verticalspace = verticalspace + blocks[count].height; // increment space
blockspace = 20; // increment space
// Set 3.rd row
// ---------------------------------------------------
for (count;count<18; count++){
Set_Pos_BOB(&blocks[count],blockspace,verticalspace);
blockspace = blockspace + blocks[count].width +2;
}
// ----------------------------------------------------
blockspace = 20; // space
verticalspace = verticalspace + blocks[count].height; // space
// Set 4.th row
// ----------------------------------------------------
for (count;count<24; count++){
Set_Pos_BOB(&blocks[count],blockspace,verticalspace);
blockspace = blockspace + blocks[count].width +2;
}
// ----------------------------------------------------
Where blockspace is the space between the blocks and verticalspace is the space from the top
The Set_Pos_BOB function
PHP Code:
int Set_Pos_BOB(BOB_PTR bob, // Pointer to BOB
int x, int y); // coords
Last edited by CyberCarsten; Dec 10th, 2002 at 10:56 AM.
-
Dec 10th, 2002, 10:59 AM
#6
Frenzied Member
It still works, just change row and column to blockspace and verticalspace.
Z.
-
Dec 10th, 2002, 11:00 AM
#7
An alternative file format, as I used it in my breakout, would look like this:
I used the numbers from 0 to 9 and the letters A, B, C and D.
The level looked like this:
Code:
9999999999
8000880008
7077777707
6060000606
5555555555
4444004444
3030330303
ABABAABABA
CC00DD00CC
As you see, it simply was a solid block. The level is always 10*9 bricks large. Every number/letter represents one brick. 0 means no brick, 1 to 9 were different classes of bricks (differing in look and point value). A and B were crackers, bricks that have to be hit twice before being destroyed, C and D were solids, undestructible bricks.
The look and point value of the various bricks were defined in a seperate file, not unlike the file Yonatan showed you.
e.g.
Code:
1 brick1.bmp 100
2 brick2.bmp 200
A cracker1.bmp 500
B cracker2.bmp 800
with the template being
<id> <image file name> <point value>
This was read in a single function and stored as an array of class objects.
The only problem was that I never finished the game
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 10th, 2002, 12:15 PM
#8
Thread Starter
Frenzied Member
So it would look like this:
[level1]
brick xcoord ycoord points
<object type> <x> <y> <points>
Num = fscanf(File, " %d %d %d ", ObjectType, &x, &y, &Points);
if(Num != 3)
return false;
Set_Pos_BOB(&blocks[index], x,y);
??
How can I decide which level to load?
Last edited by CyberCarsten; Dec 10th, 2002 at 12:23 PM.
-
Dec 10th, 2002, 12:48 PM
#9
Frenzied Member
I would just use the format:
Code:
brick 10 10 100
brick 10 20 100
and put each level into a sperarate file.
And your previous code wont work...
Code:
char s[32]; // unsafe
Num = fscanf(File, "%s %d %d %d", c, &x, &y, &Points);
...
Z.
-
Dec 15th, 2002, 08:34 PM
#10
Thread Starter
Frenzied Member
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|