主题:一款俄罗斯方块游戏的源代码,(用Tc2.0编写)
速度很快
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<malloc.h>
#include<math.h>
/**********/
#define PATH "D:\\学习\\turboc2" /*改为你的TC2.0的目录*/
#define BK 7
#define DC 8
#define LC 15
#define P1 1
#define P2 4
#define X 121
#define Y 41
#define M 8
/************/
int newmap[26][14] ={{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0, 1,1},
{1,1, 0,0,0,0,0,0,0,0,0,0 , 1,1},
{1,1, 1,1,1,1,1,1,1,1,1,1, 1,1},
{1,1, 1,1,1,1,1,1,1,1,1,1, 1,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,0,0}
},
/***********/
{
{0,0,2,0},
{0,0,2,0},
{0,2,2,0},
{0,0,0,0}
},
/***********/
{
{0,0,0,0},
{0,2,0,0},
{0,2,2,0},
{0,0,2,0}
},
/***********/
{
{0,0,0,0},
{0,2,2,0},
{0,2,2,0},
{0,0,0,0}
},
/***********/
{
{0,0,0,0},
{0,2,2,0},
{0,0,2,0},
{0,2,2,0}
},
{
{0,0,0,0},
{0,0,2,0},
{0,2,2,0},
{0,2,0,0}
}
};
char far * bit[3],*sim;
int score,level;
/************/
int getkey();
void edge(int l,int t,int r,int b,int c1,int c2);
void init();
void new();
void newk();
void draw(int x,int y,int c);
void show();
void close();
void down();
int candown();
void tobotton();
int canturn();
void turn();
int canleft();
void left();
int canright();
void right();
int isok(int x);
void ok(int x);
void showscore();
void drawnext();
int isover();
void over();
/**************/
int getkey()
{
char ch=0;
if(kbhit())
ch=getch();
return(ch);
}
/************/
void init()
{
int gdriver=DETECT,gmode;
int size,i,j;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,PATH);
setbkcolor(BK);
setfillstyle(11,8);
edge(X-9,Y-9,X+408,Y+408,DC,DC);
floodfill(1,1,DC);
edge(X-9,Y-9,X+408,Y+408,DC,LC);
edge(X-7,Y-7,X+406,Y+406,DC,LC);
edge(X-1,Y-1,X+201,Y+401,DC,LC);
edge(X-3,Y-3,X+203,Y+403,LC,DC);
edge(X+260,Y-1,X+341,Y+81,DC,LC);
edge(X+258,Y-3,X+343,Y+83,LC,DC);
size=imagesize(X+1,Y+1,X+20,Y+20);
bit[0]=(char far*)malloc(size);
if(bit[0]==NULL){sound(2000);getch();nosound();exit(1);}
getimage(X+1,Y+1,X+20,Y+20,bit[0]);
for(i=0;i<20;i++)
for(j=0;j<20;j++)
if(bmp[i][j]==1)
putpixel(X+1+j,Y+1+i,P1);
bit[1]=(char far*)malloc(size);
if(bit[1]==0){sound(2000);getch();nosound();exit(1);}
getimage(X+1,Y+1,X+20,Y+20,bit[1]);
for(i=0;i<20;i++)
for(j=0;j<20;j++)
if(bmp[i][j]==1)
putpixel(X+1+j,Y+1+i,P2);
bit[2]=(char far*)malloc(size);
getimage(X+1,Y+1,X+20,Y+20,bit[2]);
setcolor(8);
outtextxy(X+245,Y+95,"score");
outtextxy(X+320,Y+95,"level");
edge(X+240,Y+107,X+290,Y+122,LC,DC);
edge(X+242,Y+109,X+288,Y+120,DC,LC);
edge(X+315,Y+107,X+365,Y+122,LC,DC);
edge(X+317,Y+109,X+363,Y+120,DC,LC);
size=imagesize(X+240,Y+107,X+290,Y+122);
sim=(char far*)malloc(size);
getimage(X+240,Y+107,X+290,Y+122,sim);
edge(X+207,Y+133,X+400,Y+401,LC,DC);
edge(X+210,Y+135,X+397,Y+398,LC,DC);
outtextxy(X+213,Y+140,"Control Option:");
outtextxy(X+213,Y+150,"-----------------");
outtextxy(X+213,Y+160," [P]--Start/Pause");
outtextxy(X+213,Y+177," [Esc]--Quit the game");
outtextxy(X+213,Y+194," [W]--Turn");
outtextxy(X+213,Y+211," [A]--Left");
outtextxy(X+213,Y+228," [S]--Down fast");
outtextxy(X+213,Y+245," [D]--Right");
outtextxy(X+213,Y+262,"[Space]--Down to botton");
for(i=0;i<4;i++)
edge(X+213+i*3,Y+278+i*4,X+394-i*3,Y+391-i*4,DC,LC);
outtextxy(X+224,Y+300," This game was");
outtextxy(X+224,Y+312,"programed by jackin.");
outtextxy(X+224,Y+324," If you discover" );
outtextxy(X+224,Y+336,"errors in it ,please");
outtextxy(X+224,Y+348," email to:");
setcolor(4);
outtextxy(X+224,Y+360," jackin0627@163.com");
if(bit[0]==0||bit[1]==0||bit[2]==0||sim==0)
{
close();
exit(1);
}
putimage(X+1,Y+1,bit[0],0);
}
/*************/
void edge(int l,int t,int r,int b,int c1,int c2)
{
int cl=getcolor();
setcolor(c1);
line(l,t,r-1,t);
line(l,t,l,b-1);
setcolor(c2);
line(r,t,r,b);
line(l,b,r,b);
setcolor(cl);
}
/*************/
void draw(int x,int y,int c)
{
putimage(X+20*y,Y+x*20,bit[c],0);
}
/************/
void show()
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<10;j++)
if(map[1][i+4][j+2]!=map[0][i+4][j+2])
{
map[0][i+4][j+2]=map[1][i+4][j+2];
draw(i,j,map[1][i+4][j+2]);
}
}
/******************/
void close()
{
int i;
for(i=0;i<3;i++)
free(bit[i]);
free(sim);
closegraph();
}
/**************/
void new()
{
int i,j,t=-1;
score=0;
level=1;
showscore();
for(i=0;i<26;i++)
for(j=0;j<14;j++)
{
map[1][i][j]=newmap[i][j];
map[0][i][j]=0;
}
for(;t<0;)
t=rand();
next=t%M;
}
/**********/
void newk()
{
int i,j,t=-1;
xx=1;
yy=5;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
map[1][xx+i][j+yy]=kk[next][i][j];
for(;t<0;)
t=rand();
next=t%M;
show();
}
/*************/
int candown()
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(map[1][xx+i][yy+j]==2&&map[1][xx+i+1][yy+j]==1)
return(0);
}
return(1);
}
/************/
void down()
{
int i,j;
xx++;
for(i=3;i>=0;i--)
for(j=3;j>=0;j--)
{
if(map[1][xx+i-1][yy+j]!=1)
{
if(map[1][xx+i][yy+j]!=1)
map[1][xx+i][yy+j]=map[1][xx+i-1][yy+j];
}
else
{
if(map[1][xx+i][yy+j]!=1)
map[1][xx+i][yy+j]=0;
}
}
for(j=0;j<4;j++)
if(map[1][xx-1][yy+j]==2)
map[1][xx-1][yy+j]=0;
}
/*******************/
void tobotton()
{
int i,j;
for(i=3;i>=0;i--)
for(j=3;j>=0;j--)
if(map[1][xx+i][yy+j]==2)
map[1][xx+i][yy+j]=1;
}
/***************/
int canturn()
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(map[1][xx+3-j][i+yy]==1&&map[1][xx+i][j+yy]==2)
return(0);
return(1);
}
/****************/
void turn()
{
int i,j,t[4][4];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(map[1][xx+i][j+yy]==1)
t[i][j]=0;
else
t[i][j]=map[1][xx+i][j+yy];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(map[1][xx+i][j+yy]!=1)
map[1][xx+i][j+yy]=t[j][3-i];
}
/*************/
int canleft()
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(map[1][xx+i][yy+j]==2&&map[1][xx+i][yy+j-1]==1)
return(0);
}
return(1);
}
/************/
void left()
{
int i,j;
yy--;
for(i=3;i>=0;i--)
for(j=0;j<4;j++)
{
if(map[1][xx+i][yy+j+1]!=1)
{
if(map[1][xx+i][yy+j]!=1)
map[1][xx+i][yy+j]=map[1][xx+i][yy+j+1];
}
else
{
if(map[1][xx+i][yy+j]!=1)
map[1][xx+i][yy+j]=0;
}
}
for(j=0;j<4;j++)
if(map[1][xx+j][yy+4]==2)
map[1][xx+j][yy+4]=0;
}
/****************/
int canright()
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(map[1][xx+i][yy+j]==2&&map[1][xx+i][yy+j+1]==1)
return(0);
}
return(1);
}
/************/
void right()
{
int i,j;
yy++;
for(i=3;i>=0;i--)
for(j=3;j>=0;j--)
{
if(map[1][xx+i][yy+j-1]!=1)
{
if(map[1][xx+i][yy+j]!=1)
map[1][xx+i][yy+j]=map[1][xx+i][yy+j-1];
}
else
{
if(map[1][xx+i][yy+j]!=1)
map[1][xx+i][yy+j]=0;
}
}
for(j=0;j<4;j++)
if(map[1][xx+j][yy-1]==2)
map[1][xx+j][yy-1]=0;
}
/*******************/
int isok(int x)
{
int i;
if(x>23)return(0);
for(i=2;i<12;i++)
if(map[1][x][i]==0||map[1][x][i]==2)
return(0);
return(1);
}
/******************/
void ok(int x)
{
int i;
for(;x>0;x--)
for(i=2;i<12;i++)
map[1][x][i]=map[1][x-1][i];
score++;
level=score/10+1;
}
/*****************/
void showscore()
{
char s[20],l[20];
itoa(score,s,10);
itoa(level,l,10);
putimage(X+240,Y+107,sim,0);
putimage(X+315,Y+107,sim,0);
outtextxy(X+244,Y+111,s);
outtextxy(X+325,Y+111,l);
}
/******************/
void drawnext()
{
int x,y;
for(x=0;x<4;x++)
for(y=0;y<4;y++)
{
putimage(X+261+20*y,Y+x*20,bit[kk[next][x][y]],0);
}
}
/*********************/
int isover()
{
int i;
for(i=2;i<12;i++)
if(map[1][3][i]==1)return(1);
return(0);
}
/****************/
void over()
{
int i,j;
for(i=23;i>3;i--)
{
for(j=2;j<12;j++)
map[1][i][j]=1;
show();
delay(1000);
}
for(i=4;i<24;i++)
{
for(j=2;j<12;j++)
map[1][i][j]=0;
show();
delay(1000);
}
}
/*******************/
main()
{
int i,j;
char cc;
init();
start:
cc=getkey();
for(;cc!='p'&&cc!='P';)
{
rand();
if(cc==27)goto end;
cc=getkey();
}
new();
newk();
for(;;)
{
for(i=0;i<20000-level*1000;i++)
{
switch(getkey())
{
case 27:
over();
goto end;
case 'w':
case 'W':
if(canturn())
turn();
break;
case 'a':
case 'A':
if(canleft())
left();
break;
case 's':
case 'S':
if(candown())
{
down();
}
else
{
}
break;
case 'd':
case 'D':
if(canright())
right();
break;
case ' ':
for(;candown();)
down();
break;
case 'p':
case 'P':
cc=getkey();
for(;cc!='p'&&cc!='P';)
{
if(cc==27)goto end;
cc=getkey();
}
break;
default:break;
}
show();
}
drawnext();
if(candown())
{
down();
show();
}
else
{
tobotton();
for(j=1;j<24;j++)
if(isok(j))
{
ok(j);
score++;
showscore();
}
if(isover())
{
over();
goto start;
}
newk();
show();
}
}
end:
close();
}