一个俄罗斯方块游戏代码,新手求帮忙调试
一个俄罗斯方块游戏代码,graphic文件已经拷贝到include和lib文件夹了,VC下编译问题还是很多,新人求指导....谢谢了
[code=c]
/*加载头文件*/
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h> /*图形函数库*/
/*定义按键码*/
#define VK_LEFT 0x25
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
#define VK_UP 0x26
#define VK_ESC 0x011b
#define TIMER 0x1c /*设置中断号*/
/*定义常量*/
#define MAX_BOX 19 /*总共有19种各形态的方块*/
#define BSIZE 20 /*方块的边长是20个象素*/
#define Sys_x 160 /*显示方块界面的左上角x座标*/
#define Sys_y 25 /*显示方块界面的左上角y座标*/
#define Horizontal_boxs 10 /*水平的方向以方块为单位的长度*/
#define Vertical_boxs 15 /*垂直的方向以方块为单位的长度*/
#define Begin_boxs_x Horizontal_boxs/2 /*产生第一个方块时出现的起始位置*/
#define FgColor 3 /*前景颜色,如文字.2-green*/
#define BgColor 0 /*背景颜色.0-blac*/
#define LeftWin_x Sys_x+Horizontal_boxs*BSIZE+46 /*右边状态栏的x座标*/
#define false 0
#define true 1
/*移动的方向*/
#define MoveLeft 1
#define MoveRight 2
#define MoveDown 3
#define MoveRoll 4
/*以后坐标的每个方块可以看作是像素点是BSIZE*BSIZE的正方形*/
/*定义全局变量*/
int current_box_numb; /*保存当前方块编号*/
int Curbox_x=Sys_x+Begin_boxs_x*BSIZE,Curbox_y=Sys_y; /*x,y是保存方块的当前坐标的*/
int flag_newbox=false; /*是否要产生新方块的标记0*/
int speed=0; /*下落速度*/
int score=0; /*总分*/
int speed_step=30; /*每等级所需要分数*/
int *oldtimer;
void interrupt (void); /* 指向原来时钟中断处理过程入口的中断处理函数指针 */
struct BOARD /*游戏底板结构,表示每个点所具有的属性*/
{
int var; /*当前状态 只有0和1,1表示此点已被占用*/
int color; /*颜色,游戏底板的每个点可以拥有不同的颜色.增强美观*/
}Table_board[Vertical_boxs][Horizontal_boxs];
/*方块结构*/
struct SHAPE
{
char box[2]; /*一个字节等于8位,每4位来表示一个方块的一行
如:box[0]="0x88",box[1]="0xc0"表示的是:
1000
1000
1100
0000*/
int color; /*每个方块的颜色*/
int next; /*下个方块的编号*/
};
/*初始化方块内容.即定义MAX_BOX个SHAPE类型的结构数组,并初始化*/
struct SHAPE shapes[MAX_BOX]=
{
/*
* 口 口口口 口口 口
* 口 口 口 口口口
* 口口 口
*/
{0x88, 0xc0, CYAN, 1},
{0xe8, 0x00, CYAN, 2},
{0xc4, 0x40, CYAN, 3},
{0x2e, 0x00, CYAN, 0},
/*
* 口 口口 口口口
* 口 口 口 口
* 口口 口口口 口
*/
{0x44, 0xc0, MAGENTA, 5},
{0x8e, 0x00, MAGENTA, 6},
{0xc8, 0x80, MAGENTA, 7},
{0xe2, 0x00, MAGENTA, 4},
/*
* 口
* 口口 口口
* 口 口口
*/
{0x8c, 0x40, YELLOW, 9},
{0x6c, 0x00, YELLOW, 8},
/*
* 口 口口
* 口口 口口
* 口
*/
{0x4c, 0x80, BROWN, 11},
{0xc6, 0x00, BROWN, 10},
/*
* 口 口 口
* 口口口 口口 口口口 口口
* 口 口 口
*/
{0x4e, 0x00, WHITE, 13},
{0x8c, 0x80, WHITE, 14},
{0xe4, 0x00, WHITE, 15},
{0x4c, 0x40, WHITE, 12},
/* 口
* 口
* 口 口口口口
* 口
*/
{0x88, 0x88, RED, 17},
{0xf0, 0x00, RED, 16},
/*
* 口口
* 口口
*/
{0xcc, 0x00, BLUE, 18}
};
unsigned int TimerCounter=0; /*定时计数器变量*/
void main()
{
int GameOver=0;
int key,nextbox;
int Currentaction=0;/*标记当前动作状态*/
int gd=VGA,gm=VGAHI,errorcode;
initgraph(&gd,&gm,"");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("\nNotice:Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to quit!");
getch();
exit(1);
}
setbkcolor(BgColor);
setcolor(FgColor);
randomize();
SetTimer(newtimer);
initialize(Sys_x,Sys_y,Horizontal_boxs,Vertical_boxs);/*初始化*/
nextbox=MkNextBox(-1); show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);
show_intro(Sys_x,Curbox_y+320);
getch();
while(1)
{
/* Currentaction=0;
flag_newbox=false;
检测是否有按键*/
if (bioskey(1)){key=bioskey(0); }
else { key=0; }
switch(key)
{
case VK_LEFT:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveLeft))
{EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_x-=BSIZE;Currentaction=MoveLeft;}
break;
case VK_RIGHT:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveRight))
{EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_x+=BSIZE;Currentaction=MoveRight;}
break;
case VK_DOWN:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
{EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_y+=BSIZE;Currentaction=MoveDown;}
else flag_newbox=true;
break;
case VK_UP:/*旋转方块*/
if(MoveAble(Curbox_x,Curbox_y,shapes[current_box_numb].next,MoveRoll))
{EraseBox(Curbox_x,Curbox_y,current_box_numb);current_box_numb=shapes[current_box_numb].next;
Currentaction=MoveRoll;
}
break;
case VK_ESC:
GameOver=1;
break;
default:
break;
}
if(Currentaction)
{ /*表示当前有动作,移动或转动*/
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
Currentaction=0;
}
/*按了往下键,但不能下移,就产生新方块*/
if(flag_newbox)
{
/*这时相当于方块到底部了,把其中出现点满一行的清去,置0*/
ErasePreBox(LeftWin_x,Sys_y+200,nextbox);
nextbox=MkNextBox(nextbox);
show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);
if(!MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))/*刚一开始,游戏结束*/
{
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
GameOver=1;
}
else
{
flag_newbox=false;
}
Currentaction=0;
}
else /*自由下落*/
{
if (Currentaction==MoveDown || TimerCounter> (20-speed*2))
{
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
{
EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_y+=BSIZE;
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
}
TimerCounter=0;
}
}
if(GameOver )/*|| flag_newbox==-1*/
{
printf("game over,thank you! your score is %d",score);
getch();
break;
}
}
getch();
KillTimer();
closegraph();
}
void initialize(int x,int y,int m,int n)
{
int i,j,oldx;
oldx=x;
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
Table_board[j][i].var=0;
Table_board[j][i].color=BgColor;
line(x,y,x+BSIZE,y);
line(x,y,x,y+BSIZE);
line(x,y+BSIZE,x+BSIZE,y+BSIZE);
line(x+BSIZE,y,x+BSIZE,y+BSIZE);
x+=BSIZE;
}
y+=BSIZE;
x=oldx;
}
Curbox_x=x;
Curbox_y=y;/*x,y是保存方块的当前坐标的*/
flag_newbox=false; /*是否要产生新方块的标记0*/
speed=0; /*下落速度*/
score=0; /*总分*/
ShowScore(score);
ShowSpeed(speed);
}
/*分数越高,时间中断的间隔就越短。:
(1) 定义新的时钟中断处理函数void interrupt newtimer(void)。
(2) 使用SetTimer()设置新的时钟中断处理过程。
(3) 定义中断恢复过程KillTimer()。
*/
void interrupt newtimer(void)
{
(*oldtimer());
TimerCounter++;
}
/* 设置新的时钟中断处理过程 */
void SetTimer(void interrupt(*IntProc)(void))
{
oldtimer=getvect(TIMER); /*获取中断号为TIMER的中断处理函数的入口地址*/
disable(); /* 设置新的时钟中断处理过程时,禁止所有中断 */
setvect(TIMER,IntProc);
/*将中断号为TIMER的中断处理函数的入口地址改为IntProc()函数的入口地址
即中断发生时,将调用IntProc()函数。*/
enable(); /* 开启中断 */
}
/* 恢复原有的时钟中断处理过程 */
void KillTimer()
{
disable();
setvect(TIMER,oldtimer);
enable();
}
void ShowScore(int score)
{
int x,y;
char score_str[5];/*保存游戏得分*/
setfillstyle(SOLID_FILL,BgColor);
x=LeftWin_x;
y=100;
bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);
sprintf(score_str,"%3d",score);
outtextxy(x,y,"SCORE");
outtextxy(x,y+10,score_str);
}