• ### Announcements

#### Archived

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

# Question (C++)

## Recommended Posts

BioSquirrel    122
Alright, I''m making this text game with a map made from just using cout and printing text... Right now I''m using a system of after each turn telling the player where he and the enemies are by using coordinates. I''m thinking this would get a little confusing since after each turn, you would have to look on the map and check where you and the enemies are. I''m using a ''do'' loop for the turn that loops until enemies = 0. I''m fairly new to C++ so does anyone know how I could have a symbol on the map representing each player that moves around the map when their x and y positions change? The only way I would know of doing this is by using tons of ''if'' statements which would result in super bad spaghetti code. If you want to see the, code tell me.

##### Share on other sites
If your map is an array, then you could just store the player''s and enemies'' positions in the map array as well. Just use characters to represent them. So when you draw the map, you automatically draw the player and enemies as well.

  char map[10][10]//fill map with info//update mapupdatePlayer(x,y);updateEnemies(x,y);//draw mapfor(int i=0;i<10;i++) { cout >> endl; for(int j=0;j<10;j++) { cout >> map[i][j]; }}cout >> endl;//function to update map for the playervoid updatePlayer(int x, int y) { map[playerX][playerY]=20; //20 is the decimal ascii code for a space playerX=x; playerY=y; map[playerX][playerY]=15; //or whatever is the player}

It''s a little disjointed, but you get the idea.

---
Make it work.
Make it fast.

##### Share on other sites
nickm    122
I dont think it''s possible to write a character to a specific location in a win32 console program (without using a hack). However, you can just redraw the map with the enemies included. I don''t know how you''ve stored all enemies, but since you''re using a do-loop I assume you''ve stored them in some sort of list (array). Anyway, consider this piece of code

  char map[20][20]; // suppose there is a map of 20x20 charactersstruct { short x, y; unsigned char type;} enemy[400];int numEnemies;short playerx, playery;void drawMap(){ char str[81]; int i, line; for (line = 0; line<20; line++) { // create a string from a line of the map for (i = 0; i<20; i++) str[i] = map[i][line]; str[i] = 0; // delimit the string // now put the enemies that are on this line // in the string for (i = 0; i

I hope this pretty much solves your problem, but feel free ask again if this is not exactly what you mean

##### Share on other sites
Rube    122
Yeah, will be hard to "flush" the Win32 console app screen.

If you want to dive into the Win32 API you can do replacements and flushing with the Character Mode Console routines. Then you could simply update the position and not redraw your entire map. It would require a little diving into the Win32 API on www.msdn.microsoft.com, but might be "prettier" to work with.

R.

##### Share on other sites
BioSquirrel    122
I think I know what you''re both saying. I''m going to try some things and see if anything works well.

##### Share on other sites
Guest Anonymous Poster
quote:
Original post by nickm
I dont think it''s possible to write a character to a specific location in a win32 console program (without using a hack).

Well, it is if you''re using a library designed to do such. Plenty of games and programs do this, particularly the older ones that relied on ascii graphics.

##### Share on other sites
BioSquirrel    122
BTW my code is long, with many unnecessary coding because I am fairly new to C++ and I''m still getting the concept of arrays. I''ll probably end up making the map a huge mass of code that has bugs in it.

##### Share on other sites
nickm    122
quote:
Original post by Anonymous Poster
Well, it is if you''re using a library designed to do such. Plenty of games and programs do this, particularly the older ones that relied on ascii graphics.

Yes I know, but these are real dos programs (and not win32 console). With win32 it''s not possible to write to the memory address which represents the screen (was it 0xB8000000?), that''ll give you an access violation. And since win32 compilers don''t have a conio.h include file, it''s not really possible to do this easily (or am I completely wrong here?). Like you say, there are libraries for that but basically the hack is just inside the library then.

##### Share on other sites
BioSquirrel    122
I put this game in the showcase today! My first actually completed almost good game... its here

##### Share on other sites
Martee    476
quote:
Original post by nickm
I dont think it''s possible to write a character to a specific location in a win32 console program (without using a hack)

SetConsoleCursorPosition() ?

##### Share on other sites
Jeff D    122
Try this:

  #include #include HANDLE H_OUTPUT = GetStdHandle(STD_OUTPUT_HANDLE); // This will be your hadle to print // a char anywhere you want. You need this.void PrintChar(int x, int y, char* CH){ COORD cursor = {x, y}; //This is a struct that is found in windows.h that you need for //Setting the cursor position. All it is is two ints that tell //called X and Y that tell where you want the cursor to be. //0,0 will be the top left corner, where 79, 24 will be the bottom //right corner. Dont go under 0,0 or 79,24. SetConsoleCursorPosition(H_OUTPUT, cursor); //This places the cursor at the coord you wnated. cout << CH;}int main(){ char CH[] = "Hi"; int x = 35; int y = 15; //Playe with the x and y values just dont make x higher than 79 or make y higher than 24. PrintChar(x, y, CH); return 0;} `

Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton

##### Share on other sites
nickm    122
This is nice! I really had no idea that this was possible.