Archived

This topic is now archived and is closed to further replies.

TheNerd Tk421

A grid movement question

Recommended Posts

TheNerd Tk421    122
how to get a figure(letter "C" to be exact) on the screen to stand for the main character.... here is the source code if you have no clue what im talking about... #include <iostream> #include <stdlib.h> #include <iomanip> #include <conio.h> using namespace std; void startup_menu(); int main_menu(); void move(); void location(int, int); int main() { //main loop startup_menu(); return 0; } void location(int xcoord, int ycoord) { cout << setw(73) << "Your location is " << xcoord << ", " << ycoord << endl; } void move() { char dir = 'a'; int x = 10, y = 10; while(dir != 'q') { dir = getch(); ("cls"); switch(dir) { case 'w': y--; system("cls"); cout << "\nYou move North." << endl; break; case 's': y++; cout << "\nYou move South." << endl; break; case 'd': x++; cout << "\nYou move East." << endl; break; case 'a': x--; cout << "\nYou move West." << endl; break; case 'l': location(x,y); break; case 'q': system("cls"); return; default: cout << "Try again"<[edited by - TheNerd Tk421 on August 8, 2003 12:51:03 PM] [edited by - TheNerd Tk421 on August 8, 2003 1:24:05 PM] [edited by - TheNerd Tk421 on August 8, 2003 1:24:41 PM] [edited by - TheNerd Tk421 on August 8, 2003 1:25:18 PM]

Share this post


Link to post
Share on other sites
sSimontis    100
You would probaly need to provide x y coodinates. Check out GameTutorials under C++, and you might find what you are looking for.

Scott Simontis
If it wasn''t for C, we''d be using BASI, PASAL and OBOL

Share this post


Link to post
Share on other sites
TheNerd Tk421    122
Can ne 1 help me with my problem?.. i need to have a character on the screen that moves when i push the directional keys
( W
A S D )...pls

[edited by - TheNerd Tk421 on August 8, 2003 1:26:05 PM]

Share this post


Link to post
Share on other sites
EL    122
I don''t think there''s a way of doing it in pure C++ (ie using some kind of function). Maybe you should build a 2 dimensional buffer representing your screen, or use something else (maybe WIN32?).

Share this post


Link to post
Share on other sites
schiggl    138
In Borland C++ exists a include file named conio.h .
There are several functions for console screen editing.
gotoxy, color change, ...

Perhaps you can find a conio.h which is compatible with your compiler.

schiggl

Share this post


Link to post
Share on other sites
There''s probably some way to set where you write text onto the screen, but I don''t know what it would be. Try searching google for "c++ console output text location of screen" or somesuch.

I can suggest a rather inelegant solution however: completely clear and redraw the screen every time the character moves. You then don''t need to specify a particular location on the screen after drawing other things.

Share this post


Link to post
Share on other sites
FtMonkey    122
I think what he means by a 2d buffer is to store your data in an array like:

int screen[max_x][max_y];

...and represent whatever is in the screen using the numbers you store for example let's say the array is 5 by 5...

0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,

Now...you would go through the array searching for different values in this case 0 represents nothing so it's ignored, and 1 represents your character or "c" once you found 1 then you would place your character accordingly to the position you found it in the array...

EDIT: To clear the screen system("cls"); (you need to include stdlib.h)


[edited by - FtMonkey on August 8, 2003 5:46:21 PM]

Share this post


Link to post
Share on other sites
EL    122
quote:
Original post by FtMonkey
I think what he means by a 2d buffer is to store your data in array like:

int screen[max_x][max_y];

...and represent whatever is in the screen using the numbers you store for example let''s say the array is 5 by 5...

0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,

Now...you would go through the array searching for different values in this case 0 represents nothing so it''s ignored, and 1 represents your character or "c" once you found 1 then you would place your character accordingly to the position you found it in the array...

EDIT: To clear the screen system("cls"); (you need to include stdlib.h)

[edited by - FtMonkey on August 8, 2003 1:46:19 PM]

That''s exactly what I meant. I had to do this once, searched through every bit of documentation (msdn) and didn''t find one match (well, none related to C/C++).

Share this post


Link to post
Share on other sites
FtMonkey    122
Easy is the same method used for tiles in rpgs use the for loop something like...



int screen[max_y][max_x];


for(int y=0;y < max_y;y++)
{
for(int x=0;x < max_x;x++)
{
int n = screen[y][x]; //find the number


}
}


EDIT: I switched some values accidently look at it now it should be correct

[edited by - FtMonkey on August 8, 2003 2:01:25 PM]

Share this post


Link to post
Share on other sites
EL    122
Normally (but not always) a console window has 25 lines and 88 characters per line (or something in that region). So :
char myScreenBuffer[25][88].
Edit : or look at the above post.

[edited by - EL on August 8, 2003 1:54:54 PM]

Share this post


Link to post
Share on other sites
EL    122
quote:
Original post by TheNerd Tk421
that just went WAY over my head....i wish C++ was as easy to learn as HTML was...

[edited by - TheNerd Tk421 on August 8, 2003 2:07:38 PM]

Represent your screen as a buffer (as shown previously).
If you move, clear the screen, update the position of ''C'' and write it to the screen.

Share this post


Link to post
Share on other sites
FtMonkey    122
Don''t get overwhelmed by the previous examples they''re not hard once you understand what you are doing it''ll come easy. Just so you know the for loop thing is very useful for a lot of 2d games specially rpgs and platform games. So you are going to have to learn it eventually why not start now?

Share this post


Link to post
Share on other sites
TheNerd Tk421    122
char myScreenBuffer[25][88]
int screen[max_y][max_x];
for(int y=0;y < max_y;y++)
{
for(int x=0;x < max_x;x++)
{
int n = screen[y][x];
}
}

.........
like that?
i do put char myScreenBuffer[25][88]
b4 int screen right?
would i do this also?:
#define max_y 25
#define max_x 88


[edited by - TheNerd Tk421 on August 8, 2003 2:23:25 PM]

Share this post


Link to post
Share on other sites
EL    122
Actually, the screen[][] and screenBuffer[][] do the same thing, you only need one of them. So max_x == 88 and max_y == 25. But I'd use char instead of int (in case you use screen and not myScreenBuffer).



[edited by - EL on August 8, 2003 2:25:19 PM]

Share this post


Link to post
Share on other sites
FtMonkey    122
Nope but you are close you don't need the char array if you are already representing the screen in an int array, the character array is just another method to do it...You have to understand what you are storing in the int array is nothing but a representation of your console window using numbers so you know where objects should be drawn (in this case is "c" which can be represented by any number) and of course you should have some number that you ignore (so you don't draw nothing where you find it) the number would probably be 0 since is nothing...

quote:

would i do this also?:
#define max_y 25
#define max_x 88



...If you know what size your array is going to be then yes if you don't then is bad coding practice to use a preset value you should use pointers, but don't worry about this for now


[edited by - FtMonkey on August 8, 2003 2:31:13 PM]

Share this post


Link to post
Share on other sites
TheNerd Tk421    122
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <conio.h>
#define max_x == 88
#define max_y == 25
using namespace std;


void startup_menu();
int main_menu();
void move();
void location(int, int);

int main()
{ //main loop
startup_menu();
return 0;
}

void location(int xcoord, int ycoord)
{
cout << setw(73) << "Your location is " << xcoord << ", " << ycoord << endl;
}


void move()
{

char myScreenBuffer[max_x][max_y]
for(int y=0;y < max_y;y++)
{
for(int x=0;x < max_x;x++)
{
int n = screen[y][x];
}
}

char dir = ''a'';
int x = 10, y = 10;

while(dir != ''q'')
{
dir = getch();
("cls");
switch(dir)
{
case ''w'': y--; system("cls"); cout << "\nYou move North." << endl; break;
case ''s'': y++; cout << "\nYou move South." << endl; break;
case ''d'': x++; cout << "\nYou move East." << endl; break;
case ''a'': x--; cout << "\nYou move West." << endl; break;
case ''l'': location(x,y); break;
case ''q'': system("cls"); return;
default: cout << "Try again"<}
if(x==7 && y==11) //if x is 7 and y is 11
{
cout << "\nYou found the treasure!";
}
else
if(x==12 && y==12)
{
cout << "\nYou have entered the town of Aranor." << endl;
}
}
}

void startup_menu()
{
char startgame;
startgame = ''@'';


while(startgame != ''\r'')
{
cout << endl << endl << endl << endl;
cout << setw(45) << "NeverQuest" << endl;
cout<< setw(46) << "Press Enter!";
startgame = getch();
system("cls");
}
main_menu();
}

int main_menu()
{

cout << "*************" << endl;
cout<< "Main Menu" << endl;
cout<< "*************" << endl;
cout<< "1) New Game" << endl;
cout<< "2) Load Game " << endl;
cout<< "3) Manual" << endl;
cout<< "4) Credits" << endl;
cout<< "5) Exit" << endl;
cout<< "*************" << endl;

while(1)
{
char choice;

choice = getch();
if(choice==''1'')
{
system("cls");

move();
}


if(choice==''4'')
{
cout<<"Modded by TheNerd Tk421"
if(choice==''3'')
{
cout<<"Go north: press ''w''"
else
if(choice==''5'')
{

return 0;
}

}
}


like this?

Share this post


Link to post
Share on other sites
FtMonkey    122
Straight out the defines are wrong you should practice more c/c++ before attempting to do stuff like this...About the defines then can't be like

#define Number == 10

..that's wrong first of all when you use == it means you are comparing two numbers so it won't work, second you don't need the equal sign so it would look like...

#define Number 10

Another...Ooops never mind I was wrong

EDIT: Put your code between [ source ] [ /source ] tags without the spaces to get the box so it's easier to read


[edited by - FtMonkey on August 8, 2003 2:44:56 PM]

[edited by - FtMonkey on August 8, 2003 2:51:39 PM]

Share this post


Link to post
Share on other sites