Very Simple Question (need help - 2D arrays)

Started by
3 comments, last by Samdil 23 years, 2 months ago
I am currently starting a new programming project. It''s going to be a text-based meld of RTS/town simulator. Now, here''s my question. I know how to use 2D arrays a bit, but I haven''t been able to get mine working. My reference books don''t seem to help me (mostly since my problem is with function calls). Can I see what you all would write (very basically) for a 10x10 2D array and allow the person to move between the tiles? I''m not sure where my problem lies. I''d like to see an example of something similar to what I have so I can find out where I went wrong. Thanks in advance. --Samdil
Advertisement
Here''s some very simple tile based [pseudo-]code. It doesn''t cover loading of maps, objects on the ground, scrolling of the screen, initialization (general, or with any API), or anything seriously complex (I''m writing this with about 5 minutes of time, heh). It also doesn''t show you the best coding habits, but I''m just meaning it to get the point across:

  #define TILE_WIDTH 10#define TILE_HEIGHT 10#define TILE_PIX_WIDTH 50#define TILE_PIX_HEIGHT 50// We''ll assume that the character is the same size as a tile =P/* Character info */struct Player {  long x, y;  /* Insert Image Stuff Here */};/* Will hold information about whether or not a tile is able to be walked through. If collide equals true then you cannot walk through it */struct TileInfo {  bool collide;  /* Insert Image Stuff Here */};/* Holds a "palette" of tiles, and 10x10 references to them. You should of course make it dynamic for better usability and efficiency */struct Map {  TileInfo TI[256];  unsigned char tiles[TILE_WIDTH][TILE_HEIGHT];}Map WorldMap;Player PlayerOne;bool KeyBoard[256]; // Init all to false in your code// Outlines a really cheesy map rendering ruitinevoid RenderMap(void) {  /* Clear Screen */  for(int a=0; a<10; a++) {    for(int b=0; b<10; b++) {      /* Draw tile image WorldMap.TI[WorldMap.tiles[a]] at<br>         x=a*TILE_PIX_WIDTH by y=b*TILE_PIX_WIDTH */<br>    }<br>  }<br>  /* Render Character at x=PlayerOne.x by y=PlayerOne.y */<br>}<br><br><br>// Tests for rectangular collision<br>bool RectRect(const RECT &a, const RECT &b) {<br>  if(a.left&gt;b.right) return false;<br>  if(a.right&lt;b.left) return false;<br>  if(a.top&gt;b.bottom) return false;<br>  if(a.bottom&lt;b.top) return false;<br>  return true;<br>}<br><br>// Handles movement of a character, tests for collision<br>void MoveCharacterDir(long x, long y) { // Amount to move char.<br>  RECT play_rect;<br>  RECT tile_rect;<br>  SetRect(&play_rect,PlayerOne.x+x,PlayerOne.y+y,PlayerOne.x+TILE_PIX_WIDTH+x,PlayerOne.y+TILE_PIX_HEIGHT+y);<br>  for(int a=0; a&lt;10; a++) {<br>    for(int b=0; b&lt;10; b++) {<br>      SetRect(&tile_rect,a*TILE_PIX_WIDTH,b*TILE_PIX_HEIGHT,(a+1)*TILE_PIX_HEIGHT,(b+1)*TILE_PIX_WIDTH)<br>      if(RectRect(play_rect,tile_rect) return;<br>    }<br>  }<br>  PlayerOne.x += x;<br>  PlayerOne.y += y;<br>}<br><br>// A fake Windows message handler (very bad impression, I know)<br>SUMTIN HandleKeyboard(SOMEMSG Msg, SOMETHING Key) {<br>  switch(Msg) {<br>    case SOME_MSG_KEY_DOWN:<br>      KeyBoard[Key] = true;<br>      break;<br>    case SOME_MSG_KEY_UP:<br>      KeyBoard[Key] = false;<br>      break;<br>    default:<br>     return DoDefaultDeally();<br>     break;<br>}<br><br>// A really pitiful game-loop, the manager of your game<br>void GameLoop(void) {<br>  if(KeyBoard[SOME_LEFT_KEY]) MoveCharacterDir(-5, 0);<br>  else if(KeyBoard[SOME_RIGHT_KEY]) MoveCharacterDir(5, long y);<br>  else if(KeyBoard[SOME_UP_KEY]) MoveCharacterDir(0, -5);<br>  else if(KeyBoard[SOME_DOWN_KEY]) MoveCharacterDir(0, 5);<br>  RenderMap();<br>  /* Do something with HandleKeyboard here */</font><br>}<br>  </pre></font></td></tr></table></center><!–ENDSCRIPT–><br>Ask as many questions as you''d like, I know that I left out a ton of stuff, but I was trying to provide a simple framework. I stress that this is horrible, non-functional pseudo-code, but I hope it gives you an idea. Good luck. Pray that someone posts some better code, and that mine doesn''t confuse you… heh <img src="tongue.gif" width=15 height=15 align=middle>    <br><br><HR WIDTH=100%><br><A href="http://www.gdarchive.net/druidgames/">http://www.gdarchive.net/druidgames/</a><br>    
Well, that''s not quite was I was looking for, since I''m just doing a VERY simple *text* game. But, I think I have it working now. I''ll post an update after I''ve finished with the adjustments.

Thanks for the help, however.

--Samdil
Well, it works, but not the way I''d like.

If someone could post a sample to compare, that''d be great. Thanks in advance.

--Samdil
OK, I decided to post my source so far and explain the problem.

I can get this to compile and link and execute fine, but when I move, the coords update only the Y value, and only in the positive direction. Example: start 0,0; input N - 0,1; input E - 0,2; input S - 0,3; input W - 0,4 etc.

Again this is just a simple program so far; I want to get this main part working before I expand on it. Source:

    #include <iostream.h>#include <conio.h>//Prototypesvoid clrscrn();int moveChar(char);//Global Variablesint px; //player X and Y coordinatesint py;char provOwner; //stores owner of the provinceint provinces[10][10] ={2, 1, 1, 1, 2, 4, 3, 3, 1, 4,						2, 2, 1, 1, 2, 2, 3, 3, 3, 4,						2, 2, 2, 3, 3, 2, 2, 4, 3, 3}; //numbers for test purposes onlychar input, fInput; //player input and proxy variable for function moveChar()struct kings {	char name;} p[4]; //this is here because eventually this will expand to hold all variables for each rulerint sCheck; //checks if input was validint i; //loop countervoid main() {	//Get player name, store in their structure	cout << "What is your name?\n";	cin >> p[0].name;	clrscrn();		do {		//Assign province owner names		if(provinces[px][py] == 1) {			provOwner = p[0].name;		}		if(provinces[px][py] == 2) {			provOwner = p[1].name;		}		if(provinces[px][py] == 3) {			provOwner == p[2].name;		}		if(provinces[px][py] == 4) {			provOwner == p[3].name;		}		//Tells where player is		cout << "You are in " << provOwner << "'s kingdom. -- " << px << "," << py << "\n\n";		//Input on direction to go (no restrictions yet on going "out of bounds")		cout << "Go where?\n\n";		cout << "(N)orth, (S)outh, (E)ast, (W)est\n";		do { //Get input until valid entry is inputted			cin >> input;			sCheck = moveChar(input);		} while(sCheck == 0);		clrscrn();	} while(input != 'exit'); //Loop while user doesn't input 'exit'/* DOESN'T WORK - still here in case I need it later; it keeps asking for input when I use this  switch(input) {		case 'N':		case 'n':			py++;			sCheck = 1;		break;		case 'E':		case 'e':			px++;			sCheck = 1;		break;		case 'S':		case 's':			py--;			sCheck = 1;		break;		case 'W':		case 'w':			px--;			sCheck = 1;		break;		default:		break;	}}*///Function seems to work, but not as intended; actually breaks input loopint moveChar(char fInput) {	if(input == 'N' | 'n') {		py++;		return 1; 	} else { 		if(input == 'S' | 's') {			py--;			return 1;		} else {			if(input == 'E' | 'e') {				px++;				return 1;			} else {				if(input == 'W' | 'w') {					px--;					return 1;				} else {					return 0;				}			}		}	}}//Clear scren :)		void clrscrn() {	for(i=0; i<25; i++) {		cout << "\n";	}	i=0;}    


Edited by - Samdil on February 1, 2001 2:32:23 PM

This topic is closed to new replies.

Advertisement