Solved

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

Posted on 2009-03-31
22
300 Views
Last Modified: 2012-05-06
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);

Open in new window

0
Comment
Question by:rafsta01
  • 11
  • 7
  • 4
22 Comments
 

Author Comment

by:rafsta01
ID: 24034853
#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
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 80 total points
ID: 24034913
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 Comment

by:rafsta01
ID: 24035166
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
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 80 total points
ID: 24035478
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 Comment

by:rafsta01
ID: 24036010
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
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 80 total points
ID: 24036084
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 Comment

by:rafsta01
ID: 24036142
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
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 80 total points
ID: 24036172
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
 
LVL 17

Assisted Solution

by:CSecurity
CSecurity earned 45 total points
ID: 24037984
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();

}

Open in new window

0
 

Author Comment

by:rafsta01
ID: 24045452
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 Comment

by:rafsta01
ID: 24045467
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Expert Comment

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

A single gotoxy and putch ?
0
 

Author Comment

by:rafsta01
ID: 24046184
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 Comment

by:rafsta01
ID: 24046212
every keystroke moves the cursor up and when gets to top of the screen it just moves accross
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24046240
Why are you using printf()? Use putch()

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

Author Comment

by:rafsta01
ID: 24046400
that answers one question but i've still got the issue with the movements they only move up with any keystroke
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 24047248
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
 
LVL 17

Accepted Solution

by:
CSecurity earned 45 total points
ID: 24047287
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();

}

Open in new window

0
 

Author Comment

by:rafsta01
ID: 24055911
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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24055926
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
 
LVL 17

Expert Comment

by:CSecurity
ID: 24068361
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 Closing Comment

by:rafsta01
ID: 31565116
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now