1.实验项目名称
用数组实现反弹球消砖块
2.实验项目功能描述
用数组知识进一步该进反弹球消砖块游戏,实现多个待消砖块的效果
3.项目模块介绍
好的清屏
void HideCuresor()
{
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void gotoxy(int x,int y)
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
数据初始化
void startup()
{
ridus=5;
pos_x=High-1;
pos_y=Width/2;
left=pos_y-ridus;
right=pos_y+ridus;
ball_x=pos_x-1;
ball_y=pos_y;
ball_vx=-1;
ball_vy=1;
canvas[ball_x][ball_y]=1;
int k,i;
for(k=left;k<=right;k++)//挡板
canvas[pos_x][k]=2;
for(k=left;k<=Width;k++)//加几排砖块
for(i=0;i<High/4;i++)
canvas[i][k]=3;
}
显示画面
void show()
{
gotoxy(0,0);//光标重置
int i,j;
for(i=0;i<High;i++)
{
for(j=0;j<Width;j++)
{
if(canvas[i][j]==0)
printf(" ");//输出空格
else if(canvas[i][j]==1)
printf("0");//输出小球0
else if(canvas[i][j]==2)
printf("*");//输出挡板*
else if(canvas[i][j]==3)
printf("#");//输出砖块#
}
printf("|
");//显示右边界
}
for(j=0;j<Width;j++)
printf("-");//显示下边界
printf("
");
}
反弹小球
void updateWithoutInput()
{
if(ball_x==High-2);
{
if((ball_y>=left)&&(ball_y<=right))//被挡板挡住
{
}
else//没有被挡板挡住
{
printf("游戏失败
");
system("pause");
exit(0);
}
}
static int speed=0;
if(speed<7)
speed++;
if(speed==7)
{
speed=0;
canvas[ball_x][ball_y]=0;
//更新小球的坐标
ball_x=ball_x+ball_vx;
ball_y=ball_y+ball_vy;
canvas[ball_x][ball_y]=1;
//碰到边界后反弹
if((ball_x==0)||(ball_x==High-2))
ball_vx=-ball_vx;
if((ball_y==0)||(ball_y==Width-1))
ball_vy=-ball_vy;
//碰到砖块后反弹
if(canvas[ball_x-1][ball_y]=3)
{
ball_vx=-ball_vx;
canvas[ball_x-1][ball_y]=0;
printf("a");
}
}
}
移动挡板反弹
void updateWithInput()
{
char input;
if(kbhit())//判断是否输入
{
input=getch();
if(input=='a'&&left>0)
{
canvas[pos_x][right]=0;
pos_y--;//左移
left=pos_y-ridus;
right=pos_y+ridus;
canvas[pos_x][left]=2;
}
if(input=='d'&&right<Width-1)
{
canvas[pos_x][left]=0;
pos_y++;//右移
left=pos_y-ridus;
right=pos_y+ridus;
canvas[pos_x][right]=2;
}
}
}
4.实现界面显示
5.代码托管链接
https://gitee.com/scfyer/first
6.实验总结
小游戏反弹球消砖块,费了九牛二虎之力打完代码,出现各种错误,以一个个对照书籍查看,dev c++中没有cwindow的头文件,最后改成了windows。最后实现界面就懵逼了。。。(与想象中的不同唉)