Question (C++)

Started by
11 comments, last by BioSquirrel 22 years, 3 months ago
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.
Advertisement
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.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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<numEnemies; i++)         if (enemy[i].y==line)            str[enemy[i].x] = ''E'';            // if the player is on this line, put      // him in the string also      if (playery==line)         str[playerx] = ''P'';            // now print the line (I don''t really use these      // functions, I''d rather use printf(), so I don''t      // know if this is valid)      cout << str << ''\n'';   }}  


I hope this pretty much solves your problem, but feel free ask again if this is not exactly what you mean
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.
I think I know what you''re both saying. I''m going to try some things and see if anything works well.
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.
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.
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.
I put this game in the showcase today! My first actually completed almost good game... its here
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() ?
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

This topic is closed to new replies.

Advertisement