Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.7 kB
6
Indexable
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;


void draw_map(int pos_x, int pos_y, int W, int H) {
    for (int i = 0; i < W; i++) {
        cout << "#";
    }
    cout << "\n";
    for (int h = 0; h < H - 2; h++) {
        cout << "#";
        for (int w = 0; w < W - 2; w++) {
            if (w == pos_x && h == pos_y) {
                cout << "$";
            }
            else {
                cout << ".";

            } 
        }  
        cout << "#" << '\n';
    }
    for (int i = 0; i < W; i++) {
        cout << "#";
    }
    cout << "\n";
    
}
void move(int& pos_x, int& pos_y, int speed,int W, int H) {
    char direction;
    if (_kbhit()) {
        direction = _getch();
        

        if (direction == 'w' || direction == 'H') {
            if ((pos_y - speed) > -1) {
                pos_y -= speed;
            }
        }if (direction == 's' || direction == 'P') {
            if ((pos_y + speed) < (H-2)) {
                pos_y += speed;
            }
        }if (direction == 'a' || direction == 'K') {
            if ((pos_x - speed) > -1) {
                pos_x -= speed;
            }
        }if (direction == 'd' || direction == 'M') {
            if ((pos_x + speed) < (W-2)) {
                pos_x += speed;
            }
        }
        system("cls");
        draw_map(pos_x, pos_y, W, H);
        cout << direction;
    }
}
int main()
{
    int W = 17;
    int H = 9;

    int pos_x = ((W - 2) / 2);
    int pos_y = ((H - 2) / 2);

    int speed = 1;
    draw_map(pos_x, pos_y, W, H);

    while (1) {
        move(pos_x, pos_y, speed,W,H);
    }
    
}