这个方块写得很简单,我只保留了一部分功能,但能运行,
初学编程,不足之处请原谅,
不明白之处可以发邮件给我。
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
#include<bios.h>
#define TIMEINT 5
#define X x-10
#define d 20
#define up 72
#define left 75
#define right 77
#define enter 2
#define esc 1
int x,y,boxshape,boxdir,start=1;//方块横纵坐标,方块外形,方块方向
int board[22][12];//桌面
int fq[7][4][9]={
{{-2,0,-1,0,0,0,0,1,1},
{0,-1,0,0,-1,1,0,1,2},
{-2,0,-2,1,-1,1,0,1,3},
{-1,-1,-1,0,-1,1,0,-1,0}},
{{-1,-1,-1,0,0,0,0,1,1},
{-1,0,-2,1,-1,1,0,0,2},
{-1,-1,-1,0,0,0,0,1,3},
{-1,0,-2,1,-1,1,0,0,0}},
{{-2,0,-2,1,-1,0,0,0,1},
{-1,-1,0,-1,0,0,0,1,2},
{0,0,-2,1,-1,1,0,1,3},
{-1,-1,-1,0,-1,1,0,1,0}},
{{0,-1,-1,0,0,0,0,1,1},
{-1,0,-2,1,-1,1,0,1,2},
{-1,-1,-1,0,-1,1,0,0,3},
{-2,0,-1,0,-1,1,0,0,0}},
{{-1,0,-1,1,0,0,0,1,1},
{-1,0,-1,1,0,0,0,1,1},
{-1,0,-1,1,0,0,0,1,1},
{-1,0,-1,1,0,0,0,1,1}},
{{-3,0,-2,0,-1,0,0,0,1},
{-1,-1,-1,0,-1,1,-1,2,2},
{-3,0,-2,0,-1,0,0,0,1},
{-1,-1,-1,0,-1,1,-1,2,2}},
{{0,-1,-1,0,-1,1,0,0},
{-2,0,-1,0,-1,1,0,1},
{0,-1,-1,0,-1,1,0,0},
{-2,0,-1,0,-1,1,0,1}},};
int collison();
void init();
void drawbox();
void clearoldbox();
int timeCome();
int get_key();
main()
{
int gdrive=VGA,gmode=VGAHI,errorcode;
int i,j,flag,k,m,key;
initgraph(&gdrive,&gmode,"d:\\tc");
errorcode=graphresult();
if(errorcode!=0)
{printf("the graph error:%s",grapherrormsg(errorcode));
printf("press any key to halt");
getch();
exit(1);
}
for(i=0;i<22;i++)
board[i][0]=board[i][11]=1;
for(j=0;j<12;j++)
board[21][j]=1;
for(i=1;i<=20;i++)
for(j=1;j<=10;j++)
board[i][j]=0;
init();
while(1)
{
if(start==1){x=15;y=4;boxshape=rand()%7;boxdir=0;drawbox();start=0;}
if(bioskey(1)!=0)
{key=get_key();
switch(key)
{case left: clearoldbox();
x=x-1;
if(collison())x=x+1;
else x=x;
drawbox();
break;
case right: clearoldbox();
x=x+1;
if(collison())x=x-1;
else x=x;
drawbox();
break;
case up: clearoldbox();
boxdir=(boxdir+1)%4;
if(collison())boxdir=(boxdir+3)%4;
else boxdir=boxdir;
drawbox();
break;
case esc:exit(0);
default :break;
}
}
if(timeCome())
{clearoldbox();
y=y+1;
if(collison())
{if(y>4)y=y-1;
else {getch();exit(0);}
board[y+fq[boxshape][boxdir][0]-1][x+fq[boxshape][boxdir][1]-9]=1;
board[y+fq[boxshape][boxdir][2]-1][x+fq[boxshape][boxdir][3]-9]=1;
board[y+fq[boxshape][boxdir][4]-1][x+fq[boxshape][boxdir][5]-9]=1;
board[y+fq[boxshape][boxdir][6]-1][x+fq[boxshape][boxdir][7]-9]=1;
drawbox();
for(i=20;i>4;i--)
{ flag=1;
for(j=1;j<11;j++)
{flag=flag&&board[i][j];
/*if(board[i][j]==0)flag=1;else flag=flag;*/}
if(flag)
{
for(k=i;k>4;k--)
for(m=1;m<=10;m++)
board[k][m]=board[k-1][m];
init();
}
}
start=1;
}
else
drawbox();
}
}
}
int collison()
{
/*if(board[y+fq[boxshape][boxdir][0]-1][x+fq[boxshape][boxdir][1]-9]==1)return 1;
if(board[y+fq[boxshape][boxdir][2]-1][x+fq[boxshape][boxdir][3]-9]==1)return 1;
if(board[y+fq[boxshape][boxdir][4]-1][x+fq[boxshape][boxdir][5]-9]==1)return 1;
if(board[y+fq[boxshape][boxdir][6]-1][x+fq[boxshape][boxdir][7]-9]==1)return 1;
return 0; */
return(board[y+fq[boxshape][boxdir][2]-1][x+fq[boxshape][boxdir][3]-9]||
board[y+fq[boxshape][boxdir][4]-1][x+fq[boxshape][boxdir][5]-9]||board[y+fq[boxshape][boxdir][6]-1][x+fq[boxshape][boxdir][7]-9]||
board[y+fq[boxshape][boxdir][0]-1][x+fq[boxshape][boxdir][1]-9]);
}
void init()
{
int i,j;
cleardevice();
setbkcolor(GREEN);
setcolor(RED);
rectangle(199,39,401,441);
setfillstyle(1,15);
for(i=20;i>4;i--)
for(j=1;j<11;j++)
if(board[i][j])bar((10+j-1)*d+1,(i+1)*d+1,(10+j)*d-1,(i+2)*d-1);
}
void drawbox()
{setfillstyle(1,15);
bar(
(x+fq[boxshape][boxdir][1])*d+1,
(y+fq[boxshape][boxdir][0])*d+1,
(x+fq[boxshape][boxdir][1]+1)*d-1,
(y+fq[boxshape][boxdir][0]+1)*d-1);
bar(
(x+fq[boxshape][boxdir][3])*d+1,
(y+fq[boxshape][boxdir][2])*d+1,
(x+fq[boxshape][boxdir][3]+1)*d-1,
(y+fq[boxshape][boxdir][2]+1)*d-1);
bar(
(x+fq[boxshape][boxdir][5])*d+1,
(y+fq[boxshape][boxdir][4])*d+1,
(x+fq[boxshape][boxdir][5]+1)*d-1,
(y+fq[boxshape][boxdir][4]+1)*d-1);
bar(
(x+fq[boxshape][boxdir][7])*d+1,
(y+fq[boxshape][boxdir][6])*d+1,
(x+fq[boxshape][boxdir][7]+1)*d-1,
(y+fq[boxshape][boxdir][6]+1)*d-1);
}
void clearoldbox()
{setfillstyle(1,GREEN);
bar(
(x+fq[boxshape][boxdir][1])*d,
(y+fq[boxshape][boxdir][0])*d,
(x+fq[boxshape][boxdir][1]+1)*d,
(y+fq[boxshape][boxdir][0]+1)*d);
bar(
(x+fq[boxshape][boxdir][3])*d,
(y+fq[boxshape][boxdir][2])*d,
(x+fq[boxshape][boxdir][3]+1)*d,
(y+fq[boxshape][boxdir][2]+1)*d);
bar(
(x+fq[boxshape][boxdir][5])*d,
(y+fq[boxshape][boxdir][4])*d,
(x+fq[boxshape][boxdir][5]+1)*d,
(y+fq[boxshape][boxdir][4]+1)*d);
bar(
(x+fq[boxshape][boxdir][7])*d,
(y+fq[boxshape][boxdir][6])*d,
(x+fq[boxshape][boxdir][7]+1)*d,
(y+fq[boxshape][boxdir][6]+1)*d);
}
int timeCome()
{
static long tm,old;
tm=biostime(0,tm);
if(tm-old<TIMEINT) return 0;
else
{
old=tm; return 1;
}
}
int get_key()
{union REGS rg;
rg.h.ah=0;
int86(0x16,&rg,&rg);
return rg.h.ah;
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |