/********************************************
* 程序名称:MR.DUAN 的打砖块(KILL THE BLOCKS !)
* 作 者:WindAutumn <duanxu@outlook.com>
* 最后修改:2012-8-11-PM
* 版 本 号:1.0
*
*// 好像有个BUG。。。间歇性发作,可能是编译器问题吧,不管了。。。
*
* *****************************************/ #include<stdio.h>
#include<Windows.h>
#include<conio.h> #define PI 3.1415926
#define LEFT 0
#define RIGHT34
#define UP 0
#define DOWN24
#define X_OFFSET 2
#define Y_OFFSET 1
#define BLOCK_MAP 0xffff// 砖块地图 unsigned short block[]= {};
char blocks[][]= {};
int arm_head=+X_OFFSET, arm_tail=+X_OFFSET; void HideCursor(HANDLE hBlock);
void GotoXY(HANDLE hBlock, int x, int y);
void InitScreen(HANDLE hBlock);
void GameStart();
void GameOver(HANDLE hBlock, int mode);
void PrintBlock(HANDLE hBlock);
void ChangeArm(HANDLE hBlock);
void PrintBall(HANDLE hBlock);
void KillBlock(HANDLE hBlock, int x, int y); void main()
{
system("color 7b");// 默认颜色,白色底色
SetConsoleTitle("KILL THE BLOCKS !");// 设置控制台标题
GameStart();// 开始游戏
} void GameStart()
{
int i; HANDLE hBlock = GetStdHandle(STD_OUTPUT_HANDLE);
HideCursor(hBlock);// 隐藏光标
InitScreen(hBlock);// 初始化屏幕
for(i=; i<; i++)
block[i]=BLOCK_MAP;
PrintBlock(hBlock);// 绘制砖块地图 GotoXY(hBlock, arm_head, DOWN - );// 以下3行打印托盘
for(i=; i<(arm_tail-arm_head)/+; i++)
printf("□"); PrintBall(hBlock);// 对小球的控制
} void InitScreen(HANDLE hBlock)// 初始化屏幕
{
int i;
SetConsoleTextAttribute(hBlock, FOREGROUND_INTENSITY | FOREGROUND_BLUE | BACKGROUND_BLUE |BACKGROUND_GREEN | BACKGROUND_RED);
// 白的背景色,亮蓝色前景色
GotoXY(hBlock, , );
printf("╔");
for(i=; i<RIGHT/; i++)
printf("═");
//printf("%2d",i);
printf("╗"); for(i=; i< DOWN; i++)
{
GotoXY(hBlock, , i);
//printf("%d",i);
printf("║");
GotoXY(hBlock, RIGHT, i);
printf("║");
} GotoXY(hBlock, , DOWN);
printf("╚");
for(i=; i<RIGHT/; i++)
printf("═");
printf("╝"); GotoXY(hBlock, , ); } void HideCursor(HANDLE hBlock)// 隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {, };
SetConsoleCursorInfo(hBlock, &cursor_info);
} void GotoXY(HANDLE hBlock, int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(hBlock, coord);
} void PrintBlock(HANDLE hBlock)
{
int i,j;
for(i=; i<; i++)// 打印右侧数字视图
{
for(j=; j<; j++)
{
if(((block[i]<<j)&0x8000) == (unsigned short)0x8000)
blocks[i][j]=;
else blocks[i][j]=;
GotoXY(hBlock,+j,i);
printf("%d",blocks[i][j]);
}
}
for(i=; i<; i++)// 打印砖块
{
for(j=; j<; j++)
{
if(blocks[i][j]==)
{
GotoXY(hBlock,+*j,+i);
printf("□");
}
}
}
} void ChangeArm(HANDLE hBlock)
{
int key_direct=;
if(_kbhit())
{
key_direct = getch();
if(key_direct == 'a'&&arm_head > LEFT+)// 托盘往左走
{
arm_head -= ;
GotoXY(hBlock, arm_head, DOWN-);
printf("□");
GotoXY(hBlock, arm_tail, DOWN-);
printf(" ");
arm_tail -= ;
}
if(key_direct == 'd'&&arm_tail < RIGHT-)// 托盘向右走
{
arm_tail += ;
GotoXY(hBlock, arm_tail, DOWN-);
printf("□");
GotoXY(hBlock, arm_head, DOWN-);
printf(" ");
arm_head += ;
}
}
} void PrintBall(HANDLE hBlock)
{
int k,flag=;
int x = (arm_head+arm_tail)/, y =DOWN - ;// 球的坐标
int tempx = x,tempy = y;
int degree =;// 初始角度 GotoXY(hBlock, x, y);// 初始球坐标
printf("□");
while()
{
if(x == LEFT+ || x == RIGHT-)
degree = (degree<=)?(-degree):(-degree);// 碰左右边的角度计算
if(y == UP+)
degree = - degree;// 碰上边的角度计算
if(y == DOWN-)
{
if(!(x>=arm_head&&x<=arm_tail))// 没有碰上托盘的情况
GameOver(hBlock,);// 失败
else if(degree > )
{
if(x == (arm_head+arm_tail)/)
degree = - degree;// 计算碰托盘之后角度
else
degree = - degree + * ((arm_head+arm_tail)/ - x);
} }
switch(degree)// 根据角度确定方块移动方向
{
case :
case :
case :
tempx = x+;
tempy = y-;
break;
case :
tempx = x+;
tempy = y-;
break;
case :
tempx = x ;
tempy = y-;
break;
case :
tempx = x-;
tempy = y-;
break;
case :
tempx = x-;
tempy = y-;
break;
case :
case :
tempx = x-;
tempy = y+;
break;
case :
tempx = x-;
tempy = y+;
break;
case :
tempx = x ;
tempy = y+;
break;
case :
tempx = x+;
tempy = y+;
break;
case :
tempx = x+;
tempy = y+;
break;
} GotoXY(hBlock, tempx, tempy);// 下一个点在哪?
printf("□");
GotoXY(hBlock, x, y);// 消除上一个点
printf(" ");
x = tempx;
y = tempy; ChangeArm(hBlock);
KillBlock(hBlock,x,y);
for(k=; k<; k++)// 如果每行非0就置flag为1
if(block[k]!=(unsigned short)0x0000)
flag=;
if(flag==)// 如果flag为0则游戏胜利
GameOver(hBlock,);
flag=;
GotoXY(hBlock,,);// 打印坐标,角度信息
printf("%3d,%2d,%2d",degree,x,y);
Sleep();// 暂停0.1s *********************important
}
} void KillBlock(HANDLE hBlock, int x, int y)
{
int i,j;
unsigned short temp;
i=y-;
j=(x-)/;
if(blocks[i][j] == )
{
blocks[i][j] = ;
temp = ~(0x0001<<j);// 掩码
block[i]=block[i] & temp;// 将block信息与( 1111 1110 1111 1111 )进行与运算,消除bit位
GotoXY(hBlock,+j,i);// 刷新右侧数字区
printf("");
//GotoXY(hBlock,50,12+i);// 打印实时每行信息
//printf("%#x",block[j]);
}
} void GameOver(HANDLE hBlock, int mode)
{
GotoXY(hBlock,,);
if(mode)
printf("you win");
else
printf("you loss"); getch();
exit();
}