论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

改进的游戏

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:47:01

/*-----------------运行环境tc2.0-------------------------------*/
/*-----------------作者:李云峰--------------------------------*/
/*-----------------oicq:21084598-------------------------------*/
/*-----------------email:lixiaolong@eyou.com-------------------*/

#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include <dos.h>
/*-------------------------------------------------------*/
void setfram(void);
void barr(int,int,int,int);
void changed(int,int,int*);
void barc(int,int,int,int);
void clearc(int,int,int*);
void delay1(unsigned x);
void initpump(void);
int test(int xx,int yy,int left,int right,int bottom,int roll);
void randone(void);
void mark(int xx,int yy);
int scans(void);
void kick(int rownum);
void redisplay(void);
void exchanged(void);
void outnum(int x,int y,int num);
void gameover(void);

/*-----------------------------------------------------*/
static int pump[37][18];
static int pause=0;
static int i=0;
static int *p[4];
static int x,y;
static int inext;
static int *pnext[4];
static int xnext=60,ynext=0;
static int quit=0;
static int *pp[7][4];
static int score;
static int layer;
static int t0[4][4]={{2,3,1},{0,1,0}};
static int t1[4][4]={{3,-2},{1,1},{1,0}};
static int t2[4][4]={{-2,3,0},{1,1,1}};
static int t3[4][4]={{-3,2},{1,1},{0,1}};
static int f[4][4]={{2,2},{1,1}};
static int a0[4][4]={{2,3,0},{0,1,1}};
static int a1[4][4]={{-3,2},{1,1},{1,0}};
static int b0[4][4]={{-2,3,1},{1,1,0}};
static int b1[4][4]={{3,-2},{1,1},{0,1}};
static int c0[4][4]={{3,2},{1,0},{1,0}};
static int c1[4][4]={{2,-3,0},{1,1,1}};
static int c2[4][4]={{-3,2},{0,1},{1,1}};
static int c3[4][4]={{2,3,1},{0,0,1}};
static int d0[4][4]={{3,2},{0,1},{0,1}};
static int d1[4][4]={{2,3,1},{1,0,0}};
static int d2[4][4]={{3,-2},{1,0},{1,1}};
static int d3[4][4]={{-2,-3,1},{1,1,1}};
static int e0[4][4]={{-4,2},{0,1},{0,1},{0,1}};
static int e1[4][4]={{-2,-4,0,0},{1,1,1,1}};


/*--------------------------------------------------------*/
main(void)
{ extern int i;
extern int *p[4];
extern int x,y;
extern int layer,score;
int exchangeon;
int rownum;
int count;
char ch;
int drive=VGA,mode=2;
initgraph(&drive,&mode,"");
/*##############数据初始化#####################################*/
pp[0][0]=t0[0];pp[0][1]=t1[0];pp[0][2]=t2[0];pp[0][3]=t3[0];
pp[1][0]=f[0];pp[1][1]=f[0];pp[1][2]=f[0];pp[1][3]=f[0];
pp[2][0]=a0[0];pp[2][1]=a1[0];pp[2][2]=a0[0];pp[2][3]=a1[0];
pp[3][0]=b0[0];pp[3][1]=b1[0];pp[3][2]=b0[0];pp[3][3]=b1[0];
pp[4][0]=c0[0];pp[4][1]=c1[0];pp[4][2]=c2[0];pp[4][3]=c3[0];
pp[5][0]=d0[0];pp[5][1]=d1[0];pp[5][2]=d2[0];pp[5][3]=d3[0];
pp[6][0]=e0[0];pp[6][1]=e1[0];pp[6][2]=e0[0];pp[6][3]=e1[0];
/*##############################################################*/
initpump();
setfram();
/*##############主程序#########################################*/

randone();
exchanged();
exchangeon=1;
while(1)
{if(exchangeon)
{randone();
barc(220,30,289,99);
changed(240,50,pnext[inext]);
exchangeon=0;}
delay1(15);
if(quit) break;
if(test(x,y,0,0,1,0))
{ clearc(x,y,p[i]);
y+=10;
changed(x,y,p[i]);
}
else {mark(x,y);
exchanged();
exchangeon=1;
if(!test(x,y,0,0,1,0)) {gameover();
ch=getch();
while(!(ch=='p'||ch=='P'))
{if(ch=='q'||ch=='Q') break;ch=getch();}
if(ch=='q'||ch=='Q') break;
initpump();
clearviewport();
layer=0;
score=0;
barc(220,160,289,189);
outnum(223,170,layer);
barc(220,250,289,279);
outnum(223,260,score);
}
count=0;
rownum=scans();
while(rownum)
{count++;layer++;
kick(rownum); rownum=scans();
redisplay();
}
barc(220,160,289,189);
outnum(223,170,layer);
switch(count){case 1:score+=100;break;
case 2:score+=300;break;
case 3:score+=500;break;
case 4:score+=700;break;
}
barc(220,250,289,279);
outnum(223,260,score);

}
if(pause){ ch=getch();
while(!(ch=='p'||ch=='P'))
{if(ch=='q'||'Q') break;ch=getch();}
pause=0;
if(ch=='q'||ch=='Q') break;
}
}
/*##############################################################*/

closegraph();
}


/*---------------------------------------------------------------*/
void changed(int xx,int yy,int*pp)
{int maxm,maxn,xx1,m,n;
maxm=abs(*pp);maxn=abs(*(pp+1));
xx1=xx;
for(m=0;m<maxm;m++)
{xx=xx1;
for(n=0;n<maxn;n++)
{if(*(pp+m*4+n)>0)
barr(xx,yy,xx+9,yy+9);
xx+=10;
}
yy+=10;
}
}
/*-----------------------------------------------------------*/
void clearc(int xx,int yy,int*pp)
{int maxm,maxn,xx1,m,n;
maxm=abs(*pp);maxn=abs(*(pp+1));
xx1=xx;
for(m=0;m<maxm;m++)
{xx=xx1;
for(n=0;n<maxn;n++)
{if(*(pp+m*4+n)>0)
barc(xx,yy,xx+9,yy+9);
xx+=10;
}
yy+=10;
}
}
/*--------------------------------------------------------------*/
void barr(int left,int top,int right,int bottom)
{char pattern[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
setcolor(0);
rectangle(left,top,right,bottom);
setfillpattern(pattern,rand()%15+1);
bar(++left,++top,--right,--bottom);
}
/*-------------------------------------------------------------*/
void barc(int left,int top,int right,int bottom)
{char pattern[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
setfillpattern(pattern, 0);
bar(left,top,right,bottom);
}
/*-------------------------------------------------------------*/

void initpump(void)
{ extern int pump[37][18];
int m,n;
n=0;
for(m=0;m<37;m++)
pump[m][n]=1;
n=17;
for(m=0;m<37;m++)
pump[m][n]=1;
m=36;
for(n=0;n<18;n++)
pump[m][n]=1;
for(m=0;m<36;m++)
for(n=1;n<17;n++)
pump[m][n]=0;
}
/*------------------------------------------------------------*/
void delay1(unsigned xx)
{int m;
extern int i;
extern int *p[4];
extern int pause;
extern int quit;
union REGS in,out;
unsigned int zf;

for(m=0;m<800;m++)
{ while(1)
{ in.h.ah=0x01;
int86(0x16,&in,&out);
zf=out.x.flags&0x0040;
if(zf) break;
in.h.ah=0x00;
int86(0x16,&in,&out);
switch(out.h.al)
{case 'j':
case 'J': if(test(x,y,0,0,0,1))
{clearc(x,y,p[i]);
i++;
if(i>3) i=0;
changed(x,y,p[i]);
}
break;

case 'a':
case 'A': if(test(x,y,1,0,0,0))
{clearc(x,y,p[i]);
x-=10;
changed(x,y,p[i]);
} break;
case 'd':
case 'D': if(test(x,y,0,1,0,0))
{clearc(x,y,p[i]);
x+=10;
changed(x,y,p[i]);
} break;
case 's':
case 'S': m=9999;break;

case 'p':
case 'P':pause=1;break;

case 'q':
case 'Q':quit=1;break;
default:break;
}
}


delay(xx);
}

}

/*----------------------------------------------------------------*/
int test(int xx,int yy,int left,int right,int bottom,int roll)
{extern int i;
extern pump[37][18];
extern int *p[4];
int *pp;
int maxm,maxn,m,n,k;
int row=yy/10;
int col=xx/10+1;
int temp;
temp=i;
i=i+roll;
if(i>3) i=0;
pp=p[i];
maxm=abs(*pp);maxn=abs(*(pp+1));
k=1;
for(m=0;m<maxm;m++)
for(n=0;n<maxn;n++)
if(*(pp+m*4+n)>0&&pump[row+m+bottom][col+n-left+right])
k=0;
i=temp;
return k;
}
/*----------------------------------------------------------------*/
void setfram(void)
{int left, top, right, bottom;
left =178;
top = 78;
right =341;
bottom =441;

rectangle(50,10,500,465);
rectangle(49,9,501,466);

settextstyle(0,0,1);
setcolor(9);
outtextxy(70,140,"LEFT: A");
outtextxy(70,170,"RIGHT: D");
outtextxy(70,200,"DOWN: S");
outtextxy(70,230,"ROLL: J");
outtextxy(70,260,"QUIT: Q");
outtextxy(70,290,"PAUSE: P");
outtextxy(70,320,"START: P");

settextstyle(0,0,2);
setcolor(10);
outtextxy(110,30,"WELCOME TO OUR GAME !");

setcolor(10);
rectangle(left,top,right,bottom);
left++;
top++;
right--;
bottom--;
setcolor(9);
rectangle(left,top,right,bottom);
setviewport(180, 80, 339,439, 0);

setcolor(14);
rectangle(218,28,291,101);
rectangle(219,29,290,100);
settextstyle(0,0,1);
setcolor(4);
outtextxy(170,60,"NEXT");


setcolor(4);
rectangle(218,158,291,191);
rectangle(219,159,290,190);
barc(220,160,289,189);
outnum(223,170,0);
settextstyle(0,0,1);
setcolor(9);
outtextxy(170,165,"LAYER");

setcolor(3);
rectangle(218,248,291,281);
rectangle(219,249,290,280);
barc(220,250,289,279);
outnum(223,260,0);
settextstyle(0,0,1);
setcolor(6);
outtextxy(170,255,"SCORE");

settextstyle(0,0,0);
setcolor(15);
outtextxy(25,180,"PRE ANY KEY TO");
outtextxy(50,195,"START !");
getch();
clearviewport();
}

/*----------------------------------------------------------------*/
void randone(void)
{ extern int inext;
extern int *pnext[4];
extern int *pp[7][4];
int m,n;

m=rand()%7;
for(n=0;n<4;n++) pnext[n]=pp[m][n];
inext=rand()%4;

}
/*----------------------------------------------------------------*/
void exchanged(void)
{ extern int inext;
extern int *pnext[4];
extern int xnext,ynext;
extern int i;
extern int *p[4];
extern int x,y;
int m;
for(m=0;m<4;m++)
p[m]=pnext[m];

i=inext;
x=xnext;
y=ynext;
changed(x,y,p[i]);
}
/*------------------------------------------------*/
void mark(int xx,int yy)
{extern int i;
extern pump[37][18];
extern int *p[4];
int *pp=p[i];
int maxm,maxn,m,n;
int row=yy/10;
int col=xx/10+1;
maxm=abs(*pp);maxn=abs(*(pp+1));
for(m=0;m<maxm;m++)
for(n=0;n<maxn;n++)
if(*(pp+m*4+n)>0)
pump[row+m][col+n]=1;

}
/*------------------------------------------------*/
int scans(void)
{extern int pump[37][18];
int k;
int row,col;
for(row=35;row>=0;row--)
{ k=0;
for(col=1;col<=16;col++)
if(pump[row][col])
k++;
if(k==16) return row;
}
return 0;
}
/*------------------------------------------------*/
void kick(int rownum)
{ extern int pump[37][18];
int row,col;
for(row=rownum;row>0;row--)
for(col=1;col<=16;col++)
pump[row][col]=pump[row-1][col];
}
/*------------------------------------------------*/
void redisplay(void)
{extern int pump[37][18];
int row,col;
clearviewport();
for(row=35;row>0;row--)
for(col=1;col<=16;col++)
if(pump[row][col])
barr((col-1)*10,row*10,(col-1)*10+9, row*10+9);

}
/*------------------------------------------------*/
void outnum(int x,int y,int num)
{char string[8];
itoa(num, string, 10);
settextstyle(0,0,0);
setcolor(10);
outtextxy(x, y, string);
}
/*------------------------------------------------*/
void gameover(void)
{ settextstyle(0,0,2);
setcolor(15);
outtextxy(8,140,"GAME OVER");
settextstyle(0,0,0);
setcolor(15);
outtextxy(43,180,"PRESS P KEY");
outtextxy(43,195,"TO RESTART");
outtextxy(43,210,"PRESS Q KEY");
outtextxy(43,225,"TO QUITE");
}
/*------------------------------------------------*/


上一篇:{应用}C程序设计例解(08) 人气:6604
下一篇:{应用}贪吃蛇源程序 人气:4875
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058