我應該打開這個事實,我對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;
}
如果您正在使用UNIX,看看詛咒 – didierc 2013-04-25 03:45:50
'系統(「暫停」);'指示的Windows和Visual Studio。嘗試Ctrl-F5而不是暫停的調用。 – harper 2013-04-25 03:53:02
您是否試圖在圖像中顯示矩陣的條目(即,您可以看到實際的數字),還是試圖通過對數字進行顏色編碼來了解矩陣中的內容(即,0是純黑色的255是純白色的)?如果是後者,像OpenCV這樣的圖像處理庫可能會有用。 – lightalchemist 2013-04-25 04:10:10