what you are trying to do is suprisingly not that difficult. this type of ascii game will involve some simple techniques. in any animated ascii game, it's just a matter of clearing and re-drawing the screen. you will need simple collision detection to tell when you've hit the enemy. you've mentioned that you need random movement for your enemy, here is a simple code that moves a stick figure around the screen:
#include <iostream>
#include <windows.h>
using namespace std;
enum{up, right, down, left};
int direction = 0;
int x = 20,
y = 10;
srand(time(NULL));
char stickman[3][4];
//clear the arrays
for(int i=0; i<3; i++)
for(int j=0; j<4; j++)
stickman[i][j] = ' ';
//The stickman
stickman[0][1] = 'O';
stickman[1][0] = '-';
stickman[1][1] = '+';
stickman[1][2] = '-';
stickman[2][1] = '^';
stickman[0][3] = '\n';
stickman[1][3] = '\n';
stickman[2][3] = '\0';
while(true)
{
direction = rand()%4;
switch(direction)
{
//Test the x,y coordinates to keep it on the screen
case up: if(y>0) {y--;} break;
case right: if(x<123){x++;} break;
case down: if(y<152){y++;} break;
case left: if(x>0) {x--;} break;
}
gotoxy(x,y); cout << stickman;
Sleep(800);
system("cls");
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}
void set_console_size()
{
HANDLE hOut;
SMALL_RECT DisplayArea = {0, 0, 0, 0};
//set x and y to whatever ye' want
int x = 125;
int y = 55;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DisplayArea.Right = x;
DisplayArea.Bottom = y;
SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
}
I gave you a few tools from the library that you'll need in your ascii game, the Sleep() function as a delay, gotoxy() so you can move around on the screen and set_console_size() so you can force the console window size to whatever you want; you'll know your boundries and not draw outside of the dos window.
Also, Here is a really good site to get a lot of functionality for your dos game.
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
New and improved version:
#include <iostream>
#include <windows.h>
using namespace std;
enum{up, right, down, left};
int direction = 0;
int x = 20,
y = 10;
srand(time(NULL));
char stickman[3][4];
//clear the arrays
for(int i=0; i<3; i++)
for(int j=0; j<4; j++)
stickman[i][j] = ' ';
//The stickman
stickman[0][1] = 'O';
stickman[1][0] = '-';
stickman[1][1] = '+';
stickman[1][2] = '-';
stickman[2][1] = '^';
stickman[0][3] = '\0';
stickman[1][3] = '\0';
stickman[2][3] = '\0';
while(true)
{
direction = rand()%4;
switch(direction)
{
//Test the x,y coordinates to keep it on the screen
case up: if(y>0) {y--;} break;
case right: if(x<123){x++;} break;
case down: if(y<152){y++;} break;
case left: if(x>0) {x--;} break;
}
gotoxy(x,y); cout << stickman[0];
gotoxy(x++,y); cout << stickman[1];
gotoxy(x++,y); cout << stickman[2];
Sleep(800);
system("cls");
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}
void set_console_size()
{
HANDLE hOut;
SMALL_RECT DisplayArea = {0, 0, 0, 0};
//set x and y to whatever ye' want
int x = 125;
int y = 55;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DisplayArea.Right = x;
DisplayArea.Bottom = y;
SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
}
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
Full working program:
#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;
void gotoxy(int, int);
void set_console_size();
int main()
{
enum{up, right, down, left};
char stickman[3][4];
int direction = 0;
int x = 40,
y = 20;
srand(time(NULL));
set_console_size();
//clear the arrays
for(int i=0; i<3; i++)
for(int j=0; j<4; j++)
stickman[i][j] = ' ';
//The stickman
stickman[0][1] = 'O';
stickman[1][0] = '-';
stickman[1][1] = '+';
stickman[1][2] = '-';
stickman[2][1] = '^';
stickman[0][3] = '\0';
stickman[1][3] = '\0';
stickman[2][3] = '\0';
while(true)
{
direction = rand()%4;
cout << direction << endl;
switch(direction)
{
//Test the x,y coordinates to keep it on the screen
case up: if(y>=0) {y--; } else{y+=3;} cout << "up"; break;
case right: if(x<=120){x+=3;} else{x-=3;} cout << "right"; break;
case down: if(y<=153){y++; } else{y-=3;} cout << "down"; break;
case left: if(x>=3) {x-=3;} else{x+=3;} cout << "left"; break;
}
//Draw stickman
cout << endl << "x=" << x << ", y=" << y;
gotoxy(x,y); cout << stickman[0];
gotoxy(x,y+1); cout << stickman[1];
gotoxy(x,y+2); cout << stickman[2];
Sleep(500);
system("cls");
}
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void set_console_size()
{
HANDLE hOut;
SMALL_RECT DisplayArea = {0, 0, 0, 0};
//set x and y to whatever ye' want
int x = 125;
int y = 55;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DisplayArea.Right = x;
DisplayArea.Bottom = y;
SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
}
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
However you still use system("cls"); :icon_smile:
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
system("cls") is cheap and easy. just like my women.
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
By the way it is moving diagonially because you set the move in accordance to i . Each time the variable is increased in the for loop, your "character" is moving both at x and y directions.
mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
system("cls") is cheap and easy. just like my women.
Easy, yes. Cheap, no. The 'work' needed to call the operating system, find and execute thepause command, then exit the O/S and return to the program is more like a $1000 call girl. It works well, but is far from cheap.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944