C与C++游戏项目练习4:弹跳的小球2.0
宝书镇楼
2.2.6课后思考题全部实现
采用了砖块数组随机生成指定数目的砖块,并且还加了个能让挡板在水平和竖直两种模式反弹小球的功能。
有两点感悟很深
第一是一定要搞清楚每个变量干什么的,写到UpdateWithOutInput的时候我把小球的坐标、砖块的坐标和当班的坐标各自代表的变量记混了,自己还浑然不觉,结果导致代码跑得莫名其妙遍地开花···············
第二就是像上限和下限(比如小球的竖直运动范围是【1,height-1](因为0和height处画了边框),这类范围是要自己在编代码的过程中一点点慢慢实验修改出来的,这些小细节一定要有足够的耐心去打磨。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define N 5//砖块个数
int ball_x, ball_y;//小球的横纵坐标
int width, height;
int ball_vx, ball_vy;//小球的速度
int position_x, position_y;
int ridus;//半径
int left, right;
int top, bottom;
int ball_number;//得分
int score;//击中砖块数
bool IsVertical;//挡板是否竖直
struct Block//定义砖块数组
{
int block_x;
int block_y;
}block[N];
void gotoxy(int x, int y)//移动光标的函数
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void Start()//初始化函数
{
width = 20;
height = 15;
ball_x = 1;
ball_y = width / 2;
ball_vx = 1;
ball_vy = 1;
position_x = height - 1;//最下一排有边框
position_y = width / 2;
ridus = 5;
left = position_y - ridus;
right = position_y + ridus;
top = position_x - ridus;
bottom = position_x + ridus;
ball_number = 0;
score = 0;
IsVertical = false;//默认挡板是平的
for (int i = 0; i < N;i++)//在游戏边框内初始化砖块数组里面每个砖块的坐标
{
block[i].block_x = rand() % (height - 1) + 1;
block[i].block_y = 1 + rand() % (width - 1);
}
}
void Show()
{
system("cls");
gotoxy(0, 0);
for (int i = 0; i <= height; i++)
{
for (int j = 0; j <= width; j++)
{
if (i == ball_x && j == ball_y)
{
printf("o");
}
else if (i == height || i == 0)
{
printf("-");
}
else if (j == width || j == 0)
{
printf("|");
}
else if (j >= left && j <= right && i == position_x && IsVertical == false)//输出横着的挡板
{
printf("*");
}
else if (i >= top && i <= bottom && j == position_y && IsVertical == true)//输出竖着的挡板
{
printf("*");
}
else//就只剩下要么是空格要么是砖块的情况
{
int flag = 0;//判定i,j这一个单元是不是砖块
for (int k = 0; k < N; k++)
{
if (block[k].block_x == i && block[k].block_y == j)//i,j是砖块数组里面某个元素的坐标,就打印砖块
{ //防止砖块把必要元素取代,把打印砖块的条件放最后(比如说随机生成碰巧和挡板一个坐标,应该优先打印挡板而不是砖块,if判断到打印了挡板就不会判断其他条件了)
printf("B");//这个位置是砖块,就把砖块打印出来
flag = 1;
break;
}
}
if (!flag)
{
printf(" ");//砖块数组遍历完了发现这个位置不是砖块,就空格占位
}
}
}
printf("\n");
}
printf("反弹小球数: %d\n", ball_number);
printf("消除砖块数:%d\n", score);
//隐藏光标
CONSOLE_CURSOR_INFO cursor_info = { 1.0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void UpdateWithOutInput()
{
if (ball_x == height - 1 && (!(ball_y >= left && ball_y <= right && ball_x == position_x&&IsVertical==false)))//小球落到底部并且水平情况下没有被挡板挡住
{
printf("游戏结束!");
system("pause");
exit(0);
}
if (IsVertical==false)//挡板属于水平模式下,小球和挡板位于同一高度判定撞击
{
if (ball_y >= left && ball_y <= right)
{
if (ball_x == position_x)//即小球撞到了挡板
{
ball_number++;//得分加一
printf("\a");//响铃
}
}
}
else//IsVertical=ture,挡板属于竖直模式下,小球和挡板位于同一宽度判定撞击
{
if (ball_x >= top && ball_x <= bottom)
{
if (ball_y == position_y)//
{
ball_number++;//得分加一
printf("\a");//响铃
}
}
//没挡住不会导致游戏结束,小球就被弹回。只有落地才是游戏介绍
}
for (int k = 0; k < N; k++)
{
if (ball_x == block[k].block_x && ball_y == block[k].block_y)//击中砖块数组中的一个成员
{
score++;
block[k].block_x = rand() % (height - 1) + 1;
block[k].block_y = rand() % (width - 1) + 1;//避开边框,重新生成个砖块
}
}
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if (ball_x == 1 || (IsVertical == false && ball_y>=left&&ball_y<=right&&ball_x== position_x))//碰到上边界或者碰到水平挡板被弹回,小球反向
{
ball_vx = -1 * ball_vx;
}
if (ball_y == 1 || ball_y == width-1 || (IsVertical == true && ball_x<=bottom&&ball_x>=top&&ball_y== position_y))//碰到边界或者碰到竖直挡板被弹回,小球反向
{
ball_vy = -1 * ball_vy;
}
Sleep(80);
}
void UpdateWithInput()
{
char input;
if (_kbhit())
{
input = _getch();
if (input == 'a')
{
if (position_y >= 1 )//注意限制范围防止越界
{
position_y--;//移动圆心
left = position_y - ridus;//左右边界随着圆心移动
right = position_y + ridus;
}
}
else if (input == 'd')
{
if(position_y <= width - 1)
{
position_y++;
left = position_y - ridus;
right = position_y + ridus;
}
}
else if (input == 'w')
{
if (position_x >= 1)
{
position_x--;
}
}
else if (input == 's')
{
if (position_x <= height - 1)
{
position_x++;
}
}
else if (input == ' ')
{
IsVertical = !IsVertical;//竖直/水平模式之间切换
}
}
}
int main()
{
Start();
while (1)
{
Show();
UpdateWithOutInput();
UpdateWithInput();
}
return 0;
}
运行效果: