11.23-11.27
一、基础框架
上下跳动的小球
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
//全局变量
int high,width; //游戏画面大小
int ball_x,ball_y; //小球的坐标
int ball_vx,ball_vy; //小球的速度
int position_x,position_y; //挡板的半径大小
int ridus; //挡板的左右位置
int left,right;
void gotoxy(int x,int y) //将光标移动到(x,y)的位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup() //数据的初始化
{
high = 15;
width = 20;
ball_x = 0;
ball_y = width/2;
ball_vx = 1;
ball_vx = 1;
}
void show() //显示画面
{
gotoxy(0,0); //光标移动到原点位置,以下重画清屏
int i,j;
for ( i=0;i<=high;i++)
{
for ( j=0;j<=width;j++)
{
if ( (i==ball_x) && (j==ball_y) )
printf("o"); //输出小球
else
printf(" "); //输出空格
}
printf("\n");
}
}
void updateWithoutInput() //与用户输入无关的更新
{
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if( (ball_x == 0) || (ball_x == high-2) )
ball_vx = -ball_vx;
if( (ball_y == 0) || (ball_y == width-1) )
ball_vy = -ball_vy;
Sleep(50);
}
void updateWithInput() //与用户输入有关的更新
{
}
int main()
{
startup(); //数据的初始化
while(1) //游戏循环的执行
{
show(); //显示画面
updateWithoutInput(); //与用户输入无关的更新
updateWithInput(); //与用户输入有关的更新
}
return 0;
}
二、显示边框
在show中函数中加入边框
右边界显示“|”字符,下边界显示“-”字符
void show() //显示画面
{
gotoxy(0,0); //光标移动到原点位置,以下重画清屏
int i,j;
for ( i=0;i<high;i++)
{
for ( j=0;j<width;j++)
{
if ( (i==ball_x) && (j==ball_y) )
printf("o"); //输出小球
else if (j == width)
printf("|"); //输出右边框
else if ( i== high)
printf("_"); //输出下边框
else
printf(" "); //输出空格
}
printf("\n");
}
}
三、移动的挡板
显示中心坐标为(position_x,position_y),半径为ridus的挡板,即在画面的最下一行、left-right范围内显示字符‘*’。通过a,d键实现挡板的左右移动。
加入全局变量
int position_x,position_y; //挡板的中心坐标
int ridus; //挡板的半径大小
int left,right; //挡板的左右位置
并加以初始化
ridus = 5;
position_x = high;
position_y = width/2;
left = position_y-ridus;
right = position_y+ridus;
加入输出挡板
void show() //显示画面
{
gotoxy(0,0); //光标移动到原点位置,以下重画清屏
int i,j;
for ( i=0;i<=high;i++)
{
for ( j=0;j<=width;j++)
{
if ( (i==ball_x) && (j==ball_y) )
printf("o"); //输出小球
else if (j == width)
printf("|"); //输出右边框
else if ( i == high)
printf("-"); //输出下边框
else if ( (i==high-1) && (j>=left) && (j<=right) )
printf("*"); //输出挡板
else
printf(" "); //输出空格
}
printf("\n");
}
}
补充与用户有关的更新
void updateWithInput() //与用户输入有关的更新
{
char input;
if(kbhit()) //根据用户的不同输入来移动,不必输入回车
{
input = getch();
if (input == 'a')
{
position_y--; //位置左移
left = position_y-ridus;
right = position_y+ridus;
}
if (input == 'd')
{
position_y++; //位置右移
left = position_y-ridus;
right = position_y+ridus;
}
}
}
三、记录反弹的小球及消砖块
通过小球的坐标ball_y是否在挡板的left—right范围内,判断小球是否被挡板接到,如果是则反弹小球;如果不是则结束游戏。另外使用变量ball_number记录反弹的次数,并显示输出。
增加砖块字符‘B’,如果小球击中砖块则得分score++。
加入的全局变量
int ball_number; //反弹小球的次数
int block_x,block_y; //砖块的位置
int score; //消掉的砖块的个数
初始化
ball_number = 0;
block_x=0;
block_y=width/2+1;
score=0;
加入输出的砖块
void show() //显示画面
{
gotoxy(0,0); //光标移动到原点位置,以下重画清屏
int i,j;
for ( i=0;i<=high;i++)
{
for ( j=0;j<=width;j++)
{
if ( (i==ball_x) && (j==ball_y) )
printf("o"); //输出小球
else if (j == width)
printf("|"); //输出右边框
else if ( i == high)
printf("-"); //输出下边框
else if ( (i==high-1) && (j>=left) && (j<=right) )
printf("*"); //输出挡板
else if( (i==block_x) && (j==block_y) )
printf("B"); //输出砖块
else
printf(" "); //输出空格
}
printf("\n");
}
printf("反弹的小球数:%d\n",ball_number);
printf("消掉的砖块数:%d\n",score);
}
加入后台机制
void updateWithoutInput() //与用户输入无关的更新
{
if (ball_x == high-2)
{
if( (ball_y>=left) && (ball_y<=right) )
{
ball_number++;
printf("\a"); //响铃
}
else //没有挡板被挡住
{
printf("游戏失败\n");
system("pause");
exit(0);
}
}
if( (ball_x==block_x) && (ball_y==block_y) )
{
score++; //分数加1
block_y=rand()%width; //产生新的砖块
}
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if( (ball_x == 0) || (ball_x == high-2) )
ball_vx = -ball_vx;
if( (ball_y == 0) || (ball_y == width-1) )
ball_vy = -ball_vy;
Sleep(80);
}
四、总结
成品
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
//全局变量
int high,width; //游戏画面大小
int ball_x,ball_y; //小球的坐标
int ball_vx,ball_vy; //小球的速度
int position_x,position_y; //挡板的中心坐标
int ridus; //挡板的半径大小
int left,right; //挡板的左右位置
int ball_number; //反弹小球的次数
int block_x,block_y; //砖块的位置
int score; //消掉的砖块的个数
void gotoxy(int x,int y) //将光标移动到(x,y)的位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup() //数据的初始化
{
high = 15;
width = 20;
ball_x = 0;
ball_y = width/2;
ball_vx = 1;
ball_vx = 1;
ridus = 5;
position_x = high;
position_y = width/2;
left = position_y-ridus;
right = position_y+ridus;
ball_number = 0;
block_x=0;
block_y=width/2+1;
score=0;
}
void show() //显示画面
{
gotoxy(0,0); //光标移动到原点位置,以下重画清屏
int i,j;
for ( i=0;i<=high;i++)
{
for ( j=0;j<=width;j++)
{
if ( (i==ball_x) && (j==ball_y) )
printf("o"); //输出小球
else if (j == width)
printf("|"); //输出右边框
else if ( i == high)
printf("-"); //输出下边框
else if ( (i==high-1) && (j>=left) && (j<=right) )
printf("*"); //输出挡板
else if( (i==block_x) && (j==block_y) )
printf("B"); //输出砖块
else
printf(" "); //输出空格
}
printf("\n");
}
printf("反弹的小球数:%d\n",ball_number);
printf("消掉的砖块数:%d\n",score);
}
void updateWithoutInput() //与用户输入无关的更新
{
if (ball_x == high-2)
{
if( (ball_y>=left) && (ball_y<=right) )
{
ball_number++;
printf("\a"); //响铃
}
else //没有挡板被挡住
{
printf("游戏失败\n");
system("pause");
exit(0);
}
}
if( (ball_x==block_x) && (ball_y==block_y) )
{
score++; //分数加1
block_y=rand()%width; //产生新的砖块
}
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if( (ball_x == 0) || (ball_x == high-2) )
ball_vx = -ball_vx;
if( (ball_y == 0) || (ball_y == width-1) )
ball_vy = -ball_vy;
Sleep(80);
}
void updateWithInput() //与用户输入有关的更新
{
char input;
if(kbhit()) //判断是否有输入
{
input = getch(); //根据用户的不同输入来移动,不必输入回车
if (input == 'a')
{
position_y--; //位置左移
left = position_y-ridus;
right = position_y+ridus;
}
if (input == 'd')
{
position_y++; //位置右移
left = position_y-ridus;
right = position_y+ridus;
}
}
}
int main()
{
startup(); //数据的初始化
while(1) //游戏循环的执行
{
show(); //显示画面
updateWithoutInput(); //与用户输入无关的更新
updateWithInput(); //与用户输入有关的更新
}
return 0;
}