12 Replies - 1587 Views - Last Post: 18 November 2012 - 10:06 AM Rate Topic: -----

#1 78jimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-November 12

Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 08:52 AM

I am making a game for a school project with gotoxy and getch and 2d character arrays. there is supposed to be a robot type figure that can move left and right by pressing 'a' and 'd'. so far i've been able to make the cursor go left or right on the grid but can't make a charactar appear at its place. my code,

#include <iostream.h>
#include<conio.h>
#include <windows.h>


//	char moveR;
//	char moveL;
	int mx=30;
	int my=10;
	int i, j;
	char direction;
	const char left_key='a', right_key='d';
	char keypress;
	char a[24][79];

	
	void gotoxy(short x, short y)
	{
      HANDLE hConsoleOutput;
      COORD Cursor = {x,y};
      hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
      SetConsoleCursorPosition(hConsoleOutput, Cursor);
	}
	
/*	void loadgrid()
	{     
		for (i=0; i<24; i++)
		{
			for (j=0; j<79; j++)
            {
                 a[i][j]='.';
			}
		}
	}*/

	void printgrid()
	{
      for (i=0; i<24; i++)
      {
            for (j=0; j<79; j++)
            {
                  cout<<a[i][j];
            }
            cout<<endl;
      }
	}

	void main()
	{
		
/*	HANDLE hOut;

	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	SetConsoleTextAttribute(hOut, BACKGROUND_BLUE);*/
	

	//loadgrid();
	printgrid();

	while (mx>0 && my>0)
	{
      if (kbhit())
      {
            keypress=getch(); //keypress=(char)getchar()
            if ((keypress == right_key) || (keypress == left_key))
                 direction = keypress;
			if (direction == 'a')
                  {
                  gotoxy(mx,my);
                  cout<<"@";
                  mx=mx-1;
                  gotoxy(mx,my);
                  cout<<"@";
                  gotoxy(mx,my);
                  }
			if (direction == 'd')
                  {
                  gotoxy(mx,my);
                  cout<<" ";
                  mx=mx+1;
                  gotoxy(mx,my);
                  cout<<"@";
                  gotoxy(mx,my);
                  }
                  direction = ' ';
      }
	}
				


	//	cout << "NOTE: MAKE SURE ALL ENTERED LETTERS ARE LOWER CASE!!!!!!!";
		
		

}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with 2d arrays and gotoxy()

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 11:15 AM

You want to erase the character here :

71	                  cout<<"@";




Changing your code slightly, you could have sections to erase the old character, calculate the new position and display the new character.
   // ...
        // erase ch
        gotoxy(mx, my);
        cout << " ";

        // calculate position
        if (direction == 'a')
        {
          mx=mx-1;
        }
        if (direction == 'd')
        {
          mx=mx+1;
        }
         
        // display character
        gotoxy(mx, my);
        cout << "@";
        gotoxy(mx, my);



Was This Post Helpful? 1
  • +
  • -

#3 78jimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-November 12

Re: Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 11:28 AM

yes that works. but i have to make a robot type figure out of characters and it has to move, how can i alot them different coordinates
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 11:47 AM

You could do it with just functions, but you could also use a robot-sized array or two.

So you could create a function to print an array at a position. Also you could use a blank array to delete the characters on screen, or use another function.

How complicated is the movement of the robot?
Was This Post Helpful? 0
  • +
  • -

#5 78jimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-November 12

Re: Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 11:53 AM

View Post#define, on 17 November 2012 - 11:47 AM, said:

You could do it with just functions, but you could also use a robot-sized array or two.

So you could create a function to print an array at a position. Also you could use a blank array to delete the characters on screen, or use another function.

How complicated is the movement of the robot?

It just has to move left and right
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 12:11 PM

Whatever you decide you will need a function that takes an x an y position. You could initially try printing a 2x2 block in a function.
Was This Post Helpful? 0
  • +
  • -

#7 78jimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-November 12

Re: Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 12:34 PM

View Post#define, on 17 November 2012 - 12:11 PM, said:

Whatever you decide you will need a function that takes an x an y position. You could initially try printing a 2x2 block in a function.

could you give an example of how that's done
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 02:27 PM

Show the function you have so far.
Was This Post Helpful? 0
  • +
  • -

#9 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Need help with 2d arrays and gotoxy()

Posted 17 November 2012 - 08:43 PM

This is not the same, but shows how a robot can move across a console field, properly.

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <windows.h>

#define VMAX 3
#define HMAX 3

void printRobot(char robot[][3],int x, int y);
void fillIn(int x,int y); 

int main(void) {
   int i,x,y;
   clock_t pause = 50;
   char robot[VMAX][HMAX]={
      {'.','o','.'},
      {'-','¦','-'},
      {'/',' ','\\'}
      };

   printf("Say I want this 'robot', to get animated\n\n");

   for(i=0;i<304;i++)
      printf("%c%c%c%c%c",'.','.','.','.','.');

   x=2;y=6;
   for(i=0;i<108;i++) {
      _gotoxy(x,y);
      printRobot(robot,x,y);
      Sleep(pause);

      fillIn(x,y);
      x+=1;  
      if(x>75){
         x=2;
         y+=VMAX;
      }
      if(i>95) 
         ++y;
         
      pause+=3;
   }
   x+=10; y+=VMAX;
   _gotoxy(x,y);
   printRobot(robot,x,y);
   _gotoxy(1,23);
   printf("\n");
   return 0;
}
void printRobot(char robot[][3],int x, int y) {
   int r,c;
   
   for(c=0,r=2;c<HMAX;c++) 
      printf("%c",robot[r][c]);
   _gotoxy(x,y-1);
   for(c=0,r=1;c<HMAX;c++)   
      printf("%c",robot[r][c]);
   _gotoxy(x,y-2);
   for(c=0,r=0;c<HMAX;c++)
      printf("%c",robot[r][c]);
}
void fillIn(int x,int y) {
   int i;
   for(i=VMAX;i>-1;i--) {
      _gotoxy(x,y-i);
      printf("...");
   }
}



That's a VERY good looking robot, if I do say so! ;)/>/>/>

This post has been edited by Adak: 17 November 2012 - 10:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 78jimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-November 12

Re: Need help with 2d arrays and gotoxy()

Posted 18 November 2012 - 02:31 AM

View PostAdak, on 17 November 2012 - 08:43 PM, said:

This is not the same, but shows how a robot can move across a console field, properly.

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <windows.h>

#define VMAX 3
#define HMAX 3

void printRobot(char robot[][3],int x, int y);
void fillIn(int x,int y); 

int main(void) {
   int i,x,y;
   clock_t pause = 50;
   char robot[VMAX][HMAX]={
      {'.','o','.'},
      {'-','','-'},
      {'/',' ','\\'}
      };

   printf("Say I want this 'robot', to get animated\n\n");

   for(i=0;i<304;i++)
      printf("%c%c%c%c%c",'.','.','.','.','.');

   x=2;y=6;
   for(i=0;i<108;i++) {
      _gotoxy(x,y);
      printRobot(robot,x,y);
      Sleep(pause);

      fillIn(x,y);
      x+=1;  
      if(x>75){
         x=2;
         y+=VMAX;
      }
      if(i>95) 
         ++y;
         
      pause+=3;
   }
   x+=10; y+=VMAX;
   _gotoxy(x,y);
   printRobot(robot,x,y);
   _gotoxy(1,23);
   printf("\n");
   return 0;
}
void printRobot(char robot[][3],int x, int y) {
   int r,c;
   
   for(c=0,r=2;c<HMAX;c++) 
      printf("%c",robot[r][c]);
   _gotoxy(x,y-1);
   for(c=0,r=1;c<HMAX;c++)   
      printf("%c",robot[r][c]);
   _gotoxy(x,y-2);
   for(c=0,r=0;c<HMAX;c++)
      printf("%c",robot[r][c]);
}
void fillIn(int x,int y) {
   int i;
   for(i=VMAX;i>-1;i--) {
      _gotoxy(x,y-i);
      printf("...");
   }
}



That's a VERY good looking robot, if I do say so! ;)/>/>/>/>

i just can't make the characters of the robot to appear instead of '@'
Was This Post Helpful? 0
  • +
  • -

#11 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Need help with 2d arrays and gotoxy()

Posted 18 November 2012 - 03:14 AM

You need a function like "printRobot"! That's why I made that program, for you. I knew I'd never be able to explain it well enough, without an example.

Remember how a scanner on a monitor works - top to bottom, and in horizontal "rows". So, if you notice in "printRobot", I don't start printing the robot until the background has already been printed all the way to to the bottom (last) row that the robot's "foot" will be on. Then I print out the rows up above, row by row, to print out the robot.

If I started printing the robot BEFORE the background was printed, then the robot would simply be overwritten in the blink of an eye, and I'd never see it --- sound familiar??

Everything above the robot's foot gets printed FIRST, and everything to the left of the robot's foot gets printed FIRST -- THEN and only then, does the robot array get printed.
Was This Post Helpful? 0
  • +
  • -

#12 78jimm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-November 12

Re: Need help with 2d arrays and gotoxy()

Posted 18 November 2012 - 07:00 AM

how can i make the program from closing when the robot reaches the boundaries? my code now is,


#include <iostream.h>
#include<conio.h>
#include <windows.h>


int mx=30;
int my=10;
int i, j;
char direction;
const char left_key='a', right_key='d';
char keypress;
	
char grid[24][79];

	
void gotoxy(short x, short y)
{
     HANDLE hConsoleOutput;
     COORD Cursor = {x,y};
     hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
	

void printgrid()
{
     for (i=0; i<24; i++)
     {
		for (j=0; j<79; j++)
        {
			cout<<grid[i][j];
        }
        cout<<endl;
     }
}

#define VMAX 3

#define HMAX 3

 

void printRobot(char robot[][3],int x, int y);

void fillIn(int x,int y);

int main()
{
	int x,y;

//   clock_t pause = 50;

   char robot[VMAX][HMAX]={

      {'.','o','.'},

      {'-','','-'},

      {'/',' ','\\'}

      };

	
		
	HANDLE hOut;

	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	//SetConsoleTextAttribute(hOut, BACKGROUND_BLUE|
//BACKGROUND_INTENSITY);
	SetConsoleTextAttribute(hOut, BACKGROUND_GREEN|
    BACKGROUND_INTENSITY);
	

	//loadgrid();
	printgrid();
	gotoxy(mx,my);
    printRobot(robot,x,y);

	while (mx<70 && my<20 && my>0 && mx>0)
	{
      if (kbhit())
      {
            keypress=getch(); //keypress=(char)getchar()
            
			if ((keypress == right_key) || (keypress == left_key))
                 direction = keypress;
			
			
			if (direction == 'a')
                 {
                  gotoxy(mx,my);
                  cout<<" "<<flush;
                  
				  mx=mx-1;
                  gotoxy(mx,my);
                  
				  printRobot(robot,x,y);
				  fillIn(x,y);
                  gotoxy(mx,my);
                  }
			
			
			if (direction == 'd')
                  {
                  gotoxy(mx,my);
                  cout<<" "<<flush;
                  
				  mx=mx+1;
                  gotoxy(mx,my);
                  printRobot(robot,x,y);
                  fillIn(x,y);
				  gotoxy(mx,my);
                  }
			
                  
			direction = ' ';
      }
	}
	
				


	//	cout << "NOTE: MAKE SURE ALL ENTERED LETTERS ARE LOWER CASE!!!!!!!";
		
		
return 0;
}



		
		
void printRobot(char robot[][3],int x, int y) {

   int r,c;

    

   for(c=0,r=2;c<HMAX;c++)

      cout<<robot[r][c]<<flush;

   gotoxy(x,y-1);

   for(c=0,r=1;c<HMAX;c++)  

      cout<<robot[r][c]<<flush;

   gotoxy(x,y-2);

   for(c=0,r=0;c<HMAX;c++)

      cout<<robot[r][c]<<flush;

}

void fillIn(int x,int y) {

   int i;

	 for(i=VMAX;i>-1;i--) {

      gotoxy(x,y-i);
      cout<<"   "<<flush;

   }

}

		
		
		
		

	

	


Was This Post Helpful? 0
  • +
  • -

#13 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Need help with 2d arrays and gotoxy()

Posted 18 November 2012 - 10:06 AM

Change your while(testCondition) to

keypress=='a';
while(keypress != 'q') {
   //get the next keypress, and move the robot
   //accordingly, in here
}



It's usually best to keep the robot away from the edges of the field. The robot is more than one char wide, so you could easily get the robot with part of his body on the far edge, and the other part on the other edge.

btw, x and y variables should never reach 0 - lowest number is 1.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1