Controlling the setcolor function

Oct 12, 2008 at 7:32pm
How I can do make it paint only the character in that loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <string>
#include <windows.h>
using namespace std;
void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}
int main()
{
    int x = 5, y = 4, x2, y2, px, py; //Relative to the character.
    int pathx = 10, pathy = 8;
    int height = 15, width = 20;
    int health = 20;
    char input;
    bool projectile_existence = false;
    int proj_life = y;
    srand ( time(NULL));
    x2 = rand() % 15 + 1;
    y2 = rand() % 20 + 1;
    do
    {
        system("CLS");
        for(int r = 0; r < height; ++r)
        {
            for(int c = 0; c < width; ++c)
                {
                if(r == y && c == x)
                {
                    cout << (char)1;
                }
                else if(r == y2 && c == x2)
                {
                    cout << (char)3;
                }
                else if(y == y2 && x == x2)
                {
                    health += 5;
                    x2 = rand () % 15 + 1;
                    y2 = rand () % 20 + 1;
                }
                else if(r == py && c == px)
                {
                    cout << "|";
                }
                else if(r == pathy && c == pathx)
                {
                    setcolor(2);
                    cout << (char)1;
                }
                else
                {
                    cout << '.';
                }
                }
            cout << endl;
        }
        cout << endl;
        cout << "Health:" << health;
        input = getch();
        switch(input)
        {
        case 'w': y--; break;
        case 'a': x--; break;
        case 's': y++; break;
        case 'd': x++; break;
        case 'f': projectile_existence = true;
        px = x; py = y; break;
        }
        py++;
    }
    
    while(input != 'q');
    return 0;
}
Last edited on Oct 12, 2008 at 7:32pm
Topic archived. No new replies allowed.