TA的每日心情 | 难过 2017-6-29 01:10 |
---|
签到天数: 376 天 [LV.9]以坛为家II
管理员
- 积分
- 13561
|
c实现俄罗斯方块代码(500多行)
//源码和程序下载地址在页面底部
//这几天写的c语言程序,由于使用了easyX里的图形函数,
//而easyx必须要求文件名为cpp.里面的内容为纯c
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<tchar.h>
#define S1 60 //初始位置(S1,S1)
#define S2 20 //单位大小
const int M=18; //宽度多少单位
const int N=23; //高度多少单位
const int Width=700;
const int Height=600;
IMAGE img,img1,img2,img3,img4,img5,img6,img7,img8,img9,img10,img11;
int b,f,p,q,number,number2,number3,count1,count2,c[5],d[5],cc[5],dd[5],ccc[4],ddd[4];
char e;
char string[50],string2[50]; //输出结果
struct Barrier
{
int x;
int y;
IMAGE z;
}barrier[M+1][N+1],barrier2[M+1][N+1],coor1;
typedef struct Barrier coor;
//顺时针旋转90度函数
coor rotate(int x,int y,int x0,int y0)
{
int x1,y1;
x1=y0-y+x0;
y1=x-x0+y0;
coor1.x=x1;
coor1.y=y1;
return coor1;
}
void rotate2()
{
for (b=0;b<4;b++)
{
c[b]=c[b]+S2*1/2;d[b]=d[b]+S2*1/2;
rotate(c[b],d[b],c[4],d[4]);
c[b]=coor1.x-S2*1/2;
d[b]=coor1.y-S2*1/2;
}
}
//左移右移函数
void Move(int i)
{
for (b=0;b<5;b++)
c[b]=c[b]+S2*i;
}
//恢复变换前坐标的函数
void Restore()
{
for (b=0;b<5;b++)
{
c[b]=cc[b];
d[b]=dd[b];
}
}
//判断旋转或左移或右移后是否重叠
int room()
{
count2=0;
for (b=0;b<4;b++)
for (f=0;f<2;f++)
{
p=c[b];q=d[b]-(d[b]-S1)%S2+S2*f;
if (barrier[(p-S1)/S2][(q-S1)/S2].x==p && barrier[(p-S1)/S2][(q-S1)/S2].y==q)
count2++;
}
if (count2!=0)
{
if (e==72)
count1--;
return 1;
}
else
return 0;
}
//向下移动i个S2,把位置用barrier2数组接收
void functon1(int i)
{
if (barrier[p][q].x==S1+p*S2 && barrier[p][q].y==S1+q*S2)
{
barrier2[p][q+i].x=S1+p*S2;
barrier2[p][q+i].y=S1+(q+i)*S2;
barrier2[p][q+i].z=barrier[p][q].z;
barrier[p][q].x=0;
barrier[p][q].y=0;
}
}
//擦除填满的一行
void function2(int i)
{
barrier[p][i].x=0;
barrier[p][i].y=0;
}
//回复给barrier数组
void function3()
{
if (barrier2[p][q].x==S1+p*S2 && barrier2[p][q].y==S1+q*S2)
{
barrier[p][q].x=barrier2[p][q].x;
barrier[p][q].y=barrier2[p][q].y;
barrier[p][q].z=barrier2[p][q].z;
//barrier2数组数据清零
barrier2[p][q].x=0; // 注意注意 :这两步不能少
barrier2[p][q].y=0; // 注意注意 :这两步不能少
}
}
//初始化图形函数
void Ding()
{
c[0]=S1+9*S2; d[0]=S1+S2;
c[1]=c[0]; d[1]=d[0]+S2;
c[2]=c[1]; d[2]=d[1]+S2;
c[3]=c[1]+S2; d[3]=d[1];
c[4]=c[1]+(1.0/2)*S2;d[4]=d[1]+(1.0/2)*S2;
}
void RightL()
{
c[0]=S1+9*S2; d[0]=S1+S2;
c[1]=c[0]+S2; d[1]=d[0];
c[2]=c[1]; d[2]=d[1]+S2;
c[3]=c[1]; d[3]=d[2]+S2;
c[4]=c[0]+S2*1.0/2;d[4]=d[0]+(3.0/2)*S2;
}
void LeftL()
{
c[0]=S1+8*S2; d[0]=S1+S2;
c[1]=c[0]; d[1]=d[0]+S2;
c[2]=c[0]; d[2]=d[1]+S2;
c[3]=c[0]+S2; d[3]=d[0];
c[4]=c[0]+(3.0/2)*S2;d[4]=d[0]+(3.0/2)*S2;
}
void LeftZ()
{
c[0]=S1+10*S2; d[0]=S1+S2;
c[1]=c[0]; d[1]=d[0]+S2;
c[2]=c[0]-S2; d[2]=d[1];
c[3]=c[2]; d[3]=d[2]+S2;
c[4]=c[2]+(1.0/2)*S2;d[4]=d[2]+(1.0/2)*S2;
}
void RightZ()
{
c[0]=S1+8*S2; d[0]=S1+S2;
c[1]=c[0]; d[1]=d[0]+S2;
c[2]=c[1]+S2; d[2]=d[1];
c[3]=c[2]; d[3]=d[2]+S2;
c[4]=c[2]+(1.0/2)*S2;d[4]=d[2]+(1.0/2)*S2;
}
void Square()
{
c[0]=S1+8*S2; d[0]=S1+S2;
c[1]=c[0]+S2; d[1]=d[0];
c[2]=c[0]; d[2]=d[0]+S2;
c[3]=c[1]; d[3]=d[2];
}
void Yi()
{
c[0]=S1+7*S2;d[0]=S1+S2;
c[1]=c[0]+S2;d[1]=d[0];
c[2]=c[1]+S2;d[2]=d[1];
c[3]=c[2]+S2;d[3]=d[2];
}
//画边框
void Region()
{
IMAGE img1;
IMAGE img2;
initgraph(Width,Height);
setbkcolor(GREEN);
setbkmode(TRANSPARENT);
cleardevice();
loadimage(&img3,"D:\\C共享\\俄罗斯方块\\images\\GameRegionRim2.jpg");
putimage(S1,S1,&img3);
getimage(&img3,S1,S1,S2,S2);
loadimage(&img4,"D:\\C共享\\俄罗斯方块\\images\\square_blue.jpg");
putimage(S1,S1,&img4);
getimage(&img4,S1,S1,S2,S2);
loadimage(&img5,"D:\\C共享\\俄罗斯方块\\images\\square_darkBlue.jpg");
putimage(S1,S1,&img5);
getimage(&img5,S1,S1,S2,S2);
loadimage(&img6,"D:\\C共享\\俄罗斯方块\\images\\square_green.jpg");
putimage(S1,S1,&img6);
getimage(&img6,S1,S1,S2,S2);
loadimage(&img7,"D:\\C共享\\俄罗斯方块\\images\\square_orange.jpg");
putimage(S1,S1,&img7);
getimage(&img7,S1,S1,S2,S2);
loadimage(&img8,"D:\\C共享\\俄罗斯方块\\images\\square_purple.jpg");
putimage(S1,S1,&img8);
getimage(&img8,S1,S1,S2,S2);
loadimage(&img9,"D:\\C共享\\俄罗斯方块\\images\\square_red.jpg");
putimage(S1,S1,&img9);
getimage(&img9,S1,S1,S2,S2);
loadimage(&img10,"D:\\C共享\\俄罗斯方块\\images\\square_yellow.jpg");
putimage(S1,S1,&img10);
getimage(&img10,S1,S1,S2,S2);
loadimage(&img2,"E:\\Image\\c2.jpg",Width,Height);
putimage(0,0,&img2);
getimage(&img,S1+S2,S1+S2,(M-1)*S2,(N-1)*S2);
for (p=S1;p<S1+M*S2+1;p+=S2)
{
q=S1;
putimage(p,q,&img3);
barrier[(p-S1)/S2][(q-S1)/S2].x=p;
barrier[(p-S1)/S2][(q-S1)/S2].y=q;
q=S1+N*S2;
putimage(p,q,&img3);
barrier[(p-S1)/S2][(q-S1)/S2].x=p;
barrier[(p-S1)/S2][(q-S1)/S2].y=q;
}
for (q=S1;q<S1+N*S2+1;q+=S2)
{
p=S1;
putimage(p,q,&img3);
barrier[(p-S1)/S2][(q-S1)/S2].x=p;
barrier[(p-S1)/S2][(q-S1)/S2].y=q;
p=S1+M*S2;
putimage(p,q,&img3);
barrier[(p-S1)/S2][(q-S1)/S2].x=p;
barrier[(p-S1)/S2][(q-S1)/S2].y=q;
}
}
void RandNumber()
{
number3=(int)(rand()%7+4);
}
void RandGraph()
{
if (number==4)
{img11=img4; Yi();}
else if (number==5)
{img11=img5; LeftZ();}
else if (number==6)
{img11=img6; Ding();}
else if (number==7)
{img11=img7; RightZ();}
else if (number==8)
{img11=img8; Square();}
else if (number==9)
{img11=img9; LeftL();}
else
{img11=img10; RightL();}
}
void RandGraph2()
{
if (number2==4)
{img11=img4; Yi();}
else if (number2==5)
{img11=img5; LeftZ();}
else if (number2==6)
{img11=img6; Ding();}
else if (number2==7)
{img11=img7; RightZ();}
else if (number2==8)
{img11=img8; Square();}
else if (number2==9)
{img11=img9; LeftL();}
else
{img11=img10; RightL();}
for (b=0;b<4;b++)
{
ccc[b]=c[b]+S2*13;
ddd[b]=d[b];
}
}
void Action()
{
int a,l,wait,count3,count4=0,count5,speed=0;//count5计数有几行满了,最多四行
int get1,get2,get3,get4; //count4计数消去的行数
int score=0,Time=16;
char *str="初级";
IMAGE img12;
LOGFONT font;
getimage(&img12,S1+(M+1)*S2,S1+S2,Width-(S1+(M+1)*S2),Height-(S1+S2));
for (l=0;;l++)
{
count1=0;
count5=0;
score=count4*100;
if (count4>=20)
{
str="中级";
score=2000+(count4-20)*200;
Time=13;
}
if (count4>=40)
{
str="高级";
score=6000+(count4-40)*400;
Time=10;
}
gettextstyle(&font);
font.lfHeight=25;
font.lfQuality = ANTIALIASED_QUALITY;
_tcscpy(font.lfFaceName,_T("楷体")); //_tcscpy函数在tchar.h中
settextstyle(&font);
settextcolor(LIGHTBLUE);
sprintf(string,"水平: %s",str);
sprintf(string2,"得分:%d",score);
outtextxy(510,250,string);
outtextxy(510,350,string2);
settextcolor(WHITE);
outtextxy(590,560,_T("Ver 1.0"));
getimage(&img1,S1+S2,S1+S2,(M-1)*S2,(N-1)*S2);
BeginBatchDraw();
RandNumber();
number2=number3;
RandGraph2();
for (b=0;b<4;b++)
putimage(ccc[b],ddd[b],&img11);
if (l==0)
{
RandNumber();
number=number3;
}
RandGraph();
for (a=0;;a++)
{
for (b=0;b<4;b++)
putimage(c[b],d[b],&img11);
if (kbhit())
{
e=getch();
if (isascii(e))
goto loop;
else
e=getch();
loop: switch(e)
{
case 72:
{
if (number==8)
goto loop2;
if (number==4)
{
count1++;
if (count1%2==1)
{
c[0]=c[0]+S2;d[0]=d[0]-S2;
c[1]=c[1];d[1]=d[1];
c[2]=c[2]-S2;d[2]=d[2]+S2;
c[3]=c[3]-2*S2;d[3]=d[3]+2*S2;
}
if (count1%2==0)
{
c[0]=c[0]-S2;d[0]=d[0]+S2;
c[1]=c[1];d[1]=d[1];
c[2]=c[2]+S2;d[2]=d[2]-S2;
c[3]=c[3]+2*S2;d[3]=d[3]-2*S2;
}
}
else
rotate2();
if (room()==1)
Restore();
break;
}
case 75:
{
Move(-1);
if (room()==1)
Restore();
break;
}
case 77:
{
Move(1);
if (room()==1)
Restore();
break;
}
case 80:
speed=20;
break;
}
}
//判断是否碰到下面的障碍物:a如果为S2的倍数才判断,这样做提高效率,
//也可以a每加1就判断一次
if (a%S2==0)
{
count2=0;
for (b=0;b<4;b++)
{
p=c[b];q=d[b]+S2;
if (barrier[(p-S1)/S2][(q-S1)/S2].x==p && barrier[(p-S1)/S2][(q-S1)/S2].y==q)
{
for (f=0;f<4;f++)
{
p=c[f];q=d[f];
barrier[(p-S1)/S2][(q-S1)/S2].x=p;
barrier[(p-S1)/S2][(q-S1)/S2].y=q;
barrier[(p-S1)/S2][(q-S1)/S2].z=img11;
}
count2++;
break;
}
}
if (count2!=0)
break;
}
loop2: for (b=0;b<5;b++)
{
d[b]=d[b]+1;
cc[b]=c[b];
dd[b]=d[b];
}
FlushBatchDraw();
if (speed>0)
speed--;
else
Sleep(Time);
putimage(S1+S2,S1+S2,&img1);
}
EndBatchDraw();
//判断游戏结束
for (p=1;p<M;p++)
for (q=1;q<N;q++)
if (barrier[p][q].x==S1+p*S2 && barrier[p][q].y==S1+q*S2)
if (q==1)
{
settextcolor(RED);
settextstyle(40,40,_T("楷体"));
outtextxy(200,Height*1/2,_T("Game Over!"));
for (;;)
getch(); //游戏结束,无论按什么键都没用,窗口不关闭
closegraph(); //没有作用
exit(0); //没有作用
}
//
number=number2;
putimage(S1+(M+1)*S2,S1+S2,&img12);
for (q=1;q<N;q++)
{
count3=0; //判断某一行是否填满
for (p=1;p<M;p++)
if (barrier[p][q].x==S1+p*S2 && barrier[p][q].y==S1+q*S2)
count3++;
if (count3==M-1)
{
count4++; //用来统计消去的行数
count5++; //计数有几行满了,最多四行
//用get系列来接收哪一行满了
if (count5==1)
get1=q;
else if (count5==2)
get2=q;
else if (count5==3)
get3=q;
else
get4=q;
}
}
if (count5>0)
{
putimage(S1+S2,S1+S2,&img);
if (count5==1)
{
for (p=1;p<M;p++)
for (q=1;q<get1;q++)
functon1(count5);
for (p=1;p<M;p++)
function2(get1);
for (p=1;p<M;p++)
for (q=1;q<get1+1;q++)
function3();
}
if (count5==2)
{
for (p=1;p<M;p++)
{
for (q=1;q<get1;q++)
functon1(count5);
for (q=get1+1;q<get2;q++)
functon1(1);
}
for (p=1;p<M;p++)
{
function2(get1);
function2(get2);
}
for (p=1;p<M;p++)
for (q=1;q<get2+1;q++)
function3();
}
if (count5==3)
{
for (p=1;p<M;p++)
{
for (q=1;q<get1;q++)
functon1(count5);
for (q=get1+1;q<get2;q++)
functon1(2);
for (q=get2+1;q<get3;q++)
functon1(1);
}
for (p=1;p<M;p++)
{
function2(get1);
function2(get2);
function2(get3);
}
for (p=1;p<M;p++)
for (q=1;q<get3+1;q++)
function3();
}
if (count5==4)
{
for (p=1;p<M;p++)
{
for (q=1;q<get1;q++)
functon1(count5);
for (q=get1+1;q<get2;q++)
functon1(3);
for (q=get2+1;q<get3;q++)
functon1(2);
for (q=get3+1;q<get4;q++)
functon1(1);
}
for (p=1;p<M;p++)
{
function2(get1);
function2(get2);
function2(get3);
function2(get4);
}
for (p=1;p<M;p++)
for (q=1;q<get4+1;q++)
function3();
}
for (p=1;p<M;p++)
for (q=1;q<N;q++)
if (barrier[p][q].x==S1+p*S2 && barrier[p][q].y==S1+q*S2) //if判断不能少,曾经犯得错误之一
putimage(barrier[p][q].x,barrier[p][q].y,&barrier[p][q].z);
}
}
}
void main()
{
srand((unsigned)time(NULL));
Region();
Action();
} |
|