0

我应该以我对 c++ 非常陌生的事实开始。

我正在尝试显示一个不断更新的 20x20 字符矩阵。目前,我正在使用 for 循环作为 cout (下面的代码)显示矩阵,但这非常闪烁 - 我正在寻找更平滑的东西。

有没有办法将此字符矩阵转换为图像并显示?

这是我在这里的第一个问题,所以如果我做错了什么,我深表歉意!

到目前为止的代码:

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

using namespace std;

int main()
{
    int randInt;

    //Initialize matrix and location
    int matrix[20][20];
    int location[2] = {0,0};
    for (int i=0; i<20; i++)
    {
            for (int j=0; j<20; j++)
            {
                matrix[i][j] = 1;
            }
    }


    //move the X around
    for (int i=0; i<100; i++)
    {
        cout << string(50, '\n');
        //Change the X's location
        randInt = rand() % 4;
        switch (randInt)
        {
            case 0:
                if(location[1] > 0)
                    location[1] = location[1]-1;
                break;
            case 1:
                if(location[0] < 20)
                    location[0] = location[0]+1;
                break;
            case 2:
                if(location[1] < 20)
                    location[1] = location[1]+1;
                break;
            case 3:
                if(location[0] > 0)
                    location[0] = location[0]-1;
                break;
            default:
                cout << "Switch statement problem";
        }
        //Display the matrix
        for (int x=0; x<20; x++)
        {
            for (int y=0; y<20; y++)
            {
                if(x==location[0] && y==location[1])
                    cout << "X";
                else
                    cout << matrix[x][y];
            }
            cout << endl;
        }
        Sleep(100);
    }

system ("pause");
return 0;
}
4

2 回答 2

0

If you want to convert the char to a image and see the color means, write the char value as pixel in simple pgm format.

Write a file in this sample format

P2
# feep.pgm
24 7
15
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

check this link http://netpbm.sourceforge.net/doc/pgm.html for pgm format

于 2013-04-25T05:32:41.113 回答
0

您应该将其重命名为location[2]`struct { int x,y; } 位置便于阅读。

然后,您可以在 RAM 中构建一个字符数组并立即将其输出。

int _tmain(int argc, _TCHAR* argv[])
{
    char matrix[20][20];
    char image[21][21];
    struct { int x, y; } location;

    int x = 0;
    int y = 0;
    location.x = 7;
    location.y = 3;

    // fill the matrix
    for (x = 0; x < 20; ++x)
    {
        for (y = 0; y < 20; ++y)
        {
            matrix[y][x] = 'a' + x + y;
        }
    }

    // prepare the image
    y = 0;

    while (y < 20)
    {
        memcpy(image[y], matrix[y], 20);
        image[y][20] = '\n';
        ++y;
    }

    // add the cross
    image[location.y][location.x] = 'X';
    image[20][0] = '\0';

    // use the image        
    puts((char*)image);
}

请根据需要添加随机功能。

于 2013-04-25T04:17:52.263 回答