C++控制台绘图头文件实例代码
2026/5/22 22:59:06 网站建设 项目流程

这个头文件是用来在C++控制台绘图的,借鉴了pygame的一些思想,提供了一些函数用来绘图,可以有效的避免多次绘制重复图像时屏闪等问题。

函数介绍

1、init(int x, int fz)

初始化屏幕, 将屏幕大小设定为 x , 字体大小为 fz 。

2、fill(concol color)

用来将整个屏幕填充为一定的颜色。

3、update()

注意!绘画完后的图像并不会立刻出现在控制台上,使用这个函数来更新。

4、getmousepos(pos & p)

获取鼠标坐标并存到 p 中

5、gt(int x, int y)

将控制台光标移到(x,y)的位置,x,y 从1开始

6、HideCursor()

隐藏控制台光标

7、settextcolor(concol color)

设置文字颜色

8、setbackcolor(concol color)

设置背景颜色

9、rect(int sx, int sy, int ex, int ey, concol color)

以(sx, sy)为左上端点、(ex, ey)为右下端点,绘制颜色为color的矩形。

10、line(int sx, int sy, int ex, int ey, concol color)

绘制一条从(sx, sy)到(ex, ey)的线。

11、dot(int x, int y, concol color)

在(x,y)的位置画一个点

先介绍到这里

里面更多的功能、函数会在以后的文章后详细解释。

示例代码

我这里将头文件命名为Drawer.h,如果用了别的名字保存头文件,这里也要改过来。

功能是从(1,1)到鼠标的位置画线。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include "Drawer.h"

usingnamespacestd;

Pos mp;// Mouse_Pos

voidgame()

{

while(1)

{

getmousepos(mp);// Get Mouse Pos

fill(black);// Let Screen be black

line(1, 1, mp.x, mp.y, green);// from (1, 1) to (mp.x, mp.y) draw Line

// ^

// |

// You can change this to red, blue, white or more

update();// Update Screen

}

}

intmain(){

system("mode con cols=102 lines=52");

init(50, 16);// 50x50 Screen and font size is 16

game();

}

示例图片

截屏截不到鼠标。。。

头文件代码

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

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

#include <windows.h>

#include <algorithm>

#include <cstdio>

#include <cmath>

#include <ctime>

#ifndef KEY_DOWN

#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)

#endif

#define clean 0x2

#define box 0x4

#define full 0x1

usingnamespacestd;

enumconcol {

black = 0,

dark_blue = 1,

dark_green = 2,

dark_aqua = 3, dark_cyan = 3,

dark_red = 4,

dark_purple = 5, dark_pink = 5, dark_magenta = 5,

dark_yellow = 6,

dark_white = 7,

gray = 8,

blue = 9,

green = 10,

cyan = 11,

red = 12,

purple = 13, pink = 13,

yellow = 14,

white = 15

};

structpixel {

concol color;

};

structPos {

intx, y;

};

structPos3d

{

intx, y, z ;

};

CONSOLE_CURSOR_INFO CursorInfo;

COORD _GoToPos;

HANDLEhOut = GetStdHandle(STD_OUTPUT_HANDLE);

HWNDhwnd = GetForegroundWindow();

intbackcol, textcol;

pixel _LastScreen[1000][1000];

pixel _NewScreen[1000][1000];

int_ScreenSideLength;

intPy3dx = 0, Py3dy = 0 ;

intxzlen = 2, yzlen = 1 ;

intfontsize = 16;

inlinevoidgetmousepos(Pos &p) {

POINT pt;

GetCursorPos(&pt);

ScreenToClient(hwnd, &pt);

p.y = (pt.y / fontsize + 0.5);

p.x = (pt.x / fontsize + 0.5);

}

inlinevoidgt(shortx,shorty) {

--x;

--y;

_GoToPos = {x * xzlen, y * yzlen};

SetConsoleCursorPosition(hOut, _GoToPos);

}

inlinevoidHideCursor()

{

GetConsoleCursorInfo(hOut, &CursorInfo);

CursorInfo.bVisible =false;

SetConsoleCursorInfo(hOut, &CursorInfo);

}

inlinevoidsettextcolor(concol textcolor) {

textcol = textcolor;

unsignedshortwAttributes = ((unsignedint)backcol << 4) | (unsignedint)textcol;

SetConsoleTextAttribute(hOut, wAttributes);

}

inlinevoidsetbackcolor(concol backcolor) {

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

backcol = backcolor;

unsignedshortwAttributes = ((unsignedint)backcol << 4) | (unsignedint)textcol;

SetConsoleTextAttribute(hOut, wAttributes);

}

structButton {

Pos mp;

constchar* name;

intlen, qx, px, py;

voidrename(constchar* ne) {

name = ne;

len =strlen(ne);

}

voidsetpos(intx,inty) {

py = y;

qx = (x - len / 2);

px = (x + len / 2);

gt(qx, y);

printf(name);

}

boolcheck() {

getmousepos(mp);

if(mp.x <= px and mp.x >= qx and mp.y <= py and mp.y >= py - 2) {

gt(qx - 2, py);

printf(">>");

gt(px, py);

printf("<<");

if(KEY_DOWN(MOUSE_MOVED))returntrue;

}else{

gt(qx - 2, py);

printf(" ");

gt(px, py);

printf(" ");

}

returnfalse;

}

};

inlinevoidinit(intx,intfz) {

fontsize = fz;

GetConsoleCursorInfo(hOut, &CursorInfo);

CursorInfo.bVisible =false;

SetConsoleCursorInfo(hOut, &CursorInfo);

_ScreenSideLength = x;

}

inlinevoidupdate() {

for(inti = 1; i <= _ScreenSideLength ; ++i) {

for(intj = 1; j <= _ScreenSideLength ; ++j) {

if(_LastScreen[j][i].color != _NewScreen[j][i].color) {

gt(j, i);

setbackcolor(_NewScreen[j][i].color);

for(inti=1;i<=yzlen;++i)

{

for(intj=1;j<=xzlen;++j)

putchar(' ');

puts("\n");

}

_LastScreen[j][i] = _NewScreen[j][i];

}

}

}

}

voidfill(concol color) {

for(inti = 1; i <= _ScreenSideLength; ++i) {

for(intj = 1; j <= _ScreenSideLength; ++j) {

if(_NewScreen[j][i].color != color) {

_NewScreen[j][i].color = color;

}

}

}

}

voidrect(intsx,intsy,intex,intey, concol color) {

if(ey < sy) swap(ey, sy);

if(ex < sx) swap(ex, sx);

for(inti = sy; i <= ey; ++i) {

for(intj = sx; j <= ex; ++j) {

_NewScreen[j][i].color = color;

}

}

}

voidChangePy(intx,inty)

{

Py3dx = x;

Py3dy = y;

}

inlinevoiddot(intx,inty, concol color) {

_NewScreen[x][y].color = color;

}

inlinevoidline(intsx,intsy,intex,intey, concol color)

{

intxlen = ex - sx ;

intylen = ey - sy ;

intlen =sqrt(pow(xlen, 2) +pow(ylen, 2)) ;

for(doublei=0; i<=len; i += 1)

{

intx = xlen * i / len ;

inty = ylen * i / len ;

_NewScreen[x + sx][y + sy].color = color ;

}

}

inlinePos pos3t2(Pos3d pos)

{

if(pos.z == 0)

{

returnPos {pos.x, pos.y} ;

}

returnPos{pos.x / (log10(pos.z)) + Py3dx, pos.y / (log10(pos.z)) + Py3dy} ;

}

inlinevoidline3d(Pos3d a, Pos3d b, concol color)

{

Pos a2 = pos3t2(a);

Pos b2 = pos3t2(b);

// line(a2.x, b2.y, a2.y, b2.x, color);

line(a2.x, a2.y, b2.x, b2.y, color);

}

inlineintdis3d(Pos3d a, Pos3d b)

{

intx =abs(a.x - b.x);

inty =abs(a.y - b.y);

intz =abs(a.z - b.z);

returnsqrt(pow(x, 2) +pow(y, 2) +pow(z, 2)) ;

}

/*

Q------W

\ /

A--S

*/

inlinevoidrect3d(Pos3d q, Pos3d w, Pos3d a, Pos3d s, concol color)

{

intqlen = dis3d(q, a);

intplen = dis3d(w, s);

intlen = max(qlen, plen);

for(doublei=1;i<=len; i+=0.1)

{

Pos3d qpos = Pos3d {abs(q.x-a.x)*i/len + q.x,abs(q.y-a.y)*i/len + q.y,abs(q.z-a.z)*i/len + q.z} ;

Pos3d ppos = Pos3d {abs(w.x-s.x)*i/len + w.x,abs(w.y-s.y)*i/len + w.y,abs(w.z-s.z)*i/len + w.z} ;

line3d(qpos, ppos, color);

}

}

structcube

{

Pos3d pos ;

intxlen, ylen, zlen ;

voidinit(intxp,intyp,intzp,intxl,intyl,intzl)

{

pos = Pos3d {xp - Py3dx, yp - Py3dy, zp};

xlen = xl;

ylen = yl;

zlen = zl;

}

inlinePos3d getdotpos(intdoti)

{

switch(doti)

{

case8:returnPos3d {pos.x + xlen, pos.y + ylen, pos.z + zlen} ;

case7:returnPos3d {pos.x, pos.y + ylen, pos.z + zlen} ;

case6:returnPos3d {pos.x + xlen, pos.y, pos.z + zlen} ;

case4:returnPos3d {pos.x + xlen, pos.y + ylen, pos.z} ;

case5:returnPos3d {pos.x, pos.y, pos.z + zlen} ;

case3:returnPos3d {pos.x, pos.y + ylen, pos.z} ;

case2:returnPos3d {pos.x + xlen, pos.y, pos.z} ;

case1:returnPos3d {pos.x, pos.y, pos.z} ;

}

return{0, 0, 0};//Else

}

/*

5----------6

/| /|

/ | / |

/ | / |

1----------2 |

| 7------|---8

ylen| / | /

| / | /zlen

|/ xlen |/

3----------4

^

|

O

*/

inlinevoiddraw(concol color,intmode)

{

if(mode & full)

{

rect3d(getdotpos(5), getdotpos(7), getdotpos(6), getdotpos(8), green);

rect3d(getdotpos(1), getdotpos(3), getdotpos(5), getdotpos(7), red);

rect3d(getdotpos(5), getdotpos(1), getdotpos(6), getdotpos(2), blue);

rect3d(getdotpos(7), getdotpos(3), getdotpos(8), getdotpos(4), pink);

rect3d(getdotpos(2), getdotpos(4), getdotpos(6), getdotpos(8), yellow);

rect3d(getdotpos(1), getdotpos(3), getdotpos(2), getdotpos(4), white);

}

if(mode & clean)

{

line3d(getdotpos(1), getdotpos(2), color) ;

line3d(getdotpos(1), getdotpos(3), color) ;

line3d(getdotpos(1), getdotpos(5), color) ;

line3d(getdotpos(2), getdotpos(6), color) ;

line3d(getdotpos(2), getdotpos(4), color) ;

line3d(getdotpos(3), getdotpos(4), color) ;

line3d(getdotpos(3), getdotpos(7), color) ;

line3d(getdotpos(4), getdotpos(8), color) ;

line3d(getdotpos(5), getdotpos(6), color) ;

line3d(getdotpos(5), getdotpos(7), color) ;

line3d(getdotpos(6), getdotpos(8), color) ;

line3d(getdotpos(7), getdotpos(8), color) ;

}

if(mode & box)

{

line3d(getdotpos(1), getdotpos(4), color) ;

line3d(getdotpos(1), getdotpos(7), color) ;

line3d(getdotpos(2), getdotpos(3), color) ;

line3d(getdotpos(2), getdotpos(8), color) ;

line3d(getdotpos(3), getdotpos(5), color) ;

line3d(getdotpos(3), getdotpos(8), color) ;

line3d(getdotpos(4), getdotpos(7), color) ;

line3d(getdotpos(4), getdotpos(6), color) ;

line3d(getdotpos(5), getdotpos(2), color) ;

line3d(getdotpos(5), getdotpos(8), color) ;

line3d(getdotpos(6), getdotpos(1), color) ;

line3d(getdotpos(6), getdotpos(7), color) ;

}

}

};

总结

到此这篇关于C++控制台绘图头文件的文章就介绍到这了,

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询