C++小游戏—反弹球实现打砖块
0.前言
在上一篇中,我们用C++代码实现了弹球小游戏,上一篇链接可以点击这里查看。
这一篇中,我们继续优化代码,使用上一篇的弹球小游戏进行扩展,实现打砖块效果。
1.思路
底部挡板跟随键盘移动
在顶部生成目标物—砖块
小球在底部挡板中向一个方向移动
小球碰到墙壁反弹,
小球碰到砖块,砖块消失,游戏结束
小球碰到底部除挡板外的位置,游戏结束。
2.游戏框架
#include<cstdio>
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<ctime>
using namespace std;
//全局变量定义
//数据初始化
void start() {
}
//显示画面
void show() {
}
//输入无关
void noInput() {
}
//输入有关
void Keydown() {
}
//隐藏光标
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info={1,0}; //第二个值表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
//屏幕移动函数
void gotoxy(int x, int y) {
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
//主函数
int main() {
start(); //数据初始化
HideCursor(); //隐藏光标
while(1) {
show();
Keydown();
noInput();
}
return 0;
}3.过程
(1)画边框
画边框的原理比较简单,就是当遍历到边界的宽(width)或高(high)时,画出边界。
参考代码:
#include<cstdio>
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<ctime>
using namespace std;
//全局变量定义
//数据初始化
void start() {
}
//显示画面
void show() {
}
//输入无关
void noInput() {
}
//输入有关
void Keydown() {
}
//隐藏光标
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info={1,0}; //第二个值表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
//屏幕移动函数
void gotoxy(int x, int y) {
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
//主函数
int main() {
start(); //数据初始化
HideCursor(); //隐藏光标
while(1) {
show();
Keydown();
noInput();
}
return 0;
}效果:
(2)画挡板
定义挡板的中心坐标、半径、左右点坐标。然后加上键盘输入和移动。
参考代码:
#include<cstdio>
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<ctime>
using namespace std;
//全局变量定义
int high,width;//屏幕高度、屏幕宽度
int ball_x,ball_y; //小球的坐标,
int speedx,speedy; //小球的速度
int pad_x,pad_y; //挡板中心坐标
int r; //挡板的半径大小
int pad_left,pad_right; //挡板的左右边界点
//数据初始化
void start() {
high=15;
width=20;
ball_x=1;
ball_y=width/2;
speedx=1;
speedy=1;
pad_x=high; //挡板中心坐标
pad_y=width/2; //挡板中心坐标
r=2;
pad_left =pad_y - r; //挡板左端点
pad_right =pad_y + r; //挡板右端点
}
//屏幕移动函数
void gotoxy(int x, int y) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
COORD pos = {x,y};
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
//显示画面
void show() {
gotoxy(0,0); //通过gotoxy可以达到清屏效果
/*画小球*/
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>=pad_left) && (j<=pad_right))
printf("*"); //输出挡板
else
printf(" ");//输出空白
}
printf("\n");
}
}
//输入无关
void noInput() {
if(ball_x>=high || ball_x<=0) //上下边界判断
speedx= -1*speedx; //让增量取反
if(ball_y>=width || ball_y<=0) //左右边界判断
speedy= -1*speedy;
ball_x=ball_x+speedx;
ball_y=ball_y+speedy;
}
//输入有关
void Keydown() {
}
//隐藏光标
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info= {1,0}; //第二个值表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
//主函数
int main() {
start(); //数据初始化
HideCursor(); //隐藏光标
while(1) {
//system("cls");
show();
Keydown();
noInput();
}
return 0;
}(3)加入挡板的移动
#include<cstdio>
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<ctime>
using namespace std;
//全局变量定义
int high,width;//屏幕高度、屏幕宽度
int ball_x,ball_y; //小球的坐标,
int speedx,speedy; //小球的速度
int pad_x,pad_y; //挡板中心坐标
int r; //挡板的半径大小
int pad_left,pad_right; //挡板的左右边界点
//数据初始化
void start() {
high=15;
width=20;
ball_x=1;
ball_y=width/2;
speedx=1;
speedy=1;
pad_x=high; //挡板中心坐标
pad_y=width/2; //挡板中心坐标
r=2;
pad_left =pad_y - r; //挡板左端点
pad_right =pad_y + r; //挡板右端点
}
//屏幕移动函数
void gotoxy(int x, int y) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
COORD pos = {x,y};
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
//显示画面
void show() {
gotoxy(0,0); //通过gotoxy可以达到清屏效果
/*画小球*/
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>=pad_left) && (j<=pad_right))
printf("*"); //输出挡板
else
printf(" ");//输出空白
}
printf("\n");
}
}
//输入无关
void noInput() {
if(ball_x>=high || ball_x<=0) //上下边界判断
speedx= -1*speedx; //让增量取反
if(ball_y>=width || ball_y<=0) //左右边界判断
speedy= -1*speedy;
ball_x=ball_x+speedx;
ball_y=ball_y+speedy;
}
//输入有关
void Keydown() {
char input;
if (_kbhit()) { //如果有按键按下,则_kbhit()函数返回真
input = _getch();//使用_getch()函数获取按下的键值
switch(input) {
case 75: //左键
case 'a':
case 'A':
pad_y--;
pad_left=pad_y-r;
pad_right=pad_y+r;
break;
case 77: //右键
case 'd':
case 'D':
pad_y++;
pad_left=pad_y-r;
pad_right=pad_y+r;
break;
}
}
}
//隐藏光标
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info= {1,0}; //第二个值表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
//主函数
int main() {
start(); //数据初始化
HideCursor(); //隐藏光标
while(1) {
//system("cls");
show();
Keydown();
noInput();
}
return 0;
}扫描二维码推送至手机访问。
版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。



