控制台窗口中的 C++ 像素

2022-01-11 00:00:00 pixel console c++ codeblocks

在使用 Code::Blocks v10.05 的 C++ 中,如何在控制台屏幕上绘制单个像素?这很容易吗,还是只画一个矩形会更容易?如何给它上色?

In C++ using Code::Blocks v10.05, how do I draw a single pixel on the console screen? Is this easy at all, or would it be easier to just draw a rectangle? How do I color it?

很抱歉,我无法从 SOF、HF 甚至 cplusplus.com 获取任何代码来工作.这是屏幕上的超级马里奥世界人物.我认为是16位的游戏,适用于SNES系统.C::B 说我需要 C::B 的 SDK.它说afxwin.h";不存在.可以下载吗?

I'm sorry, but I just can't get any code from SOF, HF, or even cplusplus.com to work. This is for a Super Mario World figure on the screen. The game I think is 16-bit, and is for the SNES system. C::B says I need SDK for C::B. It says "afxwin.h" doesn't exist. Download maybe?

这就是我想要做的:

推荐答案

这取决于你的操作系统.我想你是在 Windows 平台上编程,因此你可以使用 SetPixel 但你必须使用"windows.h" 获取控制台句柄,这里以绘制 cos() 函数为例:

It depends on your OS. I suppose you are programming in a Windows platform, therefore you can use SetPixel but you have to use "windows.h" to get a console handle, so here an example for drawing the cos() function:

#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 等.

You can also use some others libraries like: conio.h allegro.h sdl, etc.

相关文章