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

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

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

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++ 实用趣味小程序(1)

1.仿动画效果#include <cstdio>#include <windows.h>  //内含延时函数 #include <process.h> /...

C++在指定位置输出符号

C++在指定位置输出符号

0.前言需要做控制台小游戏,需要获取光标在控制台的位置,从网上查到了gotoxy()函数可以实现,但是在DEVC++环境中直接调用这个gotoxy()会报错,于是查了一些资料,把过程记录下来。1.过程...

【C++图形化编程】EasyX函数~图像操作相关函数

【C++图形化编程】EasyX函数~图像操作相关函数

图像处理相关函数函数或数据类型描述IMAGE保存图像的对象loadimage读取图片文件saveimage保存绘图内容至图片文件getimage从当前绘图设备种获取图像putimage在当前绘图设备上...

C++小游戏—贪吃蛇(1)

0.前言c++小游戏来到了第二个,第二个小游戏是贪吃蛇。首先来分析一下需求。我们需要一个函数专门来绘制地图的。在地图上随机生成“食物”。按键函数,用来监听键盘事件。蛇的状态函数。移动函数等。1.参考代...

EasyX—制作复杂动画效果

这篇文章来简单学习一下复杂动画效果。本文资源下载地址:这里参考代码#include <graphics.h> #include <conio.h> in...

【C++图形化编程】EasyX实现弹跳小球

【C++图形化编程】EasyX实现弹跳小球

前面的文章实现了C++控制台显示一个弹跳的小球,这篇文章使用EasyX实现一个带有界面的弹跳小球的效果。首位,我们需要准备好EasyX。然后让EasyX画一个小的圆。然后我们使用前面学过的判断边界的函...