www.pudn.com > c.rar > ROAM.C, change:2005-11-11,size:1597b


#include<graphics.h> 
#include<stdlib.h> 
#include<time.h> 
#include<malloc.h> 
main() 
{ 
  int gdrive,gmode; 
 
  gdrive=DETECT; 
  initgraph(&gdrive,&gmode,"D:\\tc20\\BGI"); 
 
  setbkcolor(15);/*Set white backgroud color.*/ 
  roam(); 
 
  closegraph(); 
} 
int roam() 
{ 
  int maxclr,x0,y0,x1,y1,x2,y2,xl,yl; 
  int clr,*buff; 
  unsigned int  sz; 
  char ch; 
  time_t st,et,tick; 
  srand(time(NULL)); 
 
  /*Generate staff.*/ 
  maxclr=getmaxcolor()+1; 
  x1=10; 
  y1=10; 
  x2=170; 
  y2=170; 
 
  while(x1<x2) 
  { 
    clr=rand(); 
    if(clr<1) clr=1; 
    clr=clr%(maxclr+1); 
    setcolor(clr); 
    rectangle(x1,y1,x2,y2); 
    x1++; 
    y1++; 
    x2--; 
    y2--; 
  } 
 
  /*Get bitmap and save it.*/ 
  buff=NULL; 
  sz=51200; 
  buff=malloc(sz); 
  if(buff==NULL) 
  { 
    printf("Cann't allocate enough memory.\n"); 
    return -1; 
  } 
 
  sz=0; 
  for(y1=10;y1<170;y1++) 
  { 
    for(x1=10;x1<170;x1++) 
    { 
      clr=getpixel(x1,y1); 
      buff[sz]=clr; 
 
      sz=sz+1; 
      putpixel(x1+250,y1,clr); 
    } 
  } 
 
 
 
  cleardevice(); 
 
  ch='s'; 
  x0=0; 
  y0=0; 
 
  do 
  { 
    if(ch=='d'&&x0<59) 
    { 
      x0++; 
    } 
    if(ch=='a'&&x0>10) 
    { 
      x0--; 
    } 
 
    if(ch=='w'&&y0>10) 
    { 
      y0--; 
    } 
 
    if(ch=='x'&&y0<59) 
    { 
      y0++; 
    } 
 
    for(y1=0;y1<100;y1++) 
    { 
      for(x1=0;x1<100;x1++) 
      { 
	sz=(y0+y1)*160+x0+x1; 
	clr=buff[sz]; 
	putpixel(x1+10,y1+10,clr); 
      } 
    } 
    ch=getch(); 
    if(ch>64&&ch<91) 
    { 
      ch=ch+32; 
    } 
 
  }while(ch!='e'); 
 
  free(buff); 
}