#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!!!!!!!";
}
Need help with 2d arrays and gotoxy()
Page 1 of 112 Replies - 552 Views - Last Post: 18 November 2012 - 10:06 AM
#1
Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 08:52 AM
Replies To: Need help with 2d arrays and gotoxy()
#2
Re: Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 11:15 AM
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);
#3
Re: Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 11:28 AM
#4
Re: Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 11:47 AM
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?
#5
Re: Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 11:53 AM
#define, on 17 November 2012 - 11:47 AM, said:
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
#6
Re: Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 12:11 PM
#7
Re: Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 12:34 PM
#8
Re: Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 02:27 PM
#9
Re: Need help with 2d arrays and gotoxy()
Posted 17 November 2012 - 08:43 PM
#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
#10
Re: Need help with 2d arrays and gotoxy()
Posted 18 November 2012 - 02:31 AM
Adak, on 17 November 2012 - 08:43 PM, said:
#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 '@'
#11
Re: Need help with 2d arrays and gotoxy()
Posted 18 November 2012 - 03:14 AM
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.
#12
Re: Need help with 2d arrays and gotoxy()
Posted 18 November 2012 - 07:00 AM
#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;
}
}
#13
Re: Need help with 2d arrays and gotoxy()
Posted 18 November 2012 - 10:06 AM
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.
|
|

New Topic/Question
Reply



MultiQuote




|