Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <unistd.h>
- //Color Bash Prompt:
- //https://wiki.archlinux.org/index.php/Color_Bash_Prompt
- unsigned int sleep(unsigned int seconds);
- using namespace std;
- // Initialized global variables
- int Gamespeed = 50;
- int HpStart = 1;
- int MaxHp = 100;
- char move;
- // Map prototypes
- const struct Map_S {int width, height; char data[64][64];} Maps[] = {
- {
- 9, 10, {
- "#########",
- "# # #",
- "# # # # #",
- "# # # # #",
- "#+# # #+#",
- "# # # # #",
- "# # # # #",
- "# # # # #",
- "#@# * # #",
- "#######-#"
- }},
- {
- 39, 18, {
- "#####################################-#",
- "#@ # # # * * # # #",
- "# ###### # #++# # # * * + * # # # #",
- "# ## # # #### # # * # # # #",
- "# # # # # # ## #******* # # #",
- "###### ##### #### # # # #####",
- "# # # ## # ******# #",
- "# ########## #### # ## #### #",
- "# #****** #* #",
- "##### ############ ## # #* #",
- "# ## ## # ******#**** #",
- "# ## +++ ####### # #* #",
- "# # ####### ######## #* ***#",
- "# # ####### * * * #",
- "# ## * * * * * * * #",
- "# ### * * * * * * * * * #",
- "#++ +### * * * * #",
- "#######################################"
- }},
- {
- 39, 18, {
- "#######################################",
- "#@ # # #********## # #",
- "########## # # # # # ####",
- "# # #+# ###### # # #",
- "###### ##### ### # # # #### #",
- "# # # # #",
- "# ###### # #####********########## ####",
- "# # # # #",
- "########## # # *## *## #",
- "#++++# # ######::###### *## *## #",
- "#++++# ### # # #",
- "####### ###### ######**####",
- "# # # K#",
- "# ******** # #**********#",
- "# ******** ######::###### +++ #",
- "# ******** * # K# # +++ #",
- "# # ## # #",
- "####################--#################"
- }},
- {
- 39, 18, {
- "########### ###########",
- "#@ # #*********#",
- "# ###################*********#",
- "# *****#",
- "# ###################*** *****#",
- "########### # # ##### ######",
- " #K# # # # # ####### # # #",
- " #*# # ##### # # # # # # ###### #",
- " #*# # # ##### ### # # #",
- " #*# # ##### ### ##############",
- " #*# ##### ##### ####### #",
- " #*# # # ########## ####",
- "##*############### ##### #####",
- "#* *******###################**** #",
- "#* * * ******* #",
- "#** * * * ########:::######## ******* #",
- "#** * # # # # #",
- "########### #---# ###########"
- }},
- {
- 39, 18, {
- "#######################################",
- "#@ #",
- "# ############## ############## #",
- "# ############## ############## #",
- "# ############## ############## #",
- "# # # #",
- "# # ##### ##### # #",
- "# # ### ### # #",
- "# #####::####### ? #######::##### #",
- "# # ### ### # #",
- "# # ########### # #",
- "# # # #",
- "# ############## ############## #",
- "# ############## ############## #",
- "# ############## ############## #",
- "# **********K### ###K********** #",
- "#################-----#################"
- }}
- };
- //Map template.....
- /*
- {
- 39, 18, {
- "#######################################",
- "#@ #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "#######################################"
- }},
- */
- // Uninitialized variables
- int Hp;
- int Level;
- int px, py;
- int Keys;
- string w;
- // Map data
- struct Map_S Map;
- void what(){
- wh:
- cout << "\n\n:";
- cin >> w;
- if(w == "hp" | w == "HP" | w == "Hp"){
- cout << "Enter a Value: ";
- cin >> Hp;
- w.clear();
- }
- else if(w == "Keys" | w == "keys" | w == "KEYS"){
- cout << "Enter a Value: ";
- cin >> Keys;
- w.clear();
- }
- else if(w == "gamespeed"){
- cout << "Enter a value: ";
- cin >> Gamespeed;
- w.clear();
- }
- else if(w == "px"){
- cout << "Enter a value: ";
- cin >> px;
- w.clear();
- }
- else if(w == "py"){
- cout << "Enter a value: ";
- cin >> py;
- w.clear();
- }
- else if(w == "Help" | w == "help" | w == "?"){
- cout << "Possible commands:\n\n" << "hp\n" << "keys\n" << endl;
- goto wh;
- w.clear();
- }
- else {
- cout << "Fuck Off!!!!\n" << endl;
- system("read");
- w.clear();
- }
- }
- void SetMap(int Index) {
- Level = Index;
- Map = Maps[Level];
- // Get @ coords
- px = py = 0;
- for(int y = 0; y < Map.height; y++)
- for(int x = 0; x < Map.width; x++)
- if (Map.data[y][x] == '@') {
- px = x;
- py = y;
- }
- }
- void DrawMap() {
- // Clear screen
- system("clear");
- // Draw map
- for(int y = 0; y < Map.height; y++) {
- for(int x = 0; x < Map.width; x++) {
- if (Map.data[y][x] == '#')
- cout << (char)219;
- else
- cout << Map.data[y][x];
- }
- cout << endl;
- }
- // Show stats
- cout << "KEY: " << Keys << "\n\n\nHP: ";
- cout << Hp;
- cout << " / ";
- cout << MaxHp << endl;
- cout << "Level: " << Level + 1 << "\n\n";
- }
- int main() {
- // Program loop
- while (1) {
- // Initialize vairables
- Hp = HpStart;
- Keys = 0;
- // Menu loop
- while (1) {
- // Show menu screen
- system("clear");
- cout <<
- "######################################\n"
- "# #\n"
- "# MENU #\n"
- "# #\n"
- "# 1. Start Game #\n"
- "# 2. Level Select #\n"
- "# 3. Instructions #\n"
- "# 4. EXIT #\n"
- "# #\n"
- "# #\n"
- "######################################\n\n";
- cout << "Enter a choice: ";
- // Get input
- int input;
- cin >> input;
- // Start game at level 1
- if (input == 1) {
- SetMap(0);
- break;
- }
- // Go to select screen
- else if (input == 2) {
- // Level select loop
- while (1) {
- // Show level select screen
- system("clear");
- cout <<
- "###########################################\n"
- "# #\n"
- "# LevelSelect #\n"
- "# #\n"
- "# 1. level1 6. level6 11. level11 #\n"
- "# 2. level2 7. level7 12. level12 #\n"
- "# 3. level3 8. level8 13. level13 #\n"
- "# 4. level4 9. level9 14. level14 #\n"
- "# 5. level5 10. level10 15. level15 #\n"
- "# #\n"
- "# 9 - Return to menu #\n"
- "###########################################\n\n";
- cout << "Enter a choice: ";
- // Get input
- cin >> input;
- // Go to selected map
- if (input >= 1 && input <= sizeof(Maps) / sizeof(*Maps)) {
- SetMap(input - 1);
- break;
- }
- // Return to menu
- else if (input == 9) {
- break;
- }
- // Invalid input
- else {
- cout << "That's not an option. Try again...\n";
- system("pause >nul");
- }
- }
- // Break menu loop
- if (input != 9)
- break;
- }
- // Show instructions
- else if (input == 3) {
- system("clear");
- cout <<
- "######################################\n"
- "# #\n"
- "# Instructions #\n"
- "# #\n"
- "# WASD -- Movement #\n"
- "# #\n"
- "# @ -- Player #\n"
- "# * -- Damage(-HP) #\n"
- "# + -- HP boost(+HP) #\n"
- "# K -- Keys #\n"
- "# : -- Door(Open this with Keys #\n"
- "# # -- Walls #\n"
- "# ? -- ? (WHAT?) #\n"
- "# #\n"
- "# Press any key to continue #\n"
- "######################################\n\n";
- system("read");
- }
- // Quit
- else if (input == 4)
- return 0;
- // Invalid input
- else {
- cout << "That's not an option. Try again...\n";
- system("pause >nul");
- }
- }
- // Game logic loop
- DrawMap();
- while (1) {
- // Get new coords
- cin >> move;
- int px2 = px, py2 = py;
- switch(move) {
- case 'd':
- px2 = px + 1;
- break;
- case 'a':
- px2 = px - 1;
- break;
- case 's':
- py2 = py + 1;
- break;
- case 'w':
- py2 = py - 1;
- break;
- }
- // Interact with objects
- // Spike
- if (Map.data[py2][px2] == '*') {
- Hp -= 5;
- // Game over if no hp
- if (Hp <= 0)
- break;
- }
- // Hp boost
- else if (Map.data[py2][px2] == '+') {
- Hp += 5;
- // Limit hp
- if (Hp >= MaxHp)
- Hp = MaxHp;
- }
- // Goal
- else if (Map.data[py2][px2] == '-') {
- // Quit game loop if out of maps
- if (Level + 1 >= sizeof(Maps) / sizeof(*Maps))
- break;
- // Go to next map
- SetMap(Level + 1);
- DrawMap();
- continue;
- }
- // Wall
- else if (Map.data[py2][px2] == '#') {
- px2 = px;
- py2 = py;
- }
- // Key
- else if (Map.data[py2][px2] == 'K')
- Keys++;
- // Door
- else if (Map.data[py2][px2] == ':') {
- if (!Keys) {
- px2 = px;
- py2 = py;
- } else
- Keys--;
- }
- else if(Map.data[py2][px2] == '?'){
- what();
- }
- // Move character
- Map.data[py][px] = ' ';
- Map.data[py2][px2] = '@';
- // Set new choords
- px = px2;
- py = py2;
- // Draw map
- DrawMap();
- // Sleep
- sleep(Gamespeed);
- }
- // Show game over screen if no hp
- if (Hp <= 0) {
- system("clear");
- cout << "\n\n\n"
- "###########\n"
- "#GAME OVER#\n"
- "###########\n";
- system("read");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment