1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <iostream>
#include "gotoxy.h"
using namespace std;
bool moveTo(int x, int y, int &bite, string map[])
{
bool flag = false;
if (map[y][x] == ' ')
{
flag = true;
map[y][x] = '@';
}
else if (map[y][x] == 'x')
{
flag =true;
map [y][x] = '@';
bite++;
}
return flag;
}
int main()
{
string map[10] =
{
"###################",
"#@ #",
"# x #",
"# #", //This is how the map looks
"# #",
"# x #",
"# #",
"# #",
"###################"
};
int x=1;
int y=1;
int bite= moveTo(x,y,bite,map);
bool game_running = true;
do{
gotoxy(0,0); //uses the gotoxy file
for (int i=0; i< 10 ; i++)
cout << map [i] << endl;
cout << "You ate " << bite << " bite(s)" << endl;
system("pause>nul");
if(GetAsyncKeyState(VK_DOWN) && moveTo(x, y+1,bite,map))
{
map [y][x]=' ';
y++; //moves down
}
if(GetAsyncKeyState(VK_UP) && moveTo(x, y-1,bite,map) )
{
map [y][x]=' ';
y--; //moves up
}
if(GetAsyncKeyState(VK_RIGHT) && moveTo(x+1,y,bite,map) )
{
map [y][x]=' ';
x++; //moves right
}
if(GetAsyncKeyState(VK_LEFT) && moveTo (x-1,y,bite,map) )
{
map [y][x]=' ';
x--; //moves left
}
if(GetAsyncKeyState(VK_ESCAPE))
{
game_running = false;
}
}
while(game_running == true);
system("cls");
cout << "Game over! Thanks for playing!";
return 0;
}
|