俄罗斯方块编程问题
两个函数:Show_box(int x,int y,int box_numb)
Erase_box(int x,int y,int box_numb)
Show_box(int x,int y,int box_numb)用来显示方块。
Erase_box(int x,int y,int box_numb)用来擦去方块。
按道理讲,根据动画的原理,应该能很好的控制方块。
while(1)
{
......
通过switch语句改变x;
getch()
......
Show_box(int x,int y,int box_numb);
Sleep(300);
y++;
//再循环语句中y++以实现自由下落。
Erase_box(int x,int y,int box_numb);
}
可运行的结果却不正常,显示的方块被立刻擦掉了,没有形成动画。困惑一天,希望高手给点办法。
[解决办法]
网上剽窃的,仅供参考
/*
*俄罗斯方块源程序
*/
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>
#include <bios.h>
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
/*定义按键码*/
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#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 /*垂直的方向以方块为单位的长度,也就说长是15个方块*/
#define Begin_boxs_x Horizontal_boxs/2 /*产生第一个方块时出现的起始位置*/
#define FgColor CYAN //前景色
#define BgColor BLACK //背景颜色
#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; /*每等级所需要分数*/
void interrupt (*oldtimer)(__CPPARGS);/* 指向原来时钟中断处理过程入口的中断处理函数指针 */
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, 0x0, CYAN, 2},
{0xc4, 0x40, CYAN, 3},
{0x2e, 0x0, CYAN, 0},
/*
* 口 口口 口口口
* 口 口 口 口
* 口口 口口口 口
*/
{0x44, 0xc0, MAGENTA, 5},
{0x8e, 0x0, MAGENTA, 6},
{0xc8, 0x80, MAGENTA, 7},
{0xe2, 0x0, MAGENTA, 4},
/*
* 口
* 口口 口口
* 口 口口
*/
{0x8c, 0x40, YELLOW, 9},
{0x6c, 0x0, YELLOW, 8},
/*
* 口 口口
* 口口 口口
* 口
*/
{0x4c, 0x80, BROWN, 11},
{0xc6, 0x0, BROWN, 10},
/*
* 口 口 口
* 口口口 口口 口口口 口口
* 口 口 口
*/
{0x4e, 0x0, WHITE, 13},
{0x8c, 0x80, WHITE, 14},
{0xe4, 0x0, WHITE, 15},
{0x4c, 0x40, WHITE, 12},
/* 口
* 口
* 口 口口口口
* 口
*/
{0x88, 0x88, RED, 17},
{0xf0, 0x0, RED, 16},
/*
* 口口
* 口口
*/
{0xcc, 0x0, BLUE, 18}
};
unsigned int TimerCounter=0; /*定时计数器变量*/
/* 新的时钟中断处理函数 */
void interrupt newtimer(__CPPARGS)
{
(*oldtimer)(); /* call the old routine */
TimerCounter++;/* increase the global counter */
}
//时钟中断处理
void SetTimer(void interrupt(*IntProc)(__CPPARGS))
{
oldtimer=getvect(TIMER); //获取中断号为TIMER的中断处理函数的入口地址
disable(); //关中断,以屏蔽其他中断
setvect(TIMER,IntProc); //设置中断向量,调用中断处理函数
enable(); //开中断,以响应其他中断
}
/* 恢复原有的时钟中断处理过程 */
void KillTimer()
{
disable();
setvect(TIMER,oldtimer);
enable();
}
//显示帮助信息
void show_intro(int xs,int ys)
{
char stemp[50];
setcolor (WHITE); //设置边框为白色
rectangle(xs,ys,xs+239,ys+100);
sprintf(stemp," -Roll -Downwards");
stemp[0]=24; //↑
stemp[8]=25; //↓
setcolor(YELLOW); //设置文本颜色为黄色
outtextxy(xs+40,ys+30,stemp);
sprintf(stemp," -Turn Left -Turn Right");
stemp[0]=27; //←
stemp[13]=26; //→
outtextxy(xs+40,ys+45,stemp);
outtextxy(xs+40,ys+60,"Esc-Exit ");
setcolor(FgColor);
}
//未完待续
[解决办法]
参考下面这个页面切换动画技术?
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <conio.h>
#include <time.h>
#include <math.h>
time_t t;
char timestr1[30];//DDD MMM dd hh:mm:ss YYYY
char timestr2[30];//DDD MMM dd hh:mm:ss YYYY
char hhmmss[9];
int graphdriver,graphmode,page;
int i,xo,yo,r0,r1,r2,r3,r4,r5,r6,r7;
int x1,y1,x2,y2;
int hh,mm,ss;
double c,s;
int xasp,yasp;
void main() {
xo=60;yo=60;
r0=5; //轴
r1=20;//时针
r2=36;//分针
r3=45;//秒针
r4=50;//时刻度
r5=53;//分刻度
r6=55;//刻度外
r7=59;//表盘
graphdriver=VGA;
graphmode=VGAMED;
initgraph(&graphdriver,&graphmode,"D:\\BC\\BGI");
getaspectratio(&xasp, &yasp);
page=0;
timestr2[0]=0;
while (1) {
if (kbhit()) break;
time(&t);
strcpy(timestr1,ctime(&t));
if (strcmp(timestr1,timestr2)) {
strcpy(timestr2,timestr1);
hh=atoi(timestr2+11);
mm=atoi(timestr2+14);
ss=atoi(timestr2+17);
setvisualpage(page);
setactivepage(1-page);
cleardevice();
sprintf(hhmmss,"%02d:%02d:%02d",hh,mm,ss);
outtextxy(28,0,hhmmss);
pieslice(xo,yo,0,360,r0);
circle(xo,yo,r7);
for (i=0;i<60;i++) {
c=cos(i*6*3.14159265/180);
s=sin(i*6*3.14159265/180)*xasp/yasp;
if (0==(i%5)) {
x1=xo+r4*c;
y1=yo+r4*s;
} else {
x1=xo+r5*c;
y1=yo+r5*s;
}
x2=xo+r6*c;
y2=yo+r6*s;
line(x1,y1,x2,y2);
if (((hh%12)*5+mm/12+45)%60==i) {
x2=xo+r1*c;
y2=yo+r1*s;
setlinestyle(0,-1,3);
line(xo,yo,x2,y2);
setlinestyle(0,-1,1);
}
if ((mm+45)%60==i) {
x2=xo+r2*c;
y2=yo+r2*s;
setlinestyle(0,-1,3);
line(xo,yo,x2,y2);
setlinestyle(0,-1,1);
}
if ((ss+45)%60==i) {
x2=xo+r3*c;
y2=yo+r3*s;
line(xo,yo,x2,y2);
}
}
page=1-page;
}
delay(100);
}
getch();
closegraph();
}
[解决办法]
单步调试吧,这种问题不算难。