lundi 5 novembre 2018

How to stop blue lines from appearing on a Windows console during an ASCII game?

I've run into a very unusual problem. So unusual in fact, that I haven't found even a mention of anything like it on google. Here's the deal. I was working on making a ASCII based computer game for a programming class. I had created a simple program that moved a one character rectangle around the screen when you pressed the four arrow keys. But, I quickly noticed that when the rectangle moved right, it left a trail of vertical blue lines in it's wake. I have absolutely no idea why this is happening, and as mentioned, google doesn't seem to have the answers. So I'm wondering if their is anyway to fix this problem. If you want some technical information, I am running this on Windows 10 with Dev C++.

#include <ctime>
#include <iostream>
#include <windows.h>

void writeToConsole(char chr, COORD pos) {
    static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    std::cout.flush(); 
    // makes sure not changing cursor during cout
    SetConsoleCursorPosition(hOut, pos); 
    // sets where next characters are printed
    std::cout << chr;
}

int main(){
    COORD squareXY = {0, 0};
    const int width = 30;
    for (int i = 0; i < width; i++)
    // prints spaces that will be overwritten
        std::cout << ' ';
    writeToConsole((char)219, squareXY);
    // (char)219 is a solid rectangle
    int lastTimeMove = clock();
    // time measured in clocks, not second
    while (true) {
        if ((clock() - lastTimeMove) > .1 * CLOCKS_PER_SEC) {
            // trigger approximately every tenth of a second
            writeToConsole(' ', squareXY);
            // erase previous
            squareXY.X = (squareXY.X  + 1) % width;
            writeToConsole((char)219, squareXY);
            lastTimeMove = clock();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire