当前位置: 代码迷 >> C语言 >> 问题需要解决!!~~~~~~~~~!!
  详细解决方案

问题需要解决!!~~~~~~~~~!!

热度:527   发布时间:2005-12-28 14:22:00.0
问题需要解决!!~~~~~~~~~!!

看看我的俄罗斯方块,有很多需要修改的地方,尤其是方向很死键,不好使,是什么原因啊?是程序问题还是机器问题啊


----------------解决方案--------------------------------------------------------

这是俄罗斯方块程序
#include "stdlib.h"
#include "stdio.h"
#include "dos.h"
#include "bios.h"
#include "graphics.h"
#include "math.h"
#include "time.h"
#include "ctype.h"
#define DELAY for(count=0;count<=interval;count++){delay(count);}
#define FX x0=nowblock.shapex[i]+px
#define FY y0=nowblock.shapey[i]+py
#define INTERVAL 300
#define ESC 0x11b
#define ENTER 0x1c0d
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define p 0x1970
#define P 0x1950
#define w 0x1177
#define W 0x1157
#define a 0x1e61
#define A 0x1e41
#define s 0x1f73
#define S 0x1f53
#define d 0x2064
#define D 0x2044
#define x 0x2d78
#define X 0x2d58
#define r 0x1372
#define R 0x1352
#define y 0x1579
#define Y 0x1559
#define n 0x316e
#define N 0x314e

/*********方块结构***********/
struct block
{
int shapex[4],shapey[4];
int color,next;
};
/**********定义全局变量************/
int i,j;
int s0,t0;
int x0,y0;
int i0,j0;
int px,py,dx,dy;
int score,level;
int key,count,num;
int interval,mode; /*间隔 、方式*/
int grid[12][21]; /*网格*/
time_t start,finish;
struct tm *ptr;
char *c;
struct block nowblock,nextblock; /*方块、新的方块、下一个方块*/
/******方块信息*************************/
struct block shape[19]={
{0,-1, 1, 2, 0, 0, 0, 0, 3, 1},
{0, 0, 0, 0, 0,-1, 1, 2, 3, 0},
{0,-1, 0, 1, 0, 0,-1, 0, 4, 3},
{0, 0, 0, 1, 0, 1,-1, 0, 4, 4},
{0,-1, 1, 0, 0, 0, 0, 1, 4, 5},
{0, 0, 0,-1, 0, 1,-1, 0, 4, 2},
{0, 0, 0, 1, 0, 1,-1, 1, 5, 7},
{0,-1, 1,-1, 0, 0, 0, 1, 5, 8},
{0, 0, 0,-1, 0, 1,-1,-1, 5, 9},
{0,-1, 1, 1, 0, 0, 0,-1, 5, 6},
{0, 0, 0,-1, 0, 1,-1, 1, 9,11},
{0,-1, 1,-1, 0, 0, 0,-1, 9,12},
{0, 0, 0, 1, 0, 1,-1,-1, 9,13},
{0,-1, 1, 1, 0, 0, 0, 1, 9,10},
{0,-1, 0, 1, 0, 0, 1, 1,11,15},
{0, 0, 1, 1, 0, 1, 0,-1,11,14},
{0, 0,-1, 1, 0, 1, 1, 0,12,17},
{0, 0, 1, 1, 0,-1, 0, 1,12,16},
{0, 0, 1, 1, 0, 1, 0, 1,13,18}
};
/***********初始化**************************/
init()
{
for(i=0;i<=20;i++)
{
grid[0][i]=2;
grid[11][i]=2;
}
for(i=0;i<=11;i++)
grid[i][20]=2;
for(i=0;i<20;i++)

for(j=1;j<11;j++)
grid[j][i]=0;
px=5,py=-1,dx=0,dy=0;
score=0,level=1,mode=1;
interval=INTERVAL,num=0;
}
/************欢迎界面************************/
welcome()
{
settextstyle(4,0,7); /*字型,方向,字符大小*/
setcolor(4);
outtextxy(172,154,"RUSSIAN");
DELAY;
setcolor(4);
outtextxy(171,152,"RUSSIAN");
sleep(1);
setcolor(10);
outtextxy(170,150,"RUSSIAN");
settextstyle(0,0,1);
outtextxy(290,230,"loading...");
setcolor(14);
setfillstyle(1,15);
bar(199,240,441,250);
setcolor(2);
for(i=0;i<231;i+=10)
{
setfillstyle(1,1);
bar(201+i,241,209+i,249);
for(j=1;j<=27;j++)
delay(j*77);
}
}
/************显示选择模式*******************/
timelimited(int s0,int t0)
{
setfillstyle(1,1);
bar(220,190,420,280);
setcolor(14);
settextstyle(0,0,1);
outtextxy(227,203,"please choose mode...");
setfillstyle(1,s0);
bar(250,225,395,243);
setcolor(0);
outtextxy(257,230,"no time limited");
setfillstyle(1,t0);
bar(250,255,395,273);
setcolor(0);
outtextxy(257,260,"time limited(60s)");
}
/**********选择模式*************************/
choosemode()
{
timelimited(15,1);
while(1)
{
key=0;
while(bioskey(1)==0);
key=bioskey(0);
if(key==UP)
{
timelimited(15,1);
mode=1;
}
if(key==DOWN)
{
timelimited(1,15);
mode=2;
}
if(key==ENTER)
{
cleardevice();
break;
}
}
}
/*********等待进入**************************/
waiting()
{
setfillstyle(1,1);
bar(0,465,640,480);
outtextxy(5,469,"press any key to continue...");
getch();
setfillstyle(0,0);
bar(0,465,640,480);
}
/****************显示上次情况***************/
showlastscore()
{FILE *fp;
char lastname[20];
char lastscore[3];
if((fp=fopen("name","r"))!=NULL)
{
fgets(lastname,20,fp);
fclose(fp);
outtextxy(40,100,lastname);
}
if((fp=fopen("highscore","r"))!=NULL)
{
fgets(lastscore,3,fp);
fclose(fp);
outtextxy(40,120,lastscore);
}
setcolor(14);
outtextxy(40,70,"Last time:");
}
/*********游戏主界面**********************/
showface()
{
setcolor(14);
setlinestyle(0,0,1);
rectangle(157,37,363,443);
setlinestyle(0,0,1);
rectangle(160,40,360,440);
rectangle(439,39,541,141);
circle(490,230,50);
rectangle(439,319,541,441);
line(440,340,540,340);
settextstyle(0,0,1);
setcolor(14);
outtextxy(460,330,"--HELP--");
setcolor(10);
outtextxy(447,350,"ESC:EXIT");
outtextxy(447,360,"X:SPEED UP");
outtextxy(447,370,"A:LEFT");
outtextxy(447,380,"D:RIGHT");
outtextxy(447,390,"W:CHANGE");
outtextxy(447,400,"S&P:PAUSE");
outtextxy(447,410,"R:RESTART");
outtextxy(447,420,"Y:YES");
outtextxy(447,430,"N:NO");
outtextxy(460,220,"SCORE 0");
outtextxy(460,240,"LEVEL 1");
setcolor(14);
outtextxy(40,365,"Copyright:");
setcolor(10);
outtextxy(40,385,"wei7ge");
outtextxy(40,395,"wei7ge@163.com");
outtextxy(40,405,"05.8.8");
showlastscore();
}
/*************产生方块****************************/
struct block createblock()
{
struct block newblock=shape[random(19)];
return newblock;
}
/***********检查边界*******************************/
checkborder()
{
for(i=0;i<=3;i++)
{
FX+dx,FY+dy;
if(y0>=0)
if(grid[x0][y0]==2)
return 0;
if(x0<1||x0>10)
return 0;
}
return 1;
}
/*********画下一块**********************************/
drawnext()
{
setfillstyle(0,0);
bar(440,40,540,140);
setfillstyle(1,nextblock.color);
for(i=0;i<=3;i++)
{
x0=nextblock.shapex[i]+2;
y0=nextblock.shapey[i]+2;
i0=x0*20+440;
j0=y0*20+40;
bar(i0+1,j0+1,i0+19,j0+19);
}
}
/********判断消行*****************************/
deline()
{
for(i=19;i>=0;i--)
{
for(j=1;j<=10;j++)
{
if(grid[j][i]!=2)
{break;}
else num++;
}
if(num==10)
{
j=0;
while(j<2)
{
for(t0=1;t0<=10;t0++)
grid[t0][i]=0;
renovate();
hao();
DELAY;
sound(500);
DELAY;
nosound();
for(t0=1;t0<=10;t0++)
grid[t0][i]=2;
renovate();
setfillstyle(0,0);
bar(40,210,95,270);
DELAY;
j++;
}
for(s0=i;s0>0;s0--)
for(t0=1;t0<=10;t0++)
grid[t0][s0]=grid[t0][s0-1];
i++;renovate();
if(score==900) score=0;
else score+=10;
level=floor(score/100)+1;
}
num=0;
}
}
/*******显示分数*******************************/
showscore()
{
char cscore[3],clevel[1];
sprintf(cscore,"%d",score);
sprintf(clevel,"%d",level);
setfillstyle(0,0);
bar(500,220,520,250);
outtextxy(507,220,cscore);
outtextxy(507,240,clevel);
}
/********方块旋转***********************/
struct block changeblock()
{
struct block newblock=nowblock;
nowblock=shape[nowblock.next];
if(!checkborder())
{
nowblock=newblock;
}
return nowblock;
}
/**************移动一格*********************/
drawblock()
{
for(i=0;i<=3;i++)
{
FX,FY;
if(y0>=0)
grid[x0][y0]=1;
}
renovate();
DELAY;
for(i=0;i<=3;i++)
{
FX,FY;
if(y0>=0)
grid[x0][y0]=0;
}
}
/**********刷新******************************/
renovate()
{
for(t0=0;t0<20;t0++)
for(s0=1;s0<11;s0++)
{
if(grid[s0][t0]==1)
setfillstyle(1,nowblock.color);
if(grid[s0][t0]==2)
setfillstyle(1,2);
if(grid[s0][t0]==0)
setfillstyle(0,15);
bar(s0*20+141,t0*20+41,s0*20+159,t0*20+59);
}
}
/********显示:好*******************************/
hao()
{
setcolor(14);
line(60,210,40,240);
line(40,240,60,270);
line(40,215,70,215);
line(60,215,40,270);
line(70,210,90,210);
line(90,210,80,235);
line(80,235,80,270);
line(80,270,75,268);
line(65,235,93,235);
}
/***********显示时间**************************/
showtime(time_t temp)
{
setfillstyle(0,0);
bar(160,455,360,465);
ptr=localtime(&temp);
c=asctime(ptr);
setcolor(2);
if(temp==start)
outtextxy(160,20,c);
else
outtextxy(160,455,c);
}
/*********游戏结束*****************************/
gameover()
{
for(i=19;i>=0;i--)
{
for(j=1;j<11;j++)
grid[j][i]=2;
renovate();
delay(7777);
}
settextstyle(0,0,5);
setcolor(4);
outtextxy(102,152,"GAME OVER");
DELAY;
setcolor(10);
outtextxy(100,150,"GAME OVER");
DELAY;
setcolor(4);
outtextxy(98,148,"GAME OVER");
setcolor(14);
settextstyle(0,0,2);
outtextxy(212,242,"R--Again!");
outtextxy(222,282,"ESC--Exit!");
DELAY;
setcolor(5);
outtextxy(210,240,"R--Again!");
outtextxy(224,284,"ESC--Exit!");
}
/*****************输入姓名*********************/
entername()
{
FILE *fp;
char ch;
if((fp=fopen("name","w"))!=NULL)
{
printf("\n\n\n\n\n\n Please enter your name...");
while((ch=fgetchar())!='\n')
fputc(ch,fp);
fclose(fp);
}
if((fp=fopen("highscore","w"))!=NULL)
{
fprintf(fp,"%d",score);
fclose(fp);
}
if((fp=fopen("name","r"))!=NULL)
{
clrscr();
printf("\n\n\n\n\n\n\n");
printf(" ");
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}
printf("\n\n Your score is %d.",score);
printf("\n\n You are the best! well done!");
sleep(3);
return;
}
/*********主函数****************************/
main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:tc//");
welcome();
loop:
cleardevice();
init();
randomize();
choosemode();
showface();
waiting();
time(&start);
showtime(start);
nextblock=createblock();
do
{
nowblock=nextblock;
nextblock=createblock();
drawnext();
while(checkborder())
{
renovate();
drawblock();
while(1)
{
if(bioskey(1)!=0)
key=bioskey(0);
else break;
switch(key)
{
case DOWN:
case X:
case x:{interval=0;break;}
case UP:
case W:
case w:{nowblock=changeblock();break;}
case LEFT:
case A:
case a:{dx=-1;if(!checkborder()) dx=0;break;}
case RIGHT:
case D:
case d:{dx=1;if(!checkborder()) dx=0;break;}
case ESC:{
setfillstyle(1,1);
bar(0,465,640,480);
outtextxy(5,469,"Are you sure to exit(Y\\N)...");
while(1)
{
key=bioskey(0);
if(key==y||key==Y)
{
closegraph();
entername();
exit(0);
}
if(key==n||key==N)
{
setfillstyle(0,0);
bar(0,465,640,480);
break;
}
}
}
case p:
case P:
case s:
case S:{waiting();break;}
case r:
case R:{
setfillstyle(1,1);
bar(0,465,640,480);
outtextxy(5,469,"Are you sure to restart(Y\\N)...");
while(1)
{
key=bioskey(0);
if(key==y||key==Y)
{
setfillstyle(0,0);
bar(0,465,640,480);
goto loop;
}
if(key==n||key==N)
{
setfillstyle(0,0);
bar(0,465,640,480);
break;
}
}
}
}
}
if(dx==0) dy=1;
else dy=0;
px+=dx,py+=dy;
dx=0,dy=0;
}
for(i=0;i<=3;i++)
{
FX,FY;
grid[x0][y0-1]=2;
}
renovate();
interval=INTERVAL-level*20;
deline();
showscore();
px=5,py=-1;
time(&finish);
showtime(finish);
if(mode==2)
if(difftime(finish,start)>=60)
break;
}while(checkborder());
gameover();
while(1)
{
key=bioskey(0);
if(key==r||key==R)
goto loop;
if(key==ESC)
{
cleardevice();
closegraph();
entername();
exit(0);
}
}
}


----------------解决方案--------------------------------------------------------
我想应该是代码不够长吧?你再往长里写写贝!
----------------解决方案--------------------------------------------------------

高手!看了半天没看懂!

主要是对光标的控制不熟悉!


----------------解决方案--------------------------------------------------------
  相关解决方案