Looking for some input on gamemap

I made this game map for a 2D side-scroller. It runs fine, but I'd like some input on programming style and maybe some helpful hints since I'm new to this. Although it may not be apparent I'm trying to focus on code clarity.

// main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <fstream>		// file I/O suppport
#include <cstdlib>		// support for exit()

using namespace std;

void loadMap(int mapXY[][100]);
void formatMap(int mapXY[][100]);
void drawMap(int const mapXY[][100]);
void saveMap(int const mapXY[][100]);

int xMapMax = 10;		// left to right
int yMapMax = 10;		// top to bottom
char mapFileName[20] = "map.txt";

int main()
{
	
	int map[100][100];	// map[x][y]
	
	/* uncomment lines 24 and 25 to create a game board
	file if you didn't copy map.txt to your computer */

	//formatMap(mapXY);
	//saveMap(mapXY);
	
	loadMap(map);
	drawMap(map);
	
	//saveMap(map); 	// uncomment to save map before quit
	
	return 0;
}

void formatMap(int mapXY[][100])
{
	cout << "Formatting map..\n";
	
	for (int y = 0; y < yMapMax; y++)
	{
		for (int x = 0; x < xMapMax; x++)
		{
			mapXY[x][y] = 0;
			//cout << mapXY[x][y];
		}
		//cout << endl;
	}
}

void loadMap(int mapXY[][100])
{
	cout << "Loading map..\n";
	
	ifstream mapFile;        	// object for handling file input
	mapFile.open(mapFileName);	// associate mapFile with a file
	if (!mapFile.is_open())  	// failed to open file
	{
		cout << "Could not open file: " << mapFileName
		<< "\nExiting program.\n";
		exit(EXIT_FAILURE);
	}
	
	mapFile >> xMapMax; 		// retrieve max x map range
	mapFile >> yMapMax; 		// retrieve max y map range
	
	int value;
	int x = 0;
	int y = 0;
	
	while ((mapFile >> value) && mapFile.good())
	{
		
		mapXY[x][y] = value;	// x = horizonatal, y = vertical
		//cout << map[x][y];
		
		++x;
		if (x==xMapMax)	// start new row when x at end
		{
			//cout << endl;
			x = 0;		// reset x
			y++;		// add a y row
		}
	}
	
	mapFile.close(); 	// finished with the file
}

void drawMap(int const mapXY[][100])
{
	cout << "Drawing map..\n\n";
	
	for (int y = 0; y < yMapMax; y++)
	{
		for (int x = 0; x < xMapMax; x++)
		{
			cout << mapXY[x][y];
		}
		cout << endl;
	}
}

void saveMap(int const mapXY[][100])
{
	cout << "Saving map..\n";
	
	ofstream outFile;			// create object for output
	outFile.open(mapFileName);	// associate with a file
	
	outFile << xMapMax << endl; // save xMapMax and yMapMax
	outFile << yMapMax << endl; // at beginning of file
	
	for (int y = 0; y < yMapMax; y++)
	{
		for (int x = 0; x < xMapMax; x++)
		{
			//cout << mapXY[x][y];
			outFile << mapXY[x][y] << " ";
		}
		//cout << endl;
		outFile << endl;
	}
	
	outFile.close();
}


// map.txt (or uncomment lines 24 and 25 to make a new map instead)
1
2
3
4
5
6
7
8
9
10
11
12
10
10
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 2 2 1
1 0 2 2 2 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 2 0 0 1
1 0 0 0 0 2 2 2 0 1
1 1 1 1 1 1 1 1 1 1



Suggestions welcome. Feel free to use this code.
Hi
Well it compiles OK and runs and obviously does what you wanted it to, so that's usually a good start. I'll admit I don't understand some of your syntax despite 15 odd years of C++ but then again I hardly use pointers and arrays at all nowadays (- wonders of STL!).
I think the next step would be to package this up in a class, with all the implementation details hidden away and a nice clean public interface for clients. So you can do things like:

1
2
Map my_map( "map_file.txt" );
my_map.draw();


Also how flexible do you think this code is? What if I had a map with dimensions 200*180 - could your program handle that? And if not, why not? :)
Regards, keineahnung

*Edit*
Oh yeah and don't use using namespace std;
Preface those functions with std:: instead, e.g. std::cout
It looks cooler and makes your code WAY safer ;)
Last edited on
I'll admit I don't understand some of your syntax despite 15 odd years of C++

Huh? It's not like he used anything particularily complicated.


You might want to drop the C-style programming and structure your program in classes, and try to avoid pointers/arrays whenever feasible.
Also how flexible do you think this code is? What if I had a map with dimensions 200*180 - could your program handle that? And if not, why not? :)
Regards, keineahnung


If I changed the size of the array then it could handle it, but as it is I'm not sure if I can pass a multidimensional array through a function while allowing the number of elements to be changed.

I'm not going to use std:: right now as I'm new and it's a little tedious, but I will definitely implement it for the final game version if this ever gets finished.

As for using a class, I haven't much about them at all yet, but was planning to keep the map as a function. I want to add the enemies, players (yes, this will be a multiplayer game), and moving game objects as classes.



I just dropped this piece of code in the loadMap function that enables the labeling of variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	// extract game variables from file
	// (ignore notes and unused variables)
	
	char ch;
	int i = 0;
	do
	{
		do 
		{
			mapFile >> ch;
		}
		while (ch != ':' && ch != '!');

		switch (i)
		{
			case 0:
				mapFile >> xMapMax; 	// max x map range
				break;
			case 1:
				mapFile >> yMapMax; 	// max y map range
				break;
		}
		i++;	// needed to cycle the case variables above
	}
	while (ch != '!');

...


This allows the map.txt file to be more like this:

// map.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
map x range (max): 10   - the map below needs to match this
map y range (max): 10
extra unknown variable for later: 10
another unknown variable: 10

Notes, and more notes here.

Variables must be in correct order for program
to run properly. Do not erase this message!


1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 2 2 1
1 0 2 2 2 4000000 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 2 0 0 1
1 0 0 0 0 2 2 2 0 1
1 1 1 1 1 1 1 1 1 1



What's the deal with pointer arrays? The reason why I thought they would be useful is because they are extremely fast and they are so easy to quickly manipulate (though could be a bad thing too I guess).

What would give me this same ability to easily extract game tiles, draw them, and test for collisions? I want the game board to be able to be manipulated very easily in many ways, such as turning the game map upside down.
std::vector. Little to no performance overhead, but far simpler and less errorprone in their usage. Also resizable on runtime.

You would really be better off if you learned how to make use of the power of C++ before throwing yourself heads first into a game.
You would really be better off if you learned how to make use of the power of C++ before throwing yourself heads first into a game.


And how do you propose he learn more without actually coding something?

If he's not throwing himself heads first into a game, he'll be throwing himself heads first into some other kind of program. Either way he'll have to overcome the same hurdles to complete it.

At least with this project he'll have fun in the process. Plus he'll also learn how to apply the topics he's learning to what he actually wants to do.
Last edited on
I can only speak from my experiences, in which kept starting projects that were completely over my head which would only lead to me ending up aborting them in frustration. Don't get my wrong, I'm all for learning by doing - but for that to work you have to have a general understanding of what there is to learn and what you can do first. Of course you are free to disagree with me (I am sure you are already doing this right now), but that's my take on it.
hanst99 wrote:
in which kept starting projects that were completely over my head which would only lead to me ending up aborting them in frustration


keineahnung wrote:
Well it compiles OK and runs and obviously does what you wanted it to


Doesn't sound like he's in over his head to me.

I agree that if people try to bite off more than they can chew it's a good idea to tell them to back off. I just don't see that being the case here.

Basically what I saw was him starting a thread posting working, functional code (that's even in pretty good form for a beginner) and being greeted with "don't make a game, it's too hard for you". I guess that just irked me a bit.


I'm all for learning by doing - but for that to work you have to have a general understanding of what there is to learn and what you can do first.


This is another chicken before the egg thing. How can you know what there is to learn without actually learning it? How can you know what you can do without actually doing it?

The best only way, IMO, is to just get your hands dirty. The trick to avoiding frustration is to know when you're in too deep.


But yeah I guess we just have a different opinion on this one. =)
Well, a simple side-scroller isn't particularly hard. It can be done even without knowing the STL or C++ in general very well, it's just that it will take much longer and involve much more debugging. But that's only partially a bad thing.
Basically what Disch said.
Last edited on
I didn't say that it was over his head, I said I did stuff that was over my head. Well, I guess it kinda sounded like I was extending that to him as well but that wasn't the intention.

I'm not going to use std:: right now as I'm new and it's a little tedious


That was the line that led me to my post, it's just that I think one should seek to learn about the tools one uses and the rich standard library is for me one of the core features of C++. Anything else may - and probably will - lead him into situations in which he will reinvent something that C++ already provides by default, and probably in a more sophisticated fashion than the reinvention too.
I don't think he meant he isn't going to use the standard library (although I thought that at first, too), but rather that he doesn't want to fully qualify the names (which is okay).
Oh, well in that case I misunderstood. Sorry about that.
guys this is degrading into an argument.
@carebearboy would you mind if I copied your idea for a game I am trying to make?
Thank you all for your concern. I think I could have been a little more clear in what I wanted. Hanst99 has a good point about properly learning the code before jumping into making a game. This is a big thing for me. I want quality code, and at the same time I need to write code to retain that information. I've read about 350 pages of 'C++ Primer Plus' (without hardly any code written) and I figured I could benefit from using some of what I've learned: arrays, pointers, strings, file I/O, functions... I haven't touched on Classes, vectors, etc.., but am excited.

I like Athar and others have a good point as well. I find that I need to be doing something fun or I'm not interested. I cannot force myself to do the exercises in the book because they are so boring to me. If I can involve a new command I've learned into the game then I can dive right into it.

This game is not meant to be a final product. In fact, it's just a fun collaboration of what I've learned on the way combined into a program. When I'm done the book I'll likely start from scratch with the newly acquired skills (although, even reading this entire book would not allow for making a graphical game as I don't think there is a GUI section). It's a long journey. :)

bboy212,

You are free to use/edit/modify the code. This is opensource code as far as I'm concerned. Don't worry about adding me as a credit. I only ask that you 'consider' (give consideration to the thought of / think about) sharing your code with others if they ask you. Perhaps you'll improve upon it, post it on the board, and I'll then I'll want to add those improvements to my game. By that time I may have a character generator and you may want to use it... You get the idea.
Topic archived. No new replies allowed.