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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <iostream>
#include <windows.h>
#include <ctime>
#include <conio.h> //Necessary for _getch() and _kbhit()
#include <string>
using namespace std;
//From the following arrays I removed the extra brackets from every row
//so you wont have 2-d arrays
string sArrayX [16] = {
"XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX ",
" XXXXX"
};
string sArrayG [16] = {
"<|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|> ",
" <|^|>"
};
int main (){
int i=0; //initialised to 0
int iMove = 0;
char cMove;
do{
if(i>14) i=0;//check if i is greater than 14 and reset it to 0
while( !_kbhit() ){//_kbhit() returns true when you hit a button
//so this loop will stay until you press a button
system("cls");
cout << sArrayX [i];//display xxxx
cout << string(21, '\n') << sArrayG [iMove];//display gun
//( string(21, '\n') easier way to display 21 new lines)
cout << endl << "Press 'A' to move left and press 'D' to move right";
i++;//increase i so that the xxxx block moves across the screen
if(i>14) i=0;//check if i is greater than 14 and reset it to 0
//This check is performed twice because if you press fast a key it doesn't make
//it through this loop to reset the value so you go off limits from the array
Sleep(100);
}
cMove = _getch();//get the key that the user pressed to exit the above loop
if (cMove == 'a' || cMove == 'A'){
if(iMove>0)
iMove--;//move left if iMove is geater than 0 (far left end)
} else if (cMove == 'd' || cMove == 'D'){
if(iMove<15)
iMove++;//move right if iMove is less tha 15 (far right end)
} else if (cMove == 'w' || cMove == 'W'){
system("cls");
cout << sArrayX[i];
switch(iMove){
case 0:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 1:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 2:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 3:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 4:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 5:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 6:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 7:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 8:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 9:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 10:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 11:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 12:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 13:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 14:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;
case 15:
for(int i = 0; i < 22; i++)
cout << "\n |";
break;}
if(i == iMove){
system("cls");
cout << sArrayX [i];
cout << string(21, '\n');
cout << sArrayG [iMove];
cout << "You win!"; break;}
//Sleep(100);Removed because if you keep pressing 'w' cause problems
//in the xxxxx block movement
}
}while(true);//Repeat the whole thing for ever
cin.get();
return 0;
}
|