/ 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!
Expand |
Embed | Plain Text
#include <iostream> #include <cstdlib> #include <Windows.h> #include <conio.h> #include <string> #include <fstream> #include <time.h> #include <cmath> using namespace std; string options[] = {" (M/1)Movement: true", "", " (B/2)Background color: ", "", " (F/3)Foreground color: ", "", " (C/4)Character icon: ", "", "", " (G/5)Leave game", ""}; int movementFactor; string map[] = {"==================", "| |", // <--- cage "| |", "| |", "| |", "| |", "| |", "| |", "| |" "=================="}; char myMap[10][18]; // 2-dimensional version of the above map; used to easily place the creatures/manipulate the map. static bool boolCreature = true; int cage_color_selection_num = 2; int character_color_selection_num = 2; int character_selection_num = 0; int foregroundColor = 1; int backgroundColor = 0; char characters[] = {char(1), char(2), '@', char(234), char(15), char(3)}; int cage_color[] = {2, 3, 4, 5, 6, 7,}; int character_color[] = {2, 3, 4, 5, 6, 7}; char myCharacter = characters[character_selection_num]; char option_settings[] = {' ', ' ', ' ', ' ', ' ', ' ', characters[character_selection_num-1], ' ', ' ', ' ', ' '}; int option_settings1[] = {-3, -3, 1, -3, 1, -3, -3, -3, -3, -3, -3}; static void mainInterface() { system("cls"); string response; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); cout << " " << char(1) <<" Welcome to Creature-Cage! " << char(1) << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); 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; cout << "You can change your character, change the cage colors, and watch the\nun-spectacular performance!\n\n" << endl; cout << "(P)Play game" << endl; cout << "(A)About" << endl; cout << "(Q)Quit" << endl; char c = getch(); if (c == 'p'){ } else if(c == 'a'){ } else if(c == 'q'){ exit(0); } } static void AboutScreen1() { system("cls"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); cout << " Game information " << endl; cout << "------------------" << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); cout << "Programming language: C++" << endl; cout << "Lines of code: 264" << endl; cout << "compiler: Microsoft Visual C++ 2010 Express" << endl; cout << "Created by: Evan Rabdau"<< endl; cout << "\n\n(B)Main screen" << endl; char c = getch(); if (c == 'b'){ mainInterface(); } } static void add_creature_to_cage(int x, int y) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);//character text myMap[x][y] = myCharacter; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); } static void drawMap(){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //title cout << " CREATURE CAGE!" << endl; for (int i = 0; i < 10; i++) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), foregroundColor+backgroundColor); for (int j = 0; j < 18; j++) { cout << myMap[i][j]; } if (option_settings1[i] == -3){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i+5); // option text cout << options[i] << option_settings[i] << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); } else if(option_settings1[i] >= 0){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i+5); // option text cout << options[i] << option_settings[i] << option_settings1[i] << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); } } } static void mainScreen() { system("cls"); string response; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); cout << " " << char(1) <<" Welcome to Creature-Cage! " << char(1) << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); 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; cout << "You can change your character, change the cage colors, and watch the\nun-spectacular performance!\n\n" << endl; cout << "(P)Play game" << endl; cout << "(A)About" << endl; cout << "(Q)Quit" << endl; char z = getch(); if(z == 'a'){ AboutScreen1(); } else if(z == 'p'){ } else if(z == 'q'){ exit(0); } } static void creature(bool active) { bool check = true; drawMap(); system("cls"); int x, y; srand (time (NULL) ); x = rand() % 8 + 1; y = rand() % 16 + 1; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9); add_creature_to_cage(x, y); drawMap(); int a1, a2; while(active == true){ srand (time (NULL) ); int z = rand() % 8 + 1; int z1 = 1; if (z == 1 && myMap[x-1][y] != '|' && myMap[x-1][y] != '='){ //straight up system("cls"); swap(myMap[x][y], myMap[x--][y]); drawMap(); } if (z == 2 && myMap[x+1][y] != '=' && myMap[x+1][y] != '|'){ //straight down system("cls"); swap(myMap[x][y], myMap[x++][y]); drawMap(); } if (z == 3 && myMap[x][y-1] != '|' && myMap[x][y-1] != '='){ //left system("cls"); swap(myMap[x][y], myMap[x][y--]); drawMap(); } if (z == 4 && myMap[x][y+1] != '|' && myMap[x][y+1] != '='){ //right system("cls"); swap(myMap[x][y], myMap[x][y++]); drawMap(); } if (z == 5 && myMap[x-1][y+1] != '|' && myMap[x-1][y+1] != '='){ //up-right system("cls"); swap(myMap[x][y], myMap[x--][y++]); drawMap(); } if (z == 6 && myMap[x-1][y-1] != '=' && myMap[x-1][y-1] != '|'){ //up-left system("cls"); swap(myMap[x][y], myMap[x--][y--]); drawMap(); } if (z == 7 && myMap[x+1][y-1] != '=' && myMap[x+1][y-1] != '|'){ //down-left system("cls"); swap(myMap[x][y], myMap[x++][y--]); drawMap(); } if (z == 8 && myMap[x+1][y+1] != '=' && myMap[x+1][y+1] != '|'){ //down-right system("cls"); swap(myMap[x][y], myMap[x++][y++]); drawMap(); } if(kbhit()){ char k; char c; c = getch(); if (c == 'm' || c == '1') { c = 'a'; options[0] = " (M/1)Movement: false"; //movement management system("cls"); drawMap(); k = getch(); if (k == '1' || k == 'm'){ options[0] = " (M/1)Movement: true"; } } else if (c == 'c' || c == '4'){ if (character_selection_num == 7){ character_selection_num = character_selection_num*0; //controls character selection } myMap[x][y] = characters[character_selection_num++]; if(character_selection_num == 1) { option_settings[6] = char(1); } else if(character_selection_num == 2) { option_settings[6] = char(2); } else if(character_selection_num == 3) { option_settings[6] = '@'; } else if(character_selection_num == 4) { option_settings[6] = char(234); } else if(character_selection_num == 5) { option_settings[6] = char(15); } else if(character_selection_num == 6) { option_settings[6] = char(3); } } else if (c == 'f' || c == '3'){ if (foregroundColor != 16) { foregroundColor = foregroundColor + 1; option_settings1[4] = foregroundColor; system("cls"); drawMap(); } else if (foregroundColor == 16) { foregroundColor = foregroundColor * 0; option_settings1[4] = foregroundColor; system("cls"); drawMap(); } } else if (c == 'b' || c == '2'){ if (backgroundColor != 256){ backgroundColor = backgroundColor + 16; option_settings1[2] = backgroundColor; system("cls"); drawMap(); } else if (backgroundColor == 256) { backgroundColor = backgroundColor * 0; option_settings1[2] = backgroundColor; system("cls"); drawMap(); } } else if(c == 'g' || c == '5'){ mainScreen(); system("cls"); drawMap(); } } } } int main() { mainScreen(); string c; int a = 0; //---------------- for (int i = 0; i < 10; i++){ for (int j = 0; j < 18; j++){ myMap[i][j] = map[i].at(j); } } //---------puts a creature in the cage (randomly)------ do{ creature(boolCreature); }while (boolCreature == true); cin.get(); }
You need to login to post a comment.
