/*游戏解释:按F1键开始游戏,在屏幕上循环落下小方块,底下有一小方块,可以通过方向键控制左右移动,以便接到落下的方块,接到的话就加分100(记分代码暂未写),没接到继续接,循环直到按下F2键时结束。以下代码有问题:应该在运行时就实现按F2退出不玩游戏或者按F1开始游戏,而在游戏中,FOR循环里里,在方块落下的同时应该要能够使用方向键控制底下的方块,且也能够按F2退出游戏,但目前实现不了,请各位帮帮。。。*/
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#define KEY_LEFT 27
#define KEY_RIGHT 26
/*#define F1 59 */
#define F2 60
int get_key();/*键识别函数*/
struct{
void *buffer;
int size;
}buff[4],buff2[2];
char *SCORE="0";
char *color;
FK_LEFT(int i,int j,int cj)
{
putimage(i,j,buff2[1].buffer,1);
delay(cj);
putimage(i,j,buff2[0].buffer,3);
putimage(i-1,j,buff2[1].buffer,1);
}
FK_RIGHT(int i,int j,int cj)
{
putimage(i,j,buff2[1].buffer,1);
delay(cj);
putimage(i,j,buff2[0].buffer,3);
putimage(i+1,j,buff2[1].buffer,1);
}
get_key()
{
union REGS rg;
rg.h.ah=0;
int86(0x16,&rg,&rg);
return rg.h.ah;
}
FK_RANDOM()/**/
{
setcolor(BLACK);
rectangle(200,60,215,75);
floodfill(210,70,BLACK);
buff2[0].size=imagesize(200,60,215,75);
buff2[0].buffer=(void*)malloc(buff2[0].size);
getimage(200,60,215,75,buff2[0].buffer);
setcolor(RED);
setfillstyle(1,RED);
rectangle(200,60,215,75);
floodfill(210,70,RED);
buff[0].size=imagesize(200,60,215,75);
buff[0].buffer=(void*)malloc(buff[0].size);
getimage(200,60,215,75,buff[0].buffer);
putimage(200,60,buff2[0].buffer,3);
setcolor(BLUE);
setfillstyle(1,BLUE);
rectangle(200,60,215,75);
floodfill(210,70,BLUE);
buff[1].size=imagesize(200,60,215,75);
buff[1].buffer=(void*)malloc(buff[1].size);
getimage(200,60,215,75,buff[1].buffer);
putimage(200,60,buff2[0].buffer,3);
setcolor(WHITE);
setfillstyle(1,WHITE);
rectangle(200,60,215,75);
floodfill(210,70,WHITE);
buff[2].size=imagesize(200,60,215,75);
buff[2].buffer=(void*)malloc(buff[2].size);
getimage(200,60,215,75,buff[2].buffer);
putimage(200,60,buff2[0].buffer,3);
setcolor(YELLOW);
setfillstyle(1,YELLOW);
rectangle(200,60,215,75);
floodfill(210,70,YELLOW);
buff[3].size=imagesize(200,60,215,75);
buff[3].buffer=(void*)malloc(buff[3].size);
getimage(200,60,215,75,buff[3].buffer);
putimage(200,60,buff2[0].buffer,3);
setcolor(BROWN);
setfillstyle(1,BROWN);
rectangle(200,60,215,75);
floodfill(210,70,BROWN);
buff2[1].size=imagesize(200,60,215,75);
buff2[1].buffer=(void*)malloc(buff2[1].size);
getimage(200,60,215,75,buff2[1].buffer);
putimage(200,60,buff2[0].buffer,3);
}
FK_START()/*初始化图形函数*/
{char *FILE="WECOME UEING FK_GAME! SCORE: ";
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"");
setbkcolor(BLACK);
setcolor(BLUE);
setfillstyle(1,BLUE);
rectangle(1,1,639,30);
floodfill(5,8,BLUE);
setcolor(WHITE);
settextstyle(1,0,1);
outtextxy(5,8,FILE);
outtextxy(385,8,SCORE);
}
main()
{int x,y,i=300,j=464,sj=10000,key;
char ch;
FK_START();/*初始化界面*/
FK_RANDOM();/*小方块模型函数*/
putimage(i,j,buff2[1].buffer,1);/*输出底下小方块*/
ch=getch();
if(ch==35)/*F1的ASCII码是35吗?*/
{do
{
for(x=random(450),y=35,color=buff[random(4)].buffer;y<=479;y++)
{key=0;
while(bioskey(1)==0);
key=get_key();
putimage(x,y,color,1);/*随机产生小方块*/
delay(sj);
putimage(x,y,buff2[0].buffer,3);/*清除小方块*/
if(x==i&&y==j)
{delay(sj);
putimage(i,j,buff2[0].buffer,3);
putimage(i,j,buff2[1].buffer,1);
/*jifen*/
continue;
}
if(key==F2) exit(0);
if(key==KEY_LEFT)/*左移动*/ FK_LEFT(i,j,sj);
if(key==KEY_RIGHT)/*右移动*/ FK_RIGHT(i,j,sj);
}
closegraph();
}while(1);
}
}