当前位置:首页 > 趣味小程序 > 正文内容

C++小游戏—反弹球实现打砖块

亿万年的星光5年前 (2021-03-27)趣味小程序19803

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;
}


    扫描二维码推送至手机访问。

    版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。

    分享给朋友:

    相关文章

    【C++图形化编程】EasyX函数~图形绘制相关函数(1)

    简介:EasyX有很多图形绘制函数,这篇文章简单介绍下:函数或函数类型说明arc画椭圆形circle画无填充的圆clearcircle清空圆形区域clearllipse清空椭圆形区域clearpie清...

    C++实现弹窗效果

    C++实现弹窗效果

    1.格式C++实现弹窗效果需要用到messagebox,这个我在C#中用过,C++也有类似用法。messagebox函数,需要引入<windows.h>头文件2.简单用法#include&...

    【二分与分治】中间值、边界值、循环条件、模块写法(2)

    二分法的模板写法:(1)标准的二分查找(寻找的值正好等于x的任意位置)int search(int A[], int n, int targ...

    C++小游戏——flappy bird简单实现

    C++小游戏——flappy bird简单实现

    上一篇小游戏中,我们简单实现了打砖块小游戏。这一篇中,我们根据前面的框架,简单实现flappy bird小游戏。1.游戏框架   2.实现下落的小鸟#include &l...

     【C++图形化编程】飞机大战1—基础资源导入与基本框架

    【C++图形化编程】飞机大战1—基础资源导入与基本框架

    0.前言前面几篇文章中,我们实现了flappy的小游戏,这篇文章中,我们尝试制作飞机大战的游戏。首先,效果图如下:基础资源导入下载图片及音乐资源:https://box356.lanzoui.com/...

    C++小游戏—猜数游戏

    0.游戏内容玩家猜电脑产生的数字,一个两次机会,才对了给提示,猜错减去一次机会。1.参考代码#include<iostream>#include<cstdlib>#includ...