首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

这是小弟我编的时钟程序,为什么指针有时会闪烁,小弟我知道应该用双缓冲,但要如何写啊请各位大神说一下。

2013-12-28 
这是我编的时钟程序,为什么指针有时会闪烁,我知道应该用双缓冲,但要怎么写啊,请各位大神说一下。。。#include

这是我编的时钟程序,为什么指针有时会闪烁,我知道应该用双缓冲,但要怎么写啊,请各位大神说一下。。。
#include<stdio.h>
#include"graphics.h"
#include<time.h>
#include<conio.h>
#include"math.h" 
#include"dos.h" 
#define pi 3.1415926 
void set1()//画外围圈
{
  setcolor(YELLOW);
    setlinestyle(0,0,3);
    circle(300,240,200);
    setcolor(WHITE);
    circle(300,240,220);
    setfillstyle(1,3);
    pieslice(300,30,0,360,10);
    setfillstyle(1,2);
    pieslice(300,450,0,360,10);
}

void set2()//画刻盘
{    
     int i,l,x1,x2,y1,y2;
     for(i=0;i<60;i++)
{
    if(i%5==0)
    l=15; 
    else 
    l=5; 
    x1=200*sin(i*6*pi/180)+300; 
    y1=200*cos(i*6*pi/180)+240; 
    x2=(200-l)*sin(i*6*pi/180)+300; 
    y2=(200-l)*cos(i*6*pi/180)+240; 
    setcolor(YELLOW);
    line(x1,y1,x2,y2);

    }

}
void set3()//填数字
{
   setcolor(GREEN);
    settextstyle(0,0,2);
    outtextxy(290,80,"12");
    outtextxy(375,100,"1");
    outtextxy(430,160,"2");
    outtextxy(460,230,"3");
    outtextxy(430,310,"4");
    outtextxy(380,370,"5");
    outtextxy(295,390,"6");
    outtextxy(220,370,"7");
    outtextxy(160,310,"8");
    outtextxy(140,230,"9"); 
    outtextxy(157,155,"10");
    outtextxy(215,95,"11");

}

int main()
{
     int drive,mode;
      int n1,n2,n3,h1,m1,s1,h2,m2,s2;
      struct time *p;
    drive=DETECT;
    initgraph(&drive,&mode,"");
    cleardevice();
    set1();
    set2();
    set3();
    
    while(!kbhit())
   {
     time_t t;
     t=time(NULL);
     p=localtime(&t);
     n1=p->tm_sec;
     n2=p->tm_min;
     n3=p->tm_hour;

     sound(1000);
     delay(1000);
     sound(1000);
     nosound();
     setfillstyle(1,0);
      bar(200,150,290,200);

     gotoxy(27,11);
     printf("%d-%d-%d",1900+p->tm_year,1+p->tm_mon,p->tm_mday);
     gotoxy(27,12);
     printf("%d:%d:%d",p->tm_hour,p->tm_min,p->tm_sec);
     h1=300+100*sin((n3+n2/60.0)*2*pi/12);
     m1=300+150*sin(n2*2*pi/60);
     s1=300+180*sin(n1*2*pi/60);
     h2=240-100*cos((n3+n2/60.0)*2*pi/12);
     m2=240-150*cos(n2*2*pi/60);
     s2=240-180*cos(n1*2*pi/60);
     setcolor(YELLOW);
     line(300,240,h1,h2);
     setcolor(RED);
     line(300,240,m1,m2);
     setcolor(BLUE);
     line(300,240,s1,s2);

     sleep(1);
     setcolor(BLACK);
     line(300,240,h1,h2);
     line(300,240,m1,m2);
     line(300,240,s1,s2);
     }

    getch();
    closegraph();
    return 0;

}

[解决办法]
"我知道应该用双缓冲,但要怎么写啊"
你这个还真是矛盾呀, 又知道, 又不知道?

双缓冲也就把要绘制的东西先绘制到内存中, 再一次绘制到屏幕上.
------解决方案--------------------


画图程序闪烁不一定是绘制速度的问题,所以双缓冲不一定能解决闪烁问题,很多闪烁问题可能是由于不断地背景切换造成的,可以从这方面找原因和解决方法
[解决办法]
仅供参考:

#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <conio.h>
#include <time.h>
#include <math.h>
time_t t;
char timestr1[30];//DDD MMM dd hh:mm:ss YYYY
char timestr2[30];//DDD MMM dd hh:mm:ss YYYY
char hhmmss[9];
int graphdriver,graphmode,page;
int i,xo,yo,r0,r1,r2,r3,r4,r5,r6,r7;
int x1,y1,x2,y2;
int hh,mm,ss;
double c,s;
int xasp,yasp;
void main() {
    xo=60;yo=60;
    r0=5; //轴
    r1=20;//时针
    r2=36;//分针
    r3=45;//秒针
    r4=50;//时刻度
    r5=53;//分刻度
    r6=55;//刻度外
    r7=59;//表盘
    graphdriver=VGA;
    graphmode=VGAMED;
    initgraph(&graphdriver,&graphmode,"D:\\BC\\BGI");
    getaspectratio(&xasp, &yasp);
    page=0;
    timestr2[0]=0;
    while (1) {
        if (kbhit()) break;
        time(&t);
        strcpy(timestr1,ctime(&t));
        if (strcmp(timestr1,timestr2)) {
            strcpy(timestr2,timestr1);
            hh=atoi(timestr2+11);
            mm=atoi(timestr2+14);
            ss=atoi(timestr2+17);
            setvisualpage(page);
            setactivepage(1-page);
            cleardevice();
            sprintf(hhmmss,"%02d:%02d:%02d",hh,mm,ss);
            outtextxy(28,0,hhmmss);
            pieslice(xo,yo,0,360,r0);
            circle(xo,yo,r7);
            for (i=0;i<60;i++) {
                c=cos(i*6*3.14159265/180);
                s=sin(i*6*3.14159265/180)*xasp/yasp;
                if (0==(i%5)) {
                    x1=xo+r4*c;
                    y1=yo+r4*s;
                } else {
                    x1=xo+r5*c;
                    y1=yo+r5*s;
                }
x2=xo+r6*c;
y2=yo+r6*s;
line(x1,y1,x2,y2);
if (((hh%12)*5+mm/12+45)%60==i) {
x2=xo+r1*c;
y2=yo+r1*s;
setlinestyle(0,-1,3);
line(xo,yo,x2,y2);
setlinestyle(0,-1,1);
}
if ((mm+45)%60==i) {
x2=xo+r2*c;
y2=yo+r2*s;
setlinestyle(0,-1,3);
line(xo,yo,x2,y2);
setlinestyle(0,-1,1);
}
if ((ss+45)%60==i) {
x2=xo+r3*c;
y2=yo+r3*s;
line(xo,yo,x2,y2);
}
}
page=1-page;
}
        delay(100);
    }
    getch();
    closegraph();
}

热点排行