car.c

来自「在C 下实现的一个小程序 在图形模式下的一个动画 汽车开动,上面还有月亮」· C语言 代码 · 共 70 行

C
70
字号
        # include<graphics.h>
# include<conio.h>
# define TIME 9999

moon(int x,int y,int r)
{ setcolor(YELLOW);
  arc(x,y,-120,120,r);
  arc(x-0.5*r,y,-90,90,0.87*r);
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(x+0.5*r,y,YELLOW);

}
truck(int dx,int color,int nu,int *p)
{ int i;
  for(i=0;i<2*nu;i+=2)
    *(p+i)+=dx-2;
    setcolor(color);
    setfillstyle(SOLID_FILL,color);
    fillpoly(8,p);

    fillellipse(*p+30,325,10,10);
    fillellipse(*p+100,325,10,10);

}

main()
{ int road[5][2]={0,300,639,300,639,349,0,349,0,300};
  int ptruck[8][2]={20,320,20,295,110,295,110,275,145,275,160,295,160,320,20,320};

  int i;
  int gd=VGA,gm=VGAMED;
  initgraph(&gd,&gm,"");
  cleardevice();
  setbkcolor(LIGHTBLUE);

  setcolor(GREEN);
  setfillstyle(SOLID_FILL,GREEN);
  fillpoly(5,road);


  moon(480,90,50);
  truck(5,RED,8,ptruck[0]);


  settextstyle(TRIPLEX_FONT,HORIZ_DIR,4);
  outtextxy(40,40,"Press any key to start");
  getch();

  for(i=0;i<=108&&!kbhit();i++)
  { setvisualpage(i&1);
    delay(TIME);
    setactivepage(!(i&1));

    cleardevice();
    setfillstyle(SOLID_FILL,GREEN);
    fillpoly(5,road);

    moon(480-3*i,90,50);

    truck(5,RED,8,ptruck[0]);


  }
  settextstyle(TRIPLEX_FONT,HORIZ_DIR,4);
  outtextxy(100,310,"Press any key to return");
  setvisualpage(0);
  getch();
  closegraph();

⌨️ 快捷键说明

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