qq:674940174
算了我把原代码贴上来得了.你那去编译下,用TC.
如果有人转贴请写上作者和出处.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
#include<graphics.h>
#define INDEX 10
#define COLOR 2
unsigned int i,j;
unsigned int b[4][4];
unsigned int xiao[4];
unsigned int i,j,m;
unsigned int q,c[8];
struct data{ int a[4][4];}k[5]={{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}
};
void welcome()
{ unsigned int i,j;
clrscr();
textmode(C40);
for(i=0;i<40;i++)
printf("*");
for(j=1;j<24;j++)
{ printf("*");
gotoxy(40,j);
printf("*");
}
for(i=0;i<40;i++)
printf("*");
gotoxy(10,8);
textcolor(RED);
cprintf("WELCOME !");
gotoxy(15,15);
textcolor(YELLOW);
cprintf("made by HJJ ");
gotoxy(15,16);
cprintf("2006-11-11");
gotoxy(5,20);
textcolor(GREEN);
cprintf("press any key to continue !");
getch();
}
void mydelay(int clicks)
{ unsigned int now=*(unsigned int far *)0x0000046c;
while(abs(*(unsigned int far *)0x0000046c-now)<clicks)
{ }
}
void minidraw(int x,int y,int index,int color)
{ setcolor(color);
setlinestyle(SOLID_LINE,0,1);
line(x,y,x+index,y);
line(x+index,y,x+index,y+index);
line(x+index,y+index,x,y+index);
line(x,y+index,x,y);
}
void draw(int x,int y,int index,int color)
{ minidraw(x,y,index,color);
minidraw(x+1,y+1,index-2,color);
minidraw(x+2,y+2,index-4,color);
}
void drawall(int x,int y,int index,int color)
{ unsigned int r,s;
for(r=0;r<4;r++)
for(s=0;s<4;s++)
if(b[r][s]==1) draw(x+s*INDEX,y+r*INDEX,index,color);
}
int mygetimage(int x,int y,int index,char *filename)
{ unsigned long size;
char *buffer;
FILE *fp;
size=(unsigned long)imagesize(0,0,4*index,4*index);
fp=fopen(filename,"wb") ;
if(fp==NULL) { fclose(fp);
printf("file open failled !");
exit(0);
}
buffer=(char *)malloc(size);
if(buffer==NULL) return 0;
getimage(x,y,x+4*index,y+4*index,buffer);
fwrite(buffer,size,1,fp);
free(buffer);
fclose(fp);
return(1);
}
void myputimage(int x,int y,char *filename)
{ FILE *fp;
char *buffer;
unsigned long size=imagesize(0,0,4*INDEX,4*INDEX);
fp=fopen(filename,"rb");
if(fp==NULL) { fclose(fp);
printf("open file failled !");
exit(0);
}
buffer=(char *)malloc(size) ;
fread(buffer,size,1,fp);
putimage(x,y,buffer,XOR_PUT);
free(buffer);
fclose(fp);
}
void converting()
{ unsigned int r,s;
for(r=0;r<4;r++)
for(s=0;s<4;s++)
b[r][s]=k[m].a[r][s];
}
void initial()
{ unsigned int gdriver=ATT400,gmode=ATT400C3;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
cleardevice();
line(100,10,250,10);
line(250,10,250,170);
line(250,170,100,170);
line(100,170,100,10);
line(200,10,200,170);
i=130;
j=10;
m=randmake();
converting();
drawall(205,15,INDEX-1,COLOR);
mygetimage(205,15,INDEX,"picture.dat");
}
int getkey()
{ union REGS rg;
rg.h.ah=0;
int86(0x16,&rg,&rg);
return rg.x.ax;
}
int checktop()
{ if(checktopmini()<=10+ymini()*INDEX) return 0;
return 1;
}
int checktopmini()
{ int r,s,mini=160,flagx=0;
for(s=170;s>0;s-=INDEX)
{ for(r=100;r<201;r+=INDEX)
if(getpixel(r,s-1)==COLOR)
{ if(s<=mini) mini=s;
r=201;
flagx=1;
}
if(flagx==0) s=10;
}
return mini;
}
int checkdown(unsigned int y1)
{ unsigned int r,s,x1;
x1=i;
for(r=0;r<4;r++)
for(s=0;s<4;s++)
if(b[r][s]==1&&(b[r+1][s]==0||r==3))
if(getpixel(x1+s*INDEX+1,y1+(r+1)*INDEX+2)==COLOR)
return 0;
return 1;
}
int checknow()
{ unsigned int r,s;
for(r=0;r<4;r++)
for(s=0;s<4;s++)
if(b[r][s]==1&&(b[r+1][s]==0||r==3))
if(getpixel(i+s*INDEX+1,j+r*INDEX+2)==COLOR)
return 0;
return 1;
}
int checkfull()
{ int r,s,flag;
int o=0;
for(s=10;s<170;s+=INDEX)
{ flag=1;
for(r=100;r<200;r+=INDEX)
if(getpixel(r+1,s+2)!=COLOR)
{ flag=0;
r=190;
}
if(flag==1) xiao[o++]=s;
}
return o;
}
void inexistence()
{ int p,t;
char *buffer;
unsigned int size=imagesize(101,10,199,19);
buffer=(char *)malloc(size);
if(buffer==NULL) { free(buffer);
printf("malloc failed !");
exit(0);
}
for(p=0;p<4;p++)
{ if(xiao[p]>0)
{ getimage(101,xiao[p],199,xiao[p]+9,buffer);
putimage(101,xiao[p],buffer,XOR_PUT);
for(t=xiao[p]-10;t>10;t-=INDEX)
{ getimage(101,t,199,t+INDEX-1,buffer);
putimage(101,t,buffer,XOR_PUT);
putimage(101,t+INDEX,buffer,COPY_PUT);
}
xiao[p]=0;
}
}
free(buffer);
}
void rightmove()
{ int r,s,xm;
int flag=1;
xm=xmini();
for(r=0;r<4;r++)
for(s=0;s<4;s++)
if(b[r][s]==1&&(s==3||b[r][s+1]==0))
if(getpixel(i+(s+1)*INDEX+2,j+r*INDEX+2)==COLOR) flag=0;
if(flag==1&&i<200-xm*INDEX)
{ myputimage(i,j,"picture.dat");
i+=INDEX;
myputimage(i,j,"picture.dat");
}
}
void leftmove()
{ int r,s;
int flag=1;
for(r=0;r<4;r++)
for(s=0;s<4;s++)
if(b[r][s]==1&&(s==0||b[r][s-1]==0))
if(getpixel(i+s*INDEX-2,j+r*INDEX+2)==COLOR) flag=0;
if(flag==1&&i>100)
{ myputimage(i,j,"picture.dat");
i-=INDEX;
myputimage(i,j,"picture.dat");
}
}
int fastmove()
{
unsigned int x,y;
x=i;
y=j;
while(checkdown(y)&&y<160-ymini()*INDEX)
y+=INDEX;
myputimage(i,j,"picture.dat");
myputimage(x,y,"picture.dat");
j=y;
}
int ymini()
{ int r,s,mini=0;
for(r=0;r<4;r++)
{ for(s=0;s<4;s++)
if(b[r][s]==1)
{ mini+=1;
s=4;
}
}
return mini;
}
int xmini()
{ int r,s,mini=0;
for(r=0;r<4;r++)
{ for(s=0;s<4;s++)
if(b[s][r]==1)
{ mini+=1;
break;
}
}
return mini;
}
int randmake()
{ unsigned int m;
srand((unsigned)time(NULL));
m=rand()%5;
return m;
}
void endmanage()
{ myputimage(205,15,"picture.dat");
j=10;
i=130;
m=randmake();
converting();
drawall(205,15,INDEX-1,COLOR);
mygetimage(205,15,INDEX,"picture.dat");
mygetimage(205,15,INDEX,"picture1.dat");
if(checkfull()) inexistence();
}
void fall()
{ unsigned int clock=4;
unsigned char *adress;
myputimage(i,j,"picture.dat");
myputimage(i,j+INDEX,"picture.dat");
j+=INDEX;
while(clock--)
{ if(bioskey(1)==0) mydelay(2);
else return;
}
}
unsigned int checkmove()
{ if(checknow())
{ if(j<checktopmini()-xmini()*INDEX&&i<200-ymini()*INDEX&&i>99)
return 0;
}
return 1;
}
void move()
{ unsigned int r,s;
unsigned int h=0;
float co,si;
for(r=0;r<4;r++)
for(s=0;s<4;s++)
if(b[r][s]==1)
{
c[h++]=s;
c[h++]=r;
b[r][s]=0;
}
b[c[0]][c[1]]=1;
b[c[2]][c[3]]=1;
b[c[4]][c[5]]=1;
b[c[6]][c[7]]=1;
myputimage(i,j,"picture.dat");
myputimage(205,15,"picture.dat");
if(checkmove()) converting();
drawall(205,15,INDEX-1,COLOR);
mygetimage(205,15,INDEX,"picture.dat");
myputimage(i,j,"picture.dat");
}
void main()
{ unsigned char p;
welcome();
loop:initial();
do
{ if(checknow(j)>0) myputimage(i,j,"picture.dat");
while(checkdown(j)>0&&j<161-ymini()*INDEX)
{ if(bioskey(1)==0) fall();
else { q=getkey();
switch(q)
{ case 0x4b00: leftmove();break;
case 0x4800: move();break;
case 0x4d00: rightmove();break;
case 0x5000: fastmove(i,j);
}
}
}
endmanage();
}while(checktop());
getch();
cleardevice();
printf("if you want to paly the game another time,input 'y'");
p=bioskey(0);
if(p=='y') goto loop;
getch();
closegraph();
}
[此贴子已经被作者于2006-11-14 19:51:18编辑过]