`
maidingding
  • 浏览: 13044 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

回首在看C,C让我紧张了把,不错有功底,心头还是会微笑

阅读更多

         C才是我们进入编程的首要课程,但是几年C忘的差不多了,win_tc上原来写的程式居然有些看不懂了。心里不湿为了流冷汗,万一哪天有公司要求有硬件和软件相结合的公司要自己去面试,主考C,那不是掉的大了。

snow 是我C学完了,第一个小程序。虽不做C的开发,但还是怀恋下。(原程序出自国外。)

 

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>

#define POINT 200
#define DOWNSPEED 5
#define MOVESIZE 1
#define SHOW 1
#define HIDE 0

typedef struct
{
 int x;
 int y;
 int size;
}SNOW;

SNOW Snow[POINT];
int MaxX,MaxY;
int MaxSize=3;
int SnowColor=15;
int Delay=15000;
int CurSnow=0;
int *Map;

/********************************** Init_Graph *****************************/
void Init_Graph(){
 int gdriver=DETECT,gmode,errorcode;
 initgraph(&gdriver, &gmode, "G:\\turboc2\\");
 errorcode = graphresult();
 if (errorcode != grOk) /* an error occurred */
 {
  printf("Graphics error: %s\n", grapherrormsg(errorcode));
  printf("Press any key to halt:");
  getch();
  exit(1); /*terminate with an error code */
 }
 MaxX=getmaxx();
 MaxY=getmaxy();

 Map=(int *) malloc( sizeof(int)*MaxX );
}
/******************************** MakeSnow ********************************/
void MakeSnow()
{
 int i;
 if( CurSnow>=POINT ) return;
 for( i=0; Snow[i].size; i++ )
  ;
 CurSnow++;
 Snow[i].x=random(MaxX);
 Snow[i].y=random(DOWNSPEED);
 Snow[i].size=random(MaxSize)+1;
 
}
/******************************** Init_Data *******************************/
void Init_Data()
{
 int i;
 for( i=0; i< POINT; i++)
 {
  Snow[i].x=0;
  Snow[i].y=0;
  Snow[i].size=0;
 }
 for( i=0; i<MaxX; i++)
  Map[i]=MaxY;
}
/********************************* Init **********************************/
void Init()
{
 Init_Graph();
 Init_Data();
 randomize();
 MakeSnow();
}
/******************************** GetKey ********************************/
void GetKey(int *ah,int *al){
 union REGS r;
 r.h.ah=0;
 int86(0x16,&r,&r);
 /*return r.h.al;*/
 *ah=r.h.ah;
 *al=r.h.al;
}
/********************************* SetSnow ******************************/
void ShowSnow( int x, int y, int size, int flag )
{
 int color=0;

 if ( flag ) color=15;
 switch( size )
 {
 case 1:
  putpixel( x, y, color );
  break;
 case 2:
  setcolor( color );
  line( x-1, y-1, x+1, y+1 );
  line( x-1, y+1, x+1, y-1 );
  break;
 case 3:
  setcolor( color );
  line( x-1, y-1, x+1, y+1 );
  line( x-1, y+1, x+1, y-1 );
  /*
  line( x-2, y-2, x+2, y+2 );
  line( x-2, y+2, x+2, y-2 );*/

  line( x-2, y, x+2, y );
  line( x, y-2, x, y+2 );
  break;
 }

}
/********************************* Check ********************************/
void Move( int n, int tox, int toy )
{
 int x, y, size, i, j;
 float person;

 x=Snow[n].x;
 y=Snow[n].y;
 size=Snow[n].size;
 /* check end */
 j=y;
 if( x<tox )
 {
  person=(DOWNSPEED *1.0) / ( tox-x )*1.0;
  for( i=x; i<=tox; i++ )
  {
   if( j>=Map[i] )
   {
    tox=i-size;
    break;
   }
   j+=(int)( (i-x+1)*person );
  }
 }
 else if( x>tox )
 {
  person=(DOWNSPEED *1.0) / ( x-tox )*1.0;
  for( i=x; i>=tox; i-- )
  {
   if( j>=Map[i] )
   {
    tox=i+size;
    break;
   }
   j+=(int)( (x-i+1)*person );
  }
 }
 
 if( y+DOWNSPEED>=Map[tox] )
 {
  switch( size )
  {
  case 1:
   Map[x]--;
   break;
  case 2:
   Map[x]-=2;
   if( x>0 && Map[x-1]>Map[x] ) Map[x-1]=Map[x];
   if( x<MaxX-1 && Map[x+1]>Map[x] ) Map[x+1]=Map[x];
   break;
  case 3:
   Map[x]-=3;
   if( x>1 && Map[x-2]>Map[x] ) Map[x-1]=Map[x];
   if( x>0 && Map[x-1]>Map[x] ) Map[x-1]=Map[x];

   if( x<MaxX-2 && Map[x+2]>Map[x] ) Map[x+1]=Map[x];
   if( x<MaxX-1 && Map[x+1]>Map[x] ) Map[x+1]=Map[x];
   break;
  }
  CurSnow--;
  y=Map[x]+size;
  Snow[n].x=x;
  Snow[n].y=y;
  Snow[n].size=0;
 }
 else /* not end */
 {
  Snow[n].x=tox;
  Snow[n].y=toy;
 }
}
/******************************** snow **********************************/
void Begin()
{
 int ah,al;
 int i,dir,movesize;
 int x,y,size;
 int times;

 while ( !kbhit() )
 {
  times=5;
  while( times-- )
   delay( Delay );
  for( i=0; i<POINT; i++)
  {
   if ( !Snow[i].size ) continue;
   x=Snow[i].x;
   y=Snow[i].y;
   size=Snow[i].size;
   /* hide */
   ShowSnow( x, y, size, HIDE );
   /* down */
   y+= DOWNSPEED;
   /* move */
   movesize=random( MOVESIZE )+1;
   if ( random(2) )
    movesize=-movesize;
   x+=movesize;
   if( x>MaxX ) x=MaxX-1;
   if( x<0 ) x=0;
   /*Snow[i].x=x;
   Snow[i].y=y;
   Check( i );*/
   Move( i, x, y );
   /* show */
   ShowSnow( Snow[i].x, Snow[i].y, size, SHOW );     
  }
 
  MakeSnow();
 }
 GetKey( &ah, &al );
 GetKey( &ah, &al );
}

/********************************* main ***********************************/
void main()
{
 Init();
 Begin();
 closegraph();
 free( Map );
}

 

是不是思维固定了,还是变迂腐了,咋一看C时,有基础看的懂,仔细一品味没闻出个所以然出来。不知道大家有没有类似的感觉,有些东西时间丢长了,在回过头来怎么也想不懂当时写程式的逻辑了,那么觉得自己以前写的东西很幼稚,要么完全看不懂了。汗呀!。。。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics