• Status: Solved
• Priority: Medium
• Security: Public
• Views: 324

# how would i move a integer in a dos screen and replaceing current character with something else?

In Basic C Language how would i move a integer in a dos screen with the boundrys of :
x=80; /* x being horizontal boundrys so x>0 and x<80
and
y=30;/* y being horizontal boundrys so y>0 and y<30
i'm using #include<conio.h>
so in other words i'm trying to move :
@     ,  over 1 space left right up or down and replace the current position with a *

``````#include<stdio.h>
#include<conio.h>

int main(void)
{
char buffer[4];
int x, x2; /*buffer and coordinates for checking*/
int y, y2; /*buffer and coordinates for checking*/

int m; /* MOVEMENT INPUT*/
int score; /* score display integer*/

.
.
.
.
.
.
gotoxy(x, y);
``````
0
rafsta01
• 11
• 7
• 4
6 Solutions

Author Commented:
#include<stdio.h>
#include<conio.h>

int main(void)
{
char buffer[4];
int x, x2; /*buffer and coordinates for checking*/
int y, y2; /*buffer and coordinates for checking*/

int m; /* MOVEMENT INPUT*/
int score; /* score display integer*/

.
.
.
.
.
.
gotoxy(x, y);

0

Commented:
I assume you are writing a game, where @ is your player and * is a trail?

If using conio, you need to write some wrappers around gotoxy() + putch()

gotoxy(x, y); // move to the coordinate
putch(ch);  // put the character

Doing that, you can put @ at the current location, and put * at the location your player was just at.

So if you are moving UP (+1 in the Y position), then do:

// Draw trail
gotoxy(x, y);
putch('*');

// Draw new player position
gotoxy(x, y+1);
putch('@');

0

Author Commented:
when used in a for loop the cursor doesnt move,
so       scanf("%i",m);
for(m=8; y>0||y<30;y--)
{

gotoxy(x, y);      /* Draw trail*/
putch('*');

gotoxy(x, y+1);      /* Draw new player position*/
putch('@');

}
with the previous code crashes my prototype, very confused why it would do that
0

Commented:
The code does not make sense.

for(m = 8; y > 0 || y < 30; y--)

1) Why do you read into the "m" variable, and then set it to 8 in the for loop. What is y?
2) The loop will execute forever, either y will be greater than 0, or y will be less than 30, or both.
3) What is the initial value of y at the start of the loop?
0

Author Commented:
for(m = 8; y > 0 || y < 30; y--) this is the line which is meant to be for the up movement only (NUM Keys arrows) and it can only move if the y value is greater than 0 and lowet than 30 ie co-ordinates y's initial values are 0 as for x is also 0

i'm really stumped with it...
0

Commented:
You said:

>>if the y value is greater than 0 and lowet than 30

1)  || is OR, not AND, you should use && instead

2)  Why are you setting m = 8 ? You just read input into m on the line before that.

0

Author Commented:
i'm setting m because the m value is meant to be the input from the user as the controlls 8=up, 6=right, 4=left and 2=down
0

Commented:
Nevertheless, it makes no sense to set it as a predicate in the for() loop statement. If you are going to set it, just do it in a normal statement on a separate line, not inside the for().

m = 8;

Please read my other comments. if you mean "y greater than 0 and less than 30" then write:  y > 0 && y < 30
0

Commented:
your code is odd. please tell in more detail what you want.
but I think you want a code like this
regards
``````#include <iostream.h>
#include <conio.h>

int main()
{
int m,x=2,y=29;
for(m=0; (y>0 && y<30) || m < 8;y--,m++)
{
gotoxy(x, y);
putch('*');
gotoxy(x++, y+1);
putch('@');
}
cout<<endl<<endl;
getch();
}
``````
0

Author Commented:
well im designing a game where the character (@) has to move over a screen with the boundary of 80(horizontal) and 30(vertical) and the player moves left(4), right(6) up(8) or down(2) each time the player moves one space the (@) gets turned into a "tail" sort of speak which is represented as a (*) and the (@) moves to the corresponding movement space, each time the player moves the  score adds 1, but if the player moves over the tail of where its been the score minuses 10
0

Author Commented:
anyway i'm having trouble with the movement portion of this program cant seem to get the character to respond to movements and am ripping my hair out !!!!!
0

Commented:
Are you able to put the character with hardcoded x/y coords, without all of the looping?

A single gotoxy and putch ?
0

Author Commented:
ok i've started again with the use of some other structures and have come up with the following code i'm having trouble with the cursor moving and replacing the previous character with another... what i have so far is:

int main(void)
{

char buffer[4];
int x, x2;
int y, y2;
int h, v, m, score;

score=0;
printf("%i\n",score);
x=40;
y=25;

gotoxy(x, y);
printf("@");                                     /*Snail Image*/

while(1)
{
m = getch();                                /*Get user input*/
if(m= 38)                                     /*Going Up m is ASICII ref for kBoard */
{

putchxy(x, y-1, "*");      /*Draw trail*/
putchxy(x, y-2,"@");      /* Draw new player position*/
y--;
score++;

}

else if(m= 32) /*Going Down m is ASICII ref for kBoard*/
{

putchxy(x, y, "*");              /* Draw trail*/
gotoxy(x, y--);                  /*Draw new player position*/
printf("@");
y++;

}
else if(m= 34) /*Going Left m is ASICII ref for kBoard*/
{

putchxy(x, y, "*");             /* Draw trail*/
gotoxy(x--,y);                  /* Draw new player position*/
printf("@");
x++;
}

else if(m= 36)                                   /*Going Right m is ASICII ref for kBoard*/
{
putchxy(x, y, "*");             /* Draw trail*/
gotoxy(x--, y);                  /* Draw new player position*/
printf("@");
x--;
}
else
{
if(m=22) /* Escape key m is ASICII ref for kBoard*/
{
break;
}
}
}

also am i meant to use the
putchxy()
or the
gotoxy()
printf()
in the if loops
0

Author Commented:
every keystroke moves the cursor up and when gets to top of the screen it just moves accross
0

Commented:
Why are you using printf()? Use putch()

printf() is a buffered, stdio function, putch() is unbuffered and exactly what you want.
0

Author Commented:
that answers one question but i've still got the issue with the movements they only move up with any keystroke
0

Commented:
in such applications like this, people don't overwrite written characters. they clear all the screen and print all data again. for example if you want to move a character, write it somewhere first, and then instead of overwriting the previous character clear the all screen and put your character further.

clrscr();  // in conio.h

regards
0

Commented:
you can compile this code and see the result. the result is what I told you
regards
``````#include <windows.h>
#include <iostream.h>
#include <conio.h>

int main()
{
for(int i=0;i<80;i++)
{
gotoxy(i,1);
cout<<"A";
Sleep(100);
clrscr();
}
getch();
}
``````
0

Author Commented:
ok i've figured out the movements now how can i check for collisions??? using:
_conio_gettext(int x, int y, int x2, int y2, char *buffer)
0

Commented:
I would not rely on the screen buffer for that. Keep an array that represents the locations instead.  Your array can be 2-dimensional char array. Initialize it all to spaces, or whatever.

char grid[30][80];

Everytime you add the trail, add it to the array as well.

grid[y][x] = '*';
0

Commented:
yes, he is right. you're better have a 2D array and have data in it instead of get them from console!
so, any problem?
regards
0

Author Commented:
Thanks heaps for the info really helped excelent on the reponce times and overall excelent info and easy to understand
Great effort and thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.