Turbo_c俄罗斯方块

/*******************programe dyheluosi.c***********************/
/************* to test the graphics functions*************/
/************* CUMTB 2002.7*******************************/

/*******
this programe to practise a small game ,all is about 20 kinds of
pictures .
they can change their forms,if you preST UP key;
they can accerlerat to go down ,if you preST DOWN key;
they can go left or right ,if you preST LEFT or RIGHT key;
preST ENTER to restart;
preST ESC to end and out the game;
on the right ,there are cues that how many rows you have got ,and there
are also the next picture that will appear.
*******/
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<string.h>

#define len 10 /**** the picture unit is a 10*10 squre *********/
#define LT 10000 /*** --ms ****/
#define ST 5000 /*** --ms ****/
#define DOWN 20480 /*********define the preST key number********/
#define CHANGE 18432 /* the UP key */
#define LEFT 19200
#define RIGHT 19712
#define OUT 283 /* the ESC key*/
#define RES 7181
#define BOTTOM 29 /****** from 100 to 400 ,may be is 30***********/


/*****************define the structure of the picture*********/
struct point {
int x;
int y;
};
struct pic
{
struct point p[4];
int next;
} ;
/*************** end define of picture structure**************/

struct pic picture [19]; /* to store the 19 kinds of picture forms */
int bot[19][30], /* record aLT the picture unit situation, if fuLT then 1,else -1 */
*buffer, /* picture 10*10 unit */
Ss=0; /* record score*/
int setloc[][6]={{0,0,0,1,2,3},{1,2,3,0,0,0},{1,0,1,0,1,1},{-1,0,1,1,1,1},{0,0,-1,1,2,1},{1,2,1,0,0,1},
{0,0,1,1,2,1},{1,1,2,0,1,1},{0,-1,-1,1,1,2},{1,0,-1,0,1,1},{0,1,1,1,1,2},{-2,-1,0,1,1,1},{1,1,1,0,1,2},
{0,1,2,1,0,0},{0,0,1,1,2,2},{0,1,2,1,1,1},{0,0,-1,1,2,2},{1,2,2,0,0,1},{0,0,1,1,2,0}}
;
void init();
void godown (int );
void goleft(int );
void goright(int );
void clearbox();
int change(int );
void getbot(int );
void draw(int );
void location(int,int,int );
void scan(int );
void score( int );
void dispscore();
void start();
int dealcon(int );
int conflict(int );
void music();
void exitgame();

 

void location( int c,int r,int i)
/*******initial the picture data ******/
{
int n;
struct pic *pp=&picture[i];

pp->p[0].y=r;
pp->p[0].x=c;
pp->p[1].x=c+setloc[i][0];pp->p[2].x=c+setloc[i][1];pp->p[3].x=c+setloc[i][2];
pp->p[1].y=r+setloc[i][3];pp->p[2].y=r+setloc[i][4];pp->p[3].y=r+setloc[i][5];
}

 

void init ( )
/******************initial graph********************/
{
int gd = DETECT , gm , gresult ;
int size,i,j,n ;

initgraph ( &gd, &gm, "" );
gresult = graphresult ( );
if( gresult != grOk ){
printf ( " \n initial graph failure! \n " );
getch ();
exit (1);
}

for( i =0; i<=18;i++ ) /*for convenient to dealcon(), set extra the x direction*/
for(j=0;j<=29;j++)
bot[i][j]=-1;

setcolor(YELLOW);
setfillstyle(1,WHITE);
rectangle(40,40,50,50);
floodfill(42,42,YELLOW);
for(n=0;n<=17 ;n++ )
picture[n].next=n+1;
picture[1].next=0;
picture[2].next=2;
picture[6].next=3;
picture[8].next=7;
picture[10].next=9;
picture[14].next=11;
picture[18].next=15;
size=imagesize(40,40,50,50);
buffer=malloc(size);
getimage(40,40,50,50,buffer);
cleardevice();
/* interface setup*/
setfillstyle(1,BLUE);
setcolor(WHITE);
rectangle(50,50,600,450);
floodfill(56,56,WHITE);
rectangle(52,52,598,72);
outtextxy(54,60,"<ENTER>--->to RESTART the game");
outtextxy(354,60,"<ESC>----->to END the game");
setcolor(YELLOW);
outtextxy(165,430,"Copyright is owned by DYH from 2003 to 2008");
} /*end init()*/

void draw(int i)
/****** if locationed the picture,then can draw the picture*******/
{
int n=0,xx,yy;
struct pic* pp=&picture[i];

for(;n<=3;n++){
xx=pp->p[n].x*len+150;
yy=pp->p[n].y*len+100;
putimage(xx,yy,buffer,XOR_PUT);
}
}

void drawnext(int i)
/********* to display the next picture,to give the player one cue*******/
{
int n=0,xx,yy;
struct pic* pp=&picture[i];

location(0,0,i);
for(;n<=3;n++){
xx=pp->p[n].x*len+400;
yy=pp->p[n].y*len+300;
putimage(xx,yy,buffer,XOR_PUT);
}
}

main()
{
char str[4], *str1=" your score is :";
int pc,next,i,gameover=0,line; /* pc is current picture id,*/
/*next is the next picture id*/
int key,restart=0; /*key is the bioskey() number*/
/*ST delay the shorter time ;LT delay the longer time */
init();
setcolor(GREEN);
rectangle(148,98,312,402);
setcolor(GREEN);
setlinestyle(0,0,3);
setfillstyle(1,BLACK);
floodfill(155,123,GREEN);
rectangle(145,95,315,405);
outtextxy(375,220,str1);
outtextxy(390,280,"Next picture is:");

while (!kbhit()); /*to setup the interface*/
srand(time(NULL)); /* use time to generate the random picture*/
while(!restart&&!gameover){
pc=rand()%19;
while(!gameover){
next=rand()%19;
drawnext(next);
location(5,0,pc);
if(reachbot(pc)){gameover=1;break;}
draw(pc);
key=10;
while(!gameover&&!reachbot(pc)){
if(kbhit())key=bioskey(0);
delay(ST);
draw(pc);
if(key==OUT) {exitgame();exit(1);} /*to deal with the keydown method*/
else if(key==RES){gameover=1;restart=1;break;}
else if(key==LEFT){
goleft(pc);
draw(pc);
}
else if(key==RIGHT){
goright(pc);
draw(pc);
}
else if(key==CHANGE){
pc=change(pc);
draw(pc);
}
else if(key==DOWN){
godown(pc);
draw(pc);
}
else {
godown(pc);
draw(pc);
delay(LT);
}
delay(ST);
key=10;
}
if(reachbot(pc)){
getbot(pc);
scan(pc);
drawnext(next);
pc=next;
}
}
if(!restart){
while(!kbhit()){}
if(kbhit()){ key=bioskey(0);
if(key==RES) restart=1;
else clearbox();
}
}
if(restart){ gameover= 0;start();drawnext(next);restart=0;}
}

free(buffer);
exitgame();
}

int reachbot(int i)
{
int n,xx,yy;
struct pic *pp=&picture[i];

for(n=0 ;n<=3;n++){
xx=pp->p[n].x;
yy=pp->p[n].y;
if(yy>=29|| bot[xx][yy+1]>0){
return 1;
}
}

return 0;

}

int reachleft(int i)
{
int n,xx,yy;
struct pic *pp=&picture[i];

for(n=0;n<=3;n++){
xx=pp->p[n].x;
yy=pp->p[n].y;
if(xx<=0|| bot[xx-1][yy ]>0)return 1;
}
return 0;
}

int reachright(int i)
{
int n,xx,yy;
struct pic *pp=&picture[i];

for(n=0;n<=3;n++){
xx=pp->p[n].x;
yy=pp->p[n].y;
if(xx>=15|| bot[xx+1][yy ]>0)return 1;
}
return 0;
}


/*************to run the keydown method********/
void getbot(int i)
{
struct pic *pp=&picture[i];
int n=0,xx,yy;

for(;n<=3;n++){
xx=pp->p[n].x;
yy=pp->p[n].y;
bot[xx][yy]=1;
}
}

void goleft(int i)
{
int xx,yy;
struct pic *pp=&picture[i];

xx=pp->p[0].x;
yy=pp->p[0].y;
if(!reachleft(i))location(xx-1,yy,i);

}

void goright(int i)
{
int xx,yy;
struct pic *pp=&picture[i];

xx=pp->p[0].x;
yy=pp->p[0].y;
if(!reachright(i))location(xx+1,yy,i);

}
void godown(int i)
{
struct pic *pp=&picture[i];
int xx,yy;

xx=pp->p[0].x;
yy=pp->p[0].y;
if(!reachbot(i))location(xx,yy+1,i);

}


void scan(int p)
/**** if get to the bottom then scan the bottom ****/
{
struct pic *pp=&picture[p];
int n=0,rr=0,yy,xx;
int i,j;
int rec[4];
int full,repeat,*buffer2,size;;

for(;n<=3;n++){ /****to check whether the bottom is full or not **/
full=1;repeat=0;
yy=pp->p[n].y;
for(j=0;j<=15;j++){

if(bot[j][yy]<0){ full=0; break; }
}
if(full){
for(j=0;j<=rr;j++)if(rec[j]==yy)repeat=1;
if(!repeat)rec[++rr]=yy;
}
}

score(rr); /***to record the score*****/

if(rr>0){ /**if exist one row is full ,then cancel it */
for(i=1;i<=rr;i++){
yy=rec[i];
for(xx=0;xx<=15;xx++){
bot[xx][yy]=-1;
putimage(xx*len+150,yy*len+100,buffer,XOR_PUT);
}
size=imagesize(150,100,310,yy*len+100);
buffer2=malloc(size);
getimage(150,100,310,yy*len+100,buffer2);
putimage(150,100,buffer2,XOR_PUT);
putimage(150,110,buffer2,XOR_PUT);
free(buffer2);
for(xx=0;xx<=15;xx++)
for(j=yy;j>=0;j--)
if(bot[xx][j]>0){
bot[xx][j]=-1;bot[xx][j+1]=1;}
}
music();
}/* *end if **/

if(rr>0)dispscore();
}/*******end scan()********/

void score(int i)
/******to come ture the score method******/
{
static two= -1;

if(i==1)Ss+=100;
else if(i==3)Ss+=400;
else if(i==4)Ss+=600;
else if(i==2){
if(two>0)Ss+=300;else Ss+=200;
two=-two;
}
}/*******end scan ()*******/

void dispscore()
/********to display the score********/
{
char str[8];
int *buffer3,size;

itoa(Ss,str,10);
setfillstyle(1,BLUE);
size=imagesize(340,230,420,270);
buffer3=malloc(size);
getimage(340,230,420,270,buffer3);
putimage(340,230,buffer3,XOR_PUT);
floodfill(345,234,BLUE);
outtextxy(390,240,str);
free(buffer3);
}

void clearbox()
{
int i,j,xx,yy;
for(j=29;j>=0;j--)
for(i=0;i<=15;i++)
{
if(bot[i][j]>0)bot[i][j]=-1; /* to initial the picture mrtrix*/
xx=i*len+150;
yy=j*len+100;
putimage(xx,yy,buffer,COPY_PUT);
putimage(xx,yy,buffer,XOR_PUT);
}

/* to clear the box*/
}

void start()
/*******to deal with the REstart key down method******/
{
int *buffer3,size;

clearbox();
setfillstyle(1,BLUE);
size=imagesize(340,230,420,270);
buffer3=malloc(size);
getimage(340,230,420,270,buffer3);
putimage(340,230,buffer3,XOR_PUT); /* to rebuild the display score area*/
floodfill(343,235,BLUE);
free(buffer3);
}

int conflict( int i)
/********to check the conflict*********/
{
struct pic *pp=&picture[i];
int xx,yy, n;

for(n=0;n<=3;n++){
xx=pp->p[n].x;
yy=pp->p[n].y;
if( xx<0||bot[xx][yy]>0)return 1; /*left conflict,return 1; */
else if (xx>15||bot[xx][yy]>0)return 2;/*right conflict,return 2; */
else if (yy>29||bot[xx][yy]>0)return 3;/* down conflict ,return 3; */

}
return 0;
}

int dealcon(int i)
/********to deal with the conflict*****/
{
struct pic *pp=&picture[i];
int xx,yy,n;

int con=0, /*conflict flag byte*/
rec[4]={0,0,0,0}; /*record how many times check in the same derection*/

xx=pp->p[0].x;
yy=pp->p[0].y;
con=conflict(i);
if(!con)return 0;
while(con){
switch(con){
case 1:location(++xx,yy,i);rec[con]++;con=conflict(i);break;
case 2:location(--xx,yy,i);rec[con]++;con=conflict(i);break;
case 3:location(xx,--yy,i);rec[con]++;con=conflict(i);break;
}

for(n=1;n<=3;n++)if(rec[n]>4)return 1; /* limited times of check*/
}
return 0;
}

int change(int i)
{
int n,xx,yy,decide=0;
struct pic *pp=&picture[i];
if (i==2)return 2;
xx=pp->p[0].x;
yy=pp->p[0].y;

for(n=0;n<=18;n++)if(n==picture[i].next)break;
location(xx,yy,n);
decide=dealcon(n);
if(!decide)return n;
else return i;
}


void music()
/*** if one row is full then play some "sound"****/
{
sound(600);
delay(5000);
sound(700);
delay(5000);
sound(500);
delay(5000);
sound(600);
delay(5000);
nosound();
}

void exitgame()
/*** to write some end message****/
{
cleardevice();
printf("\nOK,my dear friends,game is over!\n");
printf("\nIf you have some good suggestion,please\n call me:010-62314216\n");
printf("or send me e-mail:dyh480@sohu.com\n");
getch();
closegraph();
}

 

posted on 2005-05-22 08:43  奇远  阅读(769)  评论(0编辑  收藏  举报

导航