Posted By

Desoxena on 04/09/11


Tagged

console ascii c++ awesome-sauce


Versions (?)

Creature Cage (C++ Console game)


 / Published in: C++
 

This is a small ASCII game that I made in C++. In this game, you get to watch a little creature run around randomly in a cage!

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <Windows.h>
  4. #include <conio.h>
  5. #include <string>
  6. #include <fstream>
  7. #include <time.h>
  8. #include <cmath>
  9. using namespace std;
  10. string options[] = {" (M/1)Movement: true", "", " (B/2)Background color: ", "", " (F/3)Foreground color: ", "", " (C/4)Character icon: ", "", "", " (G/5)Leave game", ""};
  11. int movementFactor;
  12. string map[] = {"==================",
  13. "| |", // <--- cage
  14. "| |",
  15. "| |",
  16. "| |",
  17. "| |",
  18. "| |",
  19. "| |",
  20. "| |"
  21. "=================="};
  22. char myMap[10][18]; // 2-dimensional version of the above map; used to easily place the creatures/manipulate the map.
  23. static bool boolCreature = true;
  24. int cage_color_selection_num = 2;
  25. int character_color_selection_num = 2;
  26. int character_selection_num = 0;
  27. int foregroundColor = 1;
  28. int backgroundColor = 0;
  29. char characters[] = {char(1), char(2), '@', char(234), char(15), char(3)};
  30. int cage_color[] = {2, 3, 4, 5, 6, 7,};
  31. int character_color[] = {2, 3, 4, 5, 6, 7};
  32. char myCharacter = characters[character_selection_num];
  33. char option_settings[] = {' ', ' ', ' ', ' ', ' ', ' ', characters[character_selection_num-1], ' ', ' ', ' ', ' '};
  34. int option_settings1[] = {-3, -3, 1, -3, 1, -3, -3, -3, -3, -3, -3};
  35. static void mainInterface() {
  36. system("cls");
  37. string response;
  38. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
  39. cout << " " << char(1) <<" Welcome to Creature-Cage! " << char(1) << endl;
  40. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  41. cout << "\n\n\nIn this little console applet, you have the ability to control a little\ncharacter, as he randomly runs around in his little cage!" << endl;
  42. cout << "You can change your character, change the cage colors, and watch the\nun-spectacular performance!\n\n" << endl;
  43. cout << "(P)Play game" << endl;
  44. cout << "(A)About" << endl;
  45. cout << "(Q)Quit" << endl;
  46. char c = getch();
  47. if (c == 'p'){
  48. }
  49. else if(c == 'a'){
  50. }
  51. else if(c == 'q'){
  52. exit(0);
  53. }
  54. }
  55. static void AboutScreen1() {
  56. system("cls");
  57. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
  58. cout << " Game information " << endl;
  59. cout << "------------------" << endl;
  60. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  61. cout << "Programming language: C++" << endl;
  62. cout << "Lines of code: 264" << endl;
  63. cout << "compiler: Microsoft Visual C++ 2010 Express" << endl;
  64. cout << "Created by: Evan Rabdau"<< endl;
  65. cout << "\n\n(B)Main screen" << endl;
  66. char c = getch();
  67. if (c == 'b'){
  68. mainInterface();
  69. }
  70. }
  71. static void add_creature_to_cage(int x, int y) {
  72. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);//character text
  73. myMap[x][y] = myCharacter;
  74. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  75. }
  76. static void drawMap(){
  77. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //title
  78. cout << " CREATURE CAGE!" << endl;
  79. for (int i = 0; i < 10; i++) {
  80. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), foregroundColor+backgroundColor);
  81. for (int j = 0; j < 18; j++) {
  82. cout << myMap[i][j];
  83. }
  84. if (option_settings1[i] == -3){
  85. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i+5); // option text
  86. cout << options[i] << option_settings[i] << endl;
  87. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  88. }
  89. else if(option_settings1[i] >= 0){
  90. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i+5); // option text
  91. cout << options[i] << option_settings[i] << option_settings1[i] << endl;
  92. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  93. }
  94. }
  95. }
  96. static void mainScreen() {
  97. system("cls");
  98. string response;
  99. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
  100. cout << " " << char(1) <<" Welcome to Creature-Cage! " << char(1) << endl;
  101. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  102. cout << "\n\n\nIn this little console applet, you have the ability to control a little\ncharacter, as he randomly runs around in his little cage!" << endl;
  103. cout << "You can change your character, change the cage colors, and watch the\nun-spectacular performance!\n\n" << endl;
  104. cout << "(P)Play game" << endl;
  105. cout << "(A)About" << endl;
  106. cout << "(Q)Quit" << endl;
  107. char z = getch();
  108. if(z == 'a'){
  109. AboutScreen1();
  110. }
  111. else if(z == 'p'){
  112. }
  113. else if(z == 'q'){
  114. exit(0);
  115. }
  116. }
  117. static void creature(bool active) {
  118. bool check = true;
  119. drawMap();
  120. system("cls");
  121. int x, y;
  122. srand (time (NULL) );
  123. x = rand() % 8 + 1;
  124. y = rand() % 16 + 1;
  125. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
  126. add_creature_to_cage(x, y);
  127. drawMap();
  128. int a1, a2;
  129. while(active == true){
  130. srand (time (NULL) );
  131. int z = rand() % 8 + 1;
  132. int z1 = 1;
  133. if (z == 1 && myMap[x-1][y] != '|' && myMap[x-1][y] != '='){ //straight up
  134. system("cls");
  135. swap(myMap[x][y], myMap[x--][y]);
  136. drawMap();
  137. }
  138. if (z == 2 && myMap[x+1][y] != '=' && myMap[x+1][y] != '|'){ //straight down
  139. system("cls");
  140. swap(myMap[x][y], myMap[x++][y]);
  141. drawMap();
  142. }
  143. if (z == 3 && myMap[x][y-1] != '|' && myMap[x][y-1] != '='){ //left
  144. system("cls");
  145. swap(myMap[x][y], myMap[x][y--]);
  146. drawMap();
  147. }
  148. if (z == 4 && myMap[x][y+1] != '|' && myMap[x][y+1] != '='){ //right
  149. system("cls");
  150. swap(myMap[x][y], myMap[x][y++]);
  151. drawMap();
  152. }
  153. if (z == 5 && myMap[x-1][y+1] != '|' && myMap[x-1][y+1] != '='){ //up-right
  154. system("cls");
  155. swap(myMap[x][y], myMap[x--][y++]);
  156. drawMap();
  157. }
  158. if (z == 6 && myMap[x-1][y-1] != '=' && myMap[x-1][y-1] != '|'){ //up-left
  159. system("cls");
  160. swap(myMap[x][y], myMap[x--][y--]);
  161. drawMap();
  162. }
  163. if (z == 7 && myMap[x+1][y-1] != '=' && myMap[x+1][y-1] != '|'){ //down-left
  164. system("cls");
  165. swap(myMap[x][y], myMap[x++][y--]);
  166. drawMap();
  167. }
  168. if (z == 8 && myMap[x+1][y+1] != '=' && myMap[x+1][y+1] != '|'){ //down-right
  169. system("cls");
  170. swap(myMap[x][y], myMap[x++][y++]);
  171. drawMap();
  172. }
  173. if(kbhit()){
  174. char k;
  175. char c;
  176. c = getch();
  177. if (c == 'm' || c == '1') {
  178. c = 'a';
  179. options[0] = " (M/1)Movement: false"; //movement management
  180. system("cls");
  181. drawMap();
  182. k = getch();
  183. if (k == '1' || k == 'm'){
  184. options[0] = " (M/1)Movement: true";
  185. }
  186. }
  187. else if (c == 'c' || c == '4'){
  188. if (character_selection_num == 7){
  189. character_selection_num = character_selection_num*0; //controls character selection
  190. }
  191. myMap[x][y] = characters[character_selection_num++];
  192. if(character_selection_num == 1) {
  193. option_settings[6] = char(1);
  194. }
  195. else if(character_selection_num == 2) {
  196. option_settings[6] = char(2);
  197. }
  198. else if(character_selection_num == 3) {
  199. option_settings[6] = '@';
  200. }
  201. else if(character_selection_num == 4) {
  202. option_settings[6] = char(234);
  203. }
  204. else if(character_selection_num == 5) {
  205. option_settings[6] = char(15);
  206. }
  207. else if(character_selection_num == 6) {
  208. option_settings[6] = char(3);
  209. }
  210. }
  211. else if (c == 'f' || c == '3'){
  212. if (foregroundColor != 16) {
  213. foregroundColor = foregroundColor + 1;
  214. option_settings1[4] = foregroundColor;
  215. system("cls");
  216. drawMap();
  217. }
  218. else if (foregroundColor == 16) {
  219. foregroundColor = foregroundColor * 0;
  220. option_settings1[4] = foregroundColor;
  221. system("cls");
  222. drawMap();
  223. }
  224. }
  225. else if (c == 'b' || c == '2'){
  226. if (backgroundColor != 256){
  227. backgroundColor = backgroundColor + 16;
  228. option_settings1[2] = backgroundColor;
  229. system("cls");
  230. drawMap();
  231. }
  232. else if (backgroundColor == 256) {
  233. backgroundColor = backgroundColor * 0;
  234. option_settings1[2] = backgroundColor;
  235. system("cls");
  236. drawMap();
  237. }
  238. }
  239. else if(c == 'g' || c == '5'){
  240. mainScreen();
  241. system("cls");
  242. drawMap();
  243. }
  244. }
  245. }
  246. }
  247. int main()
  248. {
  249. mainScreen();
  250. string c;
  251. int a = 0;
  252. //----------------
  253. for (int i = 0; i < 10; i++){
  254. for (int j = 0; j < 18; j++){
  255. myMap[i][j] = map[i].at(j);
  256. }
  257. }
  258. //---------puts a creature in the cage (randomly)------
  259. do{
  260. creature(boolCreature);
  261. }while (boolCreature == true);
  262. cin.get();
  263. }

Report this snippet  

You need to login to post a comment.