drawpic.c

来自「这是一些c++例程」· C语言 代码 · 共 141 行

C
141
字号
#include "graphics.h"
#include "stdio.h"
#include "fcntl.h"
#include "stdlib.h"

#define VR_UP    0x4800
#define VR_LEFT  0x4b00
#define VR_RIGHT 0x4d00
#define VR_DOWN  0x5000
#define VR_ESC   0x011b

void main()
{
	void *dg[3],*fy;
	int driver = DETECT, mode, errorcode;
	int x=320,y=225;
	int x1,y1,x2,y2;
	int k, stat = 0;

	/* 初始化画图板 */
	initgraph(&driver, &mode, "");
	errorcode = graphresult();
	if (errorcode != grOk)  /* 出错处理 */
	{
		printf("Graphics error: %s\n", grapherrormsg(errorcode));
		printf("Press any key to halt:");
		getch();
		exit(1);
	}

	/* 3种画笔 */
	line(203,7,200,16);
	line(203,7,206,16);
	line(200,16,206,16);
	line(243,7,240,16);
	line(243,7,246,16);
	line(283,7,286,16);
	line(283,7,280,16);
	line(282,14,284,14);

	/* 保存画笔 */
	dg[0]=malloc(imagesize(200,7,206,16));
	dg[1]=malloc(imagesize(240,7,246,16));
	dg[2]=malloc(imagesize(280,7,286,16));
	getimage(200,7,206,16,dg[0]);
	getimage(240,7,246,16,dg[1]);
	getimage(280,7,286,16,dg[2]);

	/* 开辟暂存空间 */
	fy=malloc(imagesize(0,7,6,16));

	/* 清屏 */
	cleardevice();

	/* 画绘图筐 */
	setcolor(YELLOW);
	rectangle(4,19,637,447);

	x1=x-3;
	y1=y+1;
	x2=x+3;
	y2=y+10;
	getimage(x1,y1,x2,y2,fy);	/* 保存原图像 */
	putimage(x1,y1,dg[stat],COPY_PUT);	/* 绘制初始画笔 */

	/* 接受命令 */
	for (;;)
	{
		while(bioskey(1)==0);	/* 直到有键按下,往下执行 */
		k=bioskey(0);		/* 得到键值 */

		if ((k&0x00ff) == 0)	/* k1是一个特殊的关键值*/
		{
			putimage(x1,y1,fy,COPY_PUT);
			switch(k)
			{
			case VR_UP:	/* up */
				if (y>20)
					y=y-1;
				break;
			case VR_LEFT:	/* left */
				if (x>5)
					x=x-1;
				break;
			case VR_RIGHT:	/* right */
				if (x<636)
					x=x+1;
				break;
			case VR_DOWN:	/* down */
				if (y<446)
					y=y+1;
				break;
			}
			x1=x-3;
			y1=y+1;
			x2=x+3;
			y2=y+10;
			getimage(x1,y1,x2,y2,fy);	/* 保存原图像 */
			putimage(x1,y1,dg[stat],COPY_PUT);
			if(stat == 1)
				putpixel(x,y,RED);
			if(stat == 2)
				putpixel(x,y,BLACK);
		}
		else if(k == VR_ESC)	/* esc */
		{
			closegraph();
			return;
		}
		else
		{
			k = k & (0x00FF);
			switch(k)
			{
			case 118: 			/* 'v' */
			case 86: 			/* 'V' */
				stat = 0;
				putimage(x1,y1,dg[stat],COPY_PUT);
				break;
			case 119: 			/* 'w' */
			case 87: 			/* 'W' */
				stat = 1;
				putimage(x1,y1,dg[stat],COPY_PUT);
				putpixel(x,y,RED);
				break;
			case 114: 			/* 'r' */
			case 82: 			/* 'R' */
				stat = 2;
				putimage(x1,y1,dg[stat],COPY_PUT);
				putpixel(x,y,BLACK);
				break;
			case 99: 			/* 'c' */
			case 67: 			/* 'C' */
				setcolor(RED);
				circle(x,y,20);
				break;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?