C语言写俄罗斯方块源码——————【Badboy】
时间:2022-03-13 22:55
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
char x,y,ty,ty1,zhuan,line1=0,line2=0,ss[16],hol[10][29]={0},a[4][2]={0};
int score1=0,speed=1000;
void unit(char,char,char); void dl(int);
void block(char); void fuzi(char,char);
void up(void); void down(void);
void left(void); void right(void);
char fx(char,char,char); char fy(char,char,char);
char max1(char,char,char); char min1(char,char,char);
void score(void); void xiao(void);
void save(void);
main()
{
char cycy=0,i,j,b,ci;
initgraph(VGA,VGAHI,"c://tc//egavga.bgi");
getch();
setfillstyle(1,7);
setcolor(7);
bar(200,10,350,430);
setcolor(15);rectangle(198,8,352,432);
x=4;ty=0;zhuan=0;y=1;
do
{
fuzi(ty,zhuan);
block(14);
score();
for(ci=0;ci<10;ci++)
{
if(kbhit()!=0)
b=getch();
else
b=0;
if(b==13)
break;
switch(b)
{
case 72: up(); break;
case 75: left(); break;
case 77: right(); break;
case 80: down(); break;
}
dl(speed/2);
}
j=max1(a[3][1],a[1][1],a[2][1]);
if((j>=27)||(hol[a[0][0]][a[0][1]+1]+hol[a[1][0]][a[1][1]+1]+hol[a[2][0]][a[2][1]+1]+hol[a[3][0]][a[3][1]+1]
>=1))
{
hol[a[0][0]][a[0][1]]=1;hol[a[1][0]][a[1][1]]=1;
hol[a[2][0]][a[2][1]]=1;hol[a[3][0]][a[3][1]]=1;
y=2;
x=5;
do
{
randomize();
ty1=random(100);
ty1%=7;
if(ty1==0)
ty1=7;
}
while(ty==ty1);
ty=ty1;
j=0;
xiao();
}
else
{
block(0);
y++;
}
for(i=0;i<10;i++)
if(hol[i][0]==1)
{
cycy=1;
getch();
break;
}
if(cycy==1)
{
cycy=0;
break;
}
}
while(b!=13);
getch();
closegraph();
save();
getch();
}
void unit(char x,char y,char color)
{
setfillstyle(1,color);
setcolor(color);
bar(200+x*15,10+y*15,200+(x+1)*15-2,10+(y+1)*15-2);
}
void block(char color)
{
char i,t;
for(i=0;i<4;i++)
unit(a[i][0],a[i][1],color);
}
void dl(int a)
{
int r,n;
for(r=0;r<a;r++)
for(n=0;n<30000;n++)
{
n++;
n--;
}
}
char fx(char x0,char y0,char n)
{
char x1,x2,y1,y2;
x1=y0+x-y;
y1=x+y-x0;
if(n==0)
return(x0);
if(n==1)
return(x1);
if(n>=2)
{
x2=y1+x-y;
y2=x+y-x1;
if(n==2)
return(x2);
else
{
x1=y2+x-y;
y1=x+y-x2;
if(n==3)
return(x1);
}
}
}
char fy(char x0,char y0,char n)
{
char x1,x2,y1,y2;
x1=y0+x-y;
y1=x+y-x0;
if(n==0)
return(y0);
if(n==1)
return(y1);
if(n>=2)
{
x2=y1+x-y;
y2=x+y-x1;
if(n==2)
return(y2);
else
{
x1=y2+x-y;
y1=x+y-x2;
if(n==3)
return(y1);
}
}
}
void left()
{
char j;
fuzi(ty,zhuan);
block(0);
j=min1(a[3][0],a[1][0],a[2][0]);
if((j>=1)&&(hol[a[0][0]-1][a[0][1]]==0)&&(hol[a[1][0]-1][a[1][1]]==0)&&(hol[a[2][0]-1][a[2][1]]==0)&&(hol[a[3][0]-1]
[a[3][1]]==0))
{
j--;
x--;
}
fuzi(ty,zhuan);
block(14);
}
void right()
{
char j;
fuzi(ty,zhuan);
block(0);
j=max1(a[3][0],a[1][0],a[2][0]);
if((j<9)&&(hol[a[0][0]+1][a[0][1]]==0)&&(hol[a[1][0]+1][a[1][1]]==0)&&(hol[a[2][0]+1][a[2][1]]==0)&&(hol[a[3][0]+1]
[a[3][1]]==0))
{
j++;
x++;
}
fuzi(ty,zhuan);
block(14);
}
void fuzi(char n,char zhuan)
{
a[0][0]=x;
a[0][1]=y;
n=n%7;
if(n==0)
n=7;
zhuan=zhuan%4;
switch (n)
{
case 1: a[1][0]=fx(x-1,y,zhuan);a[1][1]=fy(x-1,y,zhuan);
a[2][0]=fx(x,y+1,zhuan);a[2][1]=fy(x,y+1,zhuan);
a[3][0]=fx(x,y+2,zhuan);a[3][1]=fy(x,y+2,zhuan);
break;
case 2: a[1][0]=fx(x-1,y,zhuan);a[1][1]=fy(x-1,y,zhuan);
a[2][0]=fx(x,y-1,zhuan);a[2][1]=fy(x,y-1,zhuan);
a[3][0]=fx(x,y-2,zhuan);a[3][1]=fy(x,y-2,zhuan);
break;
case 3: a[1][0]=fx(x-1,y-1,zhuan);a[1][1]=fy(x-1,y-1,zhuan);
a[2][0]=fx(x,y+1,zhuan);a[2][1]=fy(x,y+1,zhuan);
a[3][0]=fx(x-1,y,zhuan);a[3][1]=fy(x-1,y,zhuan);
break;
case 4: a[1][0]=fx(x-1,y,zhuan);a[1][1]=fy(x-1,y,zhuan);
a[2][0]=fx(x-1,y+1,zhuan);a[2][1]=fy(x-1,y+1,zhuan);
a[3][0]=fx(x,y-1,zhuan);a[3][1]=fy(x,y-1,zhuan);
break;
case 5: a[1][0]=fx(x-1,y,zhuan);a[1][1]=fy(x-1,y,zhuan);
a[2][0]=fx(x,y-1,zhuan);a[2][1]=fy(x,y-1,zhuan);
a[3][0]=fx(x,y+1,zhuan);a[3][1]=fy(x,y+1,zhuan);
break;
case 6: a[1][0]=fx(x,y+1,zhuan);a[1][1]=fy(x,y+1,zhuan);
a[2][0]=fx(x,y-1,zhuan);a[2][1]=fy(x,y-1,zhuan);
a[3][0]=fx(x,y-2,zhuan);a[3][1]=fy(x,y-2,zhuan);
break;
case 7: a[1][0]=fx(x-1,y,zhuan);a[1][1]=fy(x-1,y,zhuan);
a[2][0]=fx(x,y-1,zhuan);a[2][1]=fy(x,y-1,zhuan);
a[3][0]=fx(x-1,y-1,zhuan);a[3][1]=fy(x-1,y-1,zhuan);
break;
}
}
void up()
{
char j,k;
fuzi(ty,zhuan);
block(0);
zhuan++;
zhuan=zhuan%4;
fuzi(ty,zhuan);
j=min1(a[3][0],a[1][0],a[2][0]);
k=max1(a[3][0],a[1][0],a[2][0]);
if((k<=9)&&(j>=0))
{
if((hol[a[0][0]][a[0][1]]==0)&&(hol[a[1][0]][a[1][1]]==0)&&(hol[a[2][0]][a[2][1]]==0)&&(hol[a[3][0]][a[3]
[1]]==0))
block(14);
else
{
zhuan+=3;
zhuan%=4;
fuzi(ty,zhuan);
block(14);
}
}
else
{
zhuan+=3;
zhuan%=4;
fuzi(ty,zhuan);
block(14);
}
}
void down(void)
{
char j,cy=0;
fuzi(ty,zhuan);
block(0);
j=max1(a[3][1],a[2][1],a[1][1]);
while(j<28)
{
j=max1(a[3][1],a[1][1],a[2][1]);
if((j>=27)||(hol[a[0][0]][a[0][1]+1]==1)||(hol[a[1][0]][a[1][1]+1]==1)||(hol[a[2][0]][a[2][1]+1]==1)||(hol[a
[3][0]][a[3][1]+1]==1))
{
hol[a[0][0]][a[0][1]]=1;
hol[a[1][0]][a[1][1]]=1;
hol[a[2][0]][a[2][1]]=1;
hol[a[3][0]][a[3][1]]=1;
block(14);
xiao();
y=2;
x=5;
do
{
randomize();
ty1=random(100);
ty1%=7;
if(ty1==0)
ty1=7;
}
while(ty1==ty);
ty=ty1;
j=0;
fuzi(ty,zhuan);
block(14);
cy=1;
}
else
{
dl(speed/4);
fuzi(ty,zhuan);
block(0);
y++;
}
if(cy==1)
{
cy=0;
break;
}
fuzi(ty,zhuan);
block(14);
}
}
void xiao(void)
{
char jj,y1,r,x1,y2;
for(jj=0;jj<4;jj++)
{
for(y1=27;y1>0;y1--)
{
r=0;
for(x1=0;x1<10;x1++)
{
if(hol[x1][y1]==1)
r++;
}
if(r==10)
{
line1++;
for(x1=0;x1<10;x1++)
{
unit(x1,y1,0);
hol[x1][y1]=0;
}
for(y2=y1-1;y2>0;y2--)
{
for(x1=0;x1<10;x1++)
{
if(hol[x1][y2]==1)
{
hol[x1][y2]=0;
unit(x1,y2,0);
unit(x1,y2+1,14);
hol[x1][y2+1]=1;
}
}
}
}
}
}
}
char max1(char a,char b,char c)
{
char max3;
if(a>=b)
max3=a;
else
max3=b;
if(max3<c)
max3=c;
return(max3);
}
char min1(char a,char b,char c)
{
char min;
if(a<=b)
min=a;
else
min=b;
if(min>c)
min=c;
return(min);
}
void score(void)
{
char a,b=1;
setcolor(0);
outtextxy(20,20,ss);
setcolor(14);
for(a=0;a<line1-line2;a++)
b*=2;
score1+=b-1;
if(b>1)
speed-=2;
line2=line1;
sprintf(ss,"Your score:%d.",score1);
outtextxy(20,20,ss);
}
void save(void)
{
FILE * fp;
int i;
i=0;
fp=fopen("bloscore.txt","rb+");
i=(int)(fgetc(fp))-48;
if(fp=NULL)
i=0;
fclose(fp);
if(i<score1)
{
fp=fopen("bloscore.txt","wb+");
fprintf(fp,"%d",score1);
fclose(fp);
printf("Your score: %d are the highest!",score1);
}
getch();
}
C语言写俄罗斯方块源码——————【Badboy】,布布扣,bubuko.com