Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Created March 28, 2020 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProfAndreaPollini/dcd06d8e539f84cfc610eb765a9ddb4a to your computer and use it in GitHub Desktop.
Save ProfAndreaPollini/dcd06d8e539f84cfc610eb765a9ddb4a to your computer and use it in GitHub Desktop.
dungeon
// Dungeon.cpp : Questo file contiene la funzione 'main', in cui inizia e termina l'esecuzione del programma.
//
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
constexpr auto W = 80;
constexpr auto H = 30;
/*
.............
.....(i,j)........
.............
.............
*/
char map[W][H];
void init_map() {
for (auto y = 0; y < H; y++) {
for (auto x = 0; x < W; x++) {
map[y][x] = '.';
}
}
}
/**
clear the console using the Win32 console API
ref: https://docs.microsoft.com/en-us/windows/console/console-reference
source: https://stackoverflow.com/a/6487534
*/
void clear() {
COORD topLeft = { 0, 0 };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen;
DWORD written;
GetConsoleScreenBufferInfo(console, &screen);
FillConsoleOutputCharacterA(
console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
FillConsoleOutputAttribute(
console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
SetConsoleCursorPosition(console, topLeft);
}
void print_entity(int , int , char );
void clamp(int*, int, int);
void clamp2(int&, int, int);
struct Entity {
int x;
int y;
char glyph;
};
int main()
{
vector<Entity> entities{};
//char hero_glyph = '@';
//int hero_x = 5,hero_y=7;
Entity hero{5,7,'@'};
Entity orc{ 10,10,'*' };
entities.push_back(hero);
entities.push_back(orc);
cout << hero.glyph;
bool running = true;
cout << "Il nostro eroe " << hero.glyph << " ti saluta!\n";
cout << "The end\n";
// init mappa
init_map();
// posiziono eroe sulla mappa
while (running) {
clear();
//print_entity(hero_x, hero_y, hero_glyph);
//stampa mappa
for (auto y = 0; y < H; y++) {
for (auto x = 0; x < W; x++) {
bool entity_found = false;
for (Entity e : entities) {
if (e.x == x && e.y == y) {
cout << e.glyph;
entity_found = true;
break;
}
}
if (!entity_found)
cout << map[y][x];
}
cout << endl;
}
char cmd;
cout << endl << "> ";
cin >> cmd;
if (cmd == 'q') { // quit command
running = false;
}
else if(cmd == 'd') {
hero.x++;
clamp(&hero.x, 0, W-1);
}
else if (cmd == 'a') {
hero.x--;
clamp2(hero.x, 0, W - 1);
}
else if (cmd == 'w') {
hero.y--;
clamp(&hero.y, 0, H - 1);
}
else if (cmd == 's') {
hero.y++;
clamp(&hero.y, 0, H - 1);
}
}
return 0;
}
void clamp(int* value, int min_value, int max_value) {
if (*value <= min_value) {
*value = min_value;
return;
}
if (*value >= max_value) {
*value = max_value;
return;
}
}
void clamp2(int& value, int min_value, int max_value) {
if (value <= min_value) {
value = min_value;
return;
}
if (value >= max_value) {
value = max_value;
return;
}
}
void print_entity(int x, int y, char glyph) {
map[y][x] = glyph;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment