[学习DOS下的图形操作]第二天 TC中像素的绘制

本文介绍了如何使用VGA调色板设置背景和前景颜色,并通过具体示例展示了如何利用C语言进行图形编程,包括随机更改背景颜色及绘制随机像素点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.设置背景和前景颜色
void setcolor( int color );
void setbkcolor( int color );
默认的VGA调色板支持16种颜色,分别为
0 BLACK 黑
1 BLUE 蓝
2 GREEN 绿
3 CYAN 青
4 RED 红
5 MAGENTA 洋红
6 BROWN 棕
7 LIGHTGAY 浅灰
8 DARKGRAY 深灰
9 LIGHTBLUE 淡蓝
10 LIGHTGREEN淡绿
11 LIGHTCYAN 淡青
12 LIGHTRED 淡红
13 LIGHTMAGENTA 淡洋红
14 YELLOW 黄
15 WHITE 白
练习1:
设置背景颜色为青色(测试一下,不翻书,把代码完整写下来)
#include <stdio.h>
#include <graphics.h>
int main(void)
{
int graph_driver = VGA;
int graph_mode = VGAHI;
char *graph_path = "D:\\DOS\\TC\\BGI";
initgraph( &graph_driver, &graph_mode, graph_path );
setbkcolor( CYAN );//或者使用setbkcolor(3);也可以
getch();
closegraph();
return 0;
}
练习2:
随机每隔一秒钟自动更换背景颜色,直到用户按键为止。
需要用到的函数有:
kbhit()如果返回0,表示没有键按下
srand()设置随机数的初始值
rand()返回一个随机数
time(NULL)返回当前的时间,整数的形式
sleep(1)睡眠
#include <stdio.h>
#include <graphics.h>
#include <dos.h>//sleep
#include <stdlib.h>//srand,rand
#include <time.h>//time
#include <conio.h>//kbhit
int main(void)
{
int bg_color = 0;
int graph_driver = VGA;
int graph_mode = VGAHI;
char *graph_path = "D:\\DOS\\TC\\BGI";
initgraph( &graph_driver, &graph_mode, graph_path );
srand( time(NULL) );
while( kbhit()==0 )
{

bg_color = rand( ) % 16 ;//0-15
setbkcolor( bg_color );
sleep(1);
}
return 0;
}
二、设置或提取屏幕上某一个像素点的颜色
void putpixel( int x, int y, int color );
把(x,y)点处的像素设置为color指定的颜色。
int getpixel( int x, int y );
获取(x,y)点处的颜色值。
练习:
随机画点程序,随机画不同颜色,不同为止的点.
#include <stdio.h>
#include <stdlib.h> // srand, rand
#include <time.h> // time
#include <conio.h> // kbhit
#include <graphics.h>
#include <dos.h>

int main(void)
{
int x, y , color;

int i,num;
int graph_driver = VGA;
int graph_mode = VGAHI;
char *graph_path = "D:\\DOS\\TC\\BGI";

initgraph( &graph_driver, &graph_mode, graph_path );
srand( time(NULL) );
setbkcolor( BLACK );
num = 3000;
while( kbhit() == 0 )
{
cleardevice();//清空屏幕
for( i = 0; i < num; i++ )
{
x = rand()%640;
y = rand()%480;
color = rand()%16+0;
putpixel( x, y, color );
}
sleep(0.1);
}
closegraph();
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值