在C++中使用Code::Blocks v10.05,我如何在控制台屏幕上绘制单个像素?这很容易吗?或者只是绘制一个矩形会更容易?我如何给它上色?对不起,但我只是不能从SOF,HF,甚至cplusplus.com获得任何代码来工作。这是一个超级马里奥世界在屏幕上的数字。游戏我认为是16位的,是为SNES系统。C::B说我需要C::B的SDK。它说“afxwin.h”不存在。下载也许?这就是我想做的
的数据
vwkv1x7d1#
这取决于你的操作系统。我假设你是在Windows平台上编程,因此你可以使用SetPixel,但你必须使用“windows.h”来获取控制台句柄,所以这里有一个绘制cos()函数的例子:
#include<windows.h>#include<iostream>#include <cmath>using namespace std;#define PI 3.14int main() { //Get a console handle HWND myconsole = GetConsoleWindow(); //Get a handle to device context HDC mydc = GetDC(myconsole); int pixel =0; //Choose any color COLORREF COLOR= RGB(255,255,255); //Draw pixels for(double i = 0; i < PI * 4; i += 0.05) { SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR); pixel+=1; } ReleaseDC(myconsole, mydc); cin.ignore(); return 0;}
#include<windows.h>
#include<iostream>
#include <cmath>
using namespace std;
#define PI 3.14
int main()
{
//Get a console handle
HWND myconsole = GetConsoleWindow();
//Get a handle to device context
HDC mydc = GetDC(myconsole);
int pixel =0;
//Choose any color
COLORREF COLOR= RGB(255,255,255);
//Draw pixels
for(double i = 0; i < PI * 4; i += 0.05)
SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
pixel+=1;
}
ReleaseDC(myconsole, mydc);
cin.ignore();
return 0;
字符串您也可以使用其他一些库,如:conio.h allegro.h sdl等。
ioekq8ef2#
如果你想让图像看起来像块,你可以利用console code page的块字符。
█
▄
▀
通过将半块与colored text结合使用,您可以将80×25的控制台窗口变成80×50的16色显示(这是QBasic版本的Nibbles所使用的方法)。然后,您只需将图像转换为16色调色板和合理的小尺寸。
6qftjkof3#
windows.h提供了一个函数SetPixel(),用于在窗口的指定位置打印一个像素。该函数的一般形式为
windows.h
SetPixel()
SetPixel(HDC hdc, int x, int y, COLORREF& color);
字符串其中,x和y是要显示的像素的坐标,颜色是像素的颜色。
重要:要使用Code::blocks IDE在您的机器上打印像素,请在链接器设置中添加链接库libgdi32.a(通常在MinGW\lib内部)。
libgdi32.a
MinGW\lib
f0ofjuux4#
控制台是一个文本设备,所以一般来说你不会写到单个像素。你可以创建一个特殊的字体,并选择它作为控制台的字体,但它将是单色的。有一些库可以简化控制台UI的编写(例如Curses),但我相信你也有更多的游戏功能,而不仅仅是显示一个精灵。如果你想写一个游戏,我建议你看看一些图形/游戏框架/库,例如SDL
n3ipq98p5#
我在code::blocks中使用windows.h绘制了直线。我不能详细解释它,但我可以提供一个代码和过程来编译它。1.进入设置菜单并选择编译器和调试器。1.点击链接器选项卡,并添加一个链接库libgdi32.a,它位于C:\Program Files\CodeBlocks\MinGW\lib目录。现在编译这个程序
#include <windows.h>#include <cmath>#define ROUND(a) ((int) (a + 0.5))/* set window handle */static HWND sHwnd;static COLORREF redColor=RGB(255,0,0);static COLORREF blueColor=RGB(0,0,255);static COLORREF greenColor=RGB(0,255,0);void SetWindowHandle(HWND hwnd){sHwnd=hwnd;}/* SetPixel */void setPixel(int x,int y,COLORREF& color=redColor){if(sHwnd==NULL){ MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR); exit(0);}HDC hdc=GetDC(sHwnd);SetPixel(hdc,x,y,color);ReleaseDC(sHwnd,hdc);return;// NEVERREACH //}void drawLineDDA(int xa, int ya, int xb, int yb){ int dx = xb - xa, dy = yb - ya, steps, k; float xIncrement, yIncrement, x = xa, y = ya; if(abs(dx) > abs(dy)) steps = abs(dx); else steps = abs(dy); xIncrement = dx / (float) steps; yIncrement = dy / (float) steps; setPixel(ROUND(x), ROUND(y)); for(int k = 0; k < steps; k++){ x += xIncrement; y += yIncrement; setPixel(x, y); }}/* Window Procedure WndProc */LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){ switch(message){ case WM_PAINT: SetWindowHandle(hwnd); drawLineDDA(10, 20, 250, 300); break; case WM_CLOSE: // FAIL THROUGH to call DefWindowProc break; case WM_DESTROY: PostQuitMessage(0); return 0; default: break; // FAIL to call DefWindowProc // } return DefWindowProc(hwnd,message,wParam,lParam);}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){static TCHAR szAppName[] = TEXT("Straight Line");WNDCLASS wndclass;wndclass.style = CS_HREDRAW|CS_VREDRAW ;wndclass.lpfnWndProc = WndProc ;wndclass.cbClsExtra = 0 ;wndclass.cbWndExtra = 0 ;wndclass.hInstance = hInstance ;wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;wndclass.lpszMenuName = NULL ;wndclass.lpszClassName = szAppName ;// Register the window //if(!RegisterClass(&wndclass)){ MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR); exit(0);}// CreateWindow //HWND hwnd=CreateWindow(szAppName,"DDA - Programming Techniques", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);if(!hwnd){ MessageBox(NULL,"Window Creation Failed!","Error",MB_OK); exit(0); } // ShowWindow and UpdateWindow // ShowWindow(hwnd,iCmdShow); UpdateWindow(hwnd); // Message Loop // MSG msg; while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg); DispatchMessage(&msg); } /* return no error to the operating system */ return 0;}
#include <windows.h>
#define ROUND(a) ((int) (a + 0.5))
/* set window handle */
static HWND sHwnd;
static COLORREF redColor=RGB(255,0,0);
static COLORREF blueColor=RGB(0,0,255);
static COLORREF greenColor=RGB(0,255,0);
void SetWindowHandle(HWND hwnd){
sHwnd=hwnd;
/* SetPixel */
void setPixel(int x,int y,COLORREF& color=redColor){
if(sHwnd==NULL){
MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);
exit(0);
HDC hdc=GetDC(sHwnd);
SetPixel(hdc,x,y,color);
ReleaseDC(sHwnd,hdc);
return;
// NEVERREACH //
void drawLineDDA(int xa, int ya, int xb, int yb){
int dx = xb - xa, dy = yb - ya, steps, k;
float xIncrement, yIncrement, x = xa, y = ya;
if(abs(dx) > abs(dy)) steps = abs(dx);
else steps = abs(dy);
xIncrement = dx / (float) steps;
yIncrement = dy / (float) steps;
setPixel(ROUND(x), ROUND(y));
for(int k = 0; k < steps; k++){
x += xIncrement;
y += yIncrement;
setPixel(x, y);
/* Window Procedure WndProc */
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
switch(message){
case WM_PAINT:
SetWindowHandle(hwnd);
drawLineDDA(10, 20, 250, 300);
break;
case WM_CLOSE: // FAIL THROUGH to call DefWindowProc
case WM_DESTROY:
PostQuitMessage(0);
default:
break; // FAIL to call DefWindowProc //
return DefWindowProc(hwnd,message,wParam,lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
static TCHAR szAppName[] = TEXT("Straight Line");
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW|CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
// Register the window //
if(!RegisterClass(&wndclass)){
MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);
// CreateWindow //
HWND hwnd=CreateWindow(szAppName,"DDA - Programming Techniques",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
NULL,
hInstance,
NULL);
if(!hwnd){
MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);
// ShowWindow and UpdateWindow //
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
// Message Loop //
MSG msg;
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
/* return no error to the operating system */
字符串在这个程序中我使用了DDA直线绘制算法。像素绘制任务是通过setPixel(ROUND(x),ROUND(y))函数来完成的。这是windows编程,你可以学习详细的here
c6ubokkw6#
在CodeBlocks中使用我发现了这个(你必须添加一个链接器选项-lgdi32):
-lgdi32
//Code Blocks: Project Build Options Linker settings Othoer linker options: add -lgdi32
字符串我忘了:你必须在包含windows.h之前加上这个:
#define _WIN32_WINNT 0x0500
型整个余弦代码。准备编译:
//Code Blocks: Project Build Options Linker settings Othoer linker options: add -lgdi32#define _WIN32_WINNT 0x0500#include "windows.h"#include <iostream>#include <cmath>using namespace std;#define PI 3.14int main(){ HWND myconsole = GetConsoleWindow(); HDC mydc = GetDC(myconsole); int pixel =0; COLORREF COLOR= RGB(255,255,255); //Draw pixels for(double i = 0; i < PI * 4; i += 0.05) { SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR); pixel+=1; } ReleaseDC(myconsole, mydc); cin.ignore(); return 0;}
#include "windows.h"
#include <iostream>
int main(){
型
6条答案
按热度按时间vwkv1x7d1#
这取决于你的操作系统。我假设你是在Windows平台上编程,因此你可以使用SetPixel,但你必须使用“windows.h”来获取控制台句柄,所以这里有一个绘制cos()函数的例子:
字符串
您也可以使用其他一些库,如:conio.h allegro.h sdl等。
ioekq8ef2#
如果你想让图像看起来像块,你可以利用console code page的块字符。
█
= '\xDB' = U+2588完整数据块▄
= '\xDC' = U+2584下半块▀
= '\xDF' = U+2580上半块通过将半块与colored text结合使用,您可以将80×25的控制台窗口变成80×50的16色显示(这是QBasic版本的Nibbles所使用的方法)。
然后,您只需将图像转换为16色调色板和合理的小尺寸。
的数据
6qftjkof3#
windows.h
提供了一个函数SetPixel()
,用于在窗口的指定位置打印一个像素。该函数的一般形式为字符串
其中,x和y是要显示的像素的坐标,颜色是像素的颜色。
重要:要使用Code::blocks IDE在您的机器上打印像素,请在链接器设置中添加链接库
libgdi32.a
(通常在MinGW\lib
内部)。f0ofjuux4#
控制台是一个文本设备,所以一般来说你不会写到单个像素。你可以创建一个特殊的字体,并选择它作为控制台的字体,但它将是单色的。有一些库可以简化控制台UI的编写(例如Curses),但我相信你也有更多的游戏功能,而不仅仅是显示一个精灵。
如果你想写一个游戏,我建议你看看一些图形/游戏框架/库,例如SDL
n3ipq98p5#
我在code::blocks中使用windows.h绘制了直线。我不能详细解释它,但我可以提供一个代码和过程来编译它。
1.进入设置菜单并选择编译器和调试器。
1.点击链接器选项卡,并添加一个链接库libgdi32.a,它位于C:\Program Files\CodeBlocks\MinGW\lib目录。
现在编译这个程序
字符串
在这个程序中我使用了DDA直线绘制算法。像素绘制任务是通过setPixel(ROUND(x),ROUND(y))函数来完成的。这是windows编程,你可以学习详细的here
c6ubokkw6#
在CodeBlocks中使用我发现了这个(你必须添加一个链接器选项
-lgdi32
):字符串
我忘了:你必须在包含
windows.h
之前加上这个:型
整个余弦代码。准备编译:
型