Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- // Initialized global variables
- int Gamespeed = 100;
- int HpStart = 1;
- int MaxHp = 100;
- // 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# # # # # ####### # # #",
- " #*# # ##### # # # # # # ###### #",
- " #*# # # ##### ### # # #",
- " #*# # ##### ### ##############",
- " #*# ##### ##### ####### #",
- " #*# # # ########## ####",
- "##*############### ##### #####",
- "#* *******###################**** #",
- "#* * * ******* #",
- "#** * * * ########:::######## ******* #",
- "#** * # # # # #",
- "########### #---# ###########"
- }}
- };
- // Uninitialized variables
- int Hp;
- int Level;
- int px, py;
- int Keys;
- // Map data
- struct Map_S Map;
- void setcolor(unsigned short color) {
- HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hcon, color);
- }
- 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("cls");
- // 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: ";
- setcolor(10);
- cout << Hp;
- setcolor(7);
- cout << " / ";
- setcolor(12);
- cout << MaxHp << endl;
- setcolor(10);
- cout << "Level: " << Level + 1;
- setcolor(7);
- }
- int main() {
- // Program loop
- while (1) {
- // Initialize vairables
- Hp = HpStart;
- Keys = 0;
- // Menu loop
- while (1) {
- // Show menu screen
- system("cls");
- setcolor(202);
- cout <<
- "######################################\n"
- "# #\n"
- "# MENU #\n"
- "# #\n"
- "# 1. Start Game #\n"
- "# 2. Level Select #\n"
- "# 3. Instructions #\n"
- "# 4. EXIT #\n"
- "# #\n"
- "# #\n"
- "######################################\n\n";
- setcolor(7);
- cout << "Enter a choice: ";
- // Get input
- int input;
- setcolor(10);
- cin >> input;
- setcolor(7);
- // 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("cls");
- setcolor(202);
- cout <<
- "######################################\n"
- "# #\n"
- "# LevelSelect #\n"
- "# #\n"
- "# 1. level1 #\n"
- "# 2. level2 #\n"
- "# 3. level3 #\n"
- "# 4. level4 #\n"
- "# #\n"
- "# #\n"
- "# 9 - Return to menu #\n"
- "######################################\n\n";
- setcolor(7);
- cout << "Enter a choice: ";
- // Get input
- setcolor(10);
- cin >> input;
- setcolor(7);
- // 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 {
- setcolor(10);
- cout << "That's not an option. Try again...\n";
- setcolor(7);
- system("pause >nul");
- }
- }
- // Break menu loop
- if (input != 9)
- break;
- }
- // Show instructions
- else if (input == 3) {
- system("cls");
- setcolor(202);
- cout <<
- "######################################\n"
- "# #\n"
- "# Instructions #\n"
- "# #\n"
- "# Blah blah. #\n"
- "# Blahdeliblah blaha #\n"
- "# Remember to blah the blaha #\n"
- "# And ablaha the hablahala. #\n"
- "# #\n"
- "# #\n"
- "# Press any key to continue #\n"
- "######################################\n\n";
- setcolor(7);
- system("pause >nul");
- }
- // Quit
- else if (input == 4)
- return 0;
- // Invalid input
- else {
- setcolor(10);
- cout << "That's not an option. Try again...\n";
- setcolor(7);
- system("pause >nul");
- }
- }
- // Game logic loop
- DrawMap();
- while (1) {
- // Get new coords
- int px2 = px, py2 = py;
- if (GetAsyncKeyState(VK_UP))
- py2 = py - 1;
- else if (GetAsyncKeyState(VK_DOWN))
- py2 = py + 1;
- else if (GetAsyncKeyState(VK_LEFT))
- px2 = px - 1;
- else if (GetAsyncKeyState(VK_RIGHT))
- px2 = px + 1;
- // Quit when esc is pressed
- else if (GetAsyncKeyState(VK_ESCAPE))
- break;
- // No logic is necessary unless a key is pressed
- else
- continue;
- // 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);
- } //Old Setup for 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--;
- }
- // 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("cls");
- setcolor(12);
- cout << "\n\n\n"
- "###########\n"
- "#GAME OVER#\n"
- "###########\n";
- setcolor(7);
- system("pause >nul");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment