反弹球消砖块游戏

反弹球消砖块,是一款很简单的打砖块游戏,控制你的挡板挡住弹球,打掉上面的砖块,本片博客中,主要使用printf与scanf函数实现消砖块游戏

整体思路

主函数

int main()
{
	startup();//初始化
	while (1)
	{
		show();//显示画面  
		updateWitoutIput();//与用户输入无关的更新  //更新数据
		updateWithInput(); //与用户输入有关的更新  //输入
	}
	return 0;
}

辅助函数

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 HideCursor()	//隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

全局变量的定义

#define HIGH 20  //游戏界面高度
#define WIDTH 30  // 游戏界面宽度
#define V 1  //小球的速度
#define RIDUS 5  //挡板的半径

int ball_x, ball_y;  //小球的坐标
int ball_vx, ball_vy;  //小球的速度
int position_x, position_y;  //挡板的中心位置
int left, right;  //挡板的左右位置
int ball_number;  //反弹小球的次数
int block_x, block_y;  //砖块的坐标
int score;  //消除砖块的个数

数据的初始化

//初始化小球
ball_x = HIGH / 2; //高度
ball_y = WIDTH / 2; //宽度
ball_vx = V;  //小球的纵向速度
ball_vy = V;  //小球的横向速度

//初始化挡板
position_x = HIGH / 2;
position_y = WIDTH / 2;  //挡板的中心位置
left = position_y - RIDUS;//挡板的左位置
right = position_y + RIDUS;  //挡板的右位置

//初始化砖块
block_x = 0; //高度
block_y = WIDTH / 2;  //宽度
score = 0;

显示画面

循环不断输出游戏的界面, 0 表示小球, B 表示砖块, * 表示挡板

for (i = 0; i < HIGH + 1; i++) //行
{
	for (j = 0; j < WIDTH; j++) //列
	{
		if (i == ball_x && j == ball_y)//输出小球
		{
			printf("0");
		}
		else if (i == block_x && j == block_y)//输出砖块
		{
			printf("B");
		}
		else if (i == HIGH)  //下边界 -1,否则循环走不到high和width
		{
			printf("-");
		}
		else if (j == WIDTH - 1) //右边界  
		{
			printf("|");
		}
		else if (i == HIGH - 1 && j >= left && j <= right)  //输出挡板
		{
			printf("*");
		}
		else
		{
			printf(" ");
		}
	}
}
printf("反弹次数:%d\n", ball_number);
printf("消除砖块的个数:%d\n", score);

与用户无关的更新

判断游戏胜负
if (ball_x == HIGH - 2) //到达挡板上方
{
	if (ball_y >= left && ball_y <= right) //被挡板挡住
	{
		ball_number++;
	}
	else  //没被挡板挡住
	{
		printf("游戏失败!\n");
		exit(0);  //直接结束
	}
}
消除砖块得分
if (ball_x == block_x && ball_y == block_y)
{
	score++;
	block_y = rand() % WIDTH;
}
改变小球的位置
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
//为了使小球在反弹和结束时能够真缺判断,所以小球下反弹边界应该在挡板的前一行,小球右反弹边界应该在右边界的前一列
if (ball_x >= HIGH - 2 || ball_x <= 0)  //遇到任意一个边界即发生变化  
	ball_vx = -ball_vx;
if (ball_y >= WIDTH -2 || ball_y <= 0)  //-1保证下球再使小球遇到边界时反弹但不会消失
	ball_vy = -ball_vy;

与用户输入有关的更新

input = _getch();
if (input == 'a')
	position_y--;
if (input == 'd')
	position_y++;
left = position_y - RIDUS;//挡板的左位置
right = position_y + RIDUS;  //挡板的右位置

完整代码

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

//全局变量的定义
#define HIGH 20  //游戏界面高度
#define WIDTH 30  // 游戏界面宽度
#define V 1  //小球的速度
#define RIDUS 5  //挡板的半径

int ball_x, ball_y;  //小球的坐标
int ball_vx, ball_vy;  //小球的速度
int position_x, position_y;  //挡板的中心位置
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 HideCursor()//隐藏光标函数
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()//数据的初始化
{
	//初始化小球
	ball_x = HIGH / 2; //高度
	ball_y = WIDTH / 2; //宽度
	ball_vx = V;  //小球的纵向速度
	ball_vy = V;  //小球的横向速度

	//初始化挡板
	position_x = HIGH / 2;
	position_y = WIDTH / 2;  //挡板的中心位置
	left = position_y - RIDUS;//挡板的左位置
	right = position_y + RIDUS;  //挡板的右位置

	//初始化砖块
	block_x = 0; //高度
	block_y = WIDTH / 2;  //宽度
	score = 0;

}

void show()//显示画面
{
	//system("cls"); 
	gotoxy(0, 0);

	int i, j;
	for (i = 0; i < HIGH + 1; i++)
	{
		for (j = 0; j < WIDTH; j++)
		{
			if (i == ball_x && j == ball_y)
			{
				printf("0");
			}
			else if (i == block_x && j == block_y)
			{
				printf("B");
			}
			else if (i == HIGH)
			{
				printf("-");
			}
			else if (j == WIDTH - 1)
			{
				printf("|");
			}
			else if (i == HIGH - 1 && j >= left && j <= right)
			{
				printf("*");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	}
	printf("反弹次数:%d\n", ball_number);
	printf("消除砖块的个数:%d\n", score);
}

void updateWitoutIput()
{
	if (ball_x == HIGH - 2)
	{
		if (ball_y >= left && ball_y <= right)
		{
			ball_number++;
		}
		else
		{
			printf("游戏失败!\n");
			exit(0);
		}
	}
	if (ball_x == block_x && ball_y == block_y)
	{
		score++;
		block_y = rand() % WIDTH;
	}

	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	if (ball_x >= HIGH - 2 || ball_x <= 0)
		ball_vx = -ball_vx;
	if (ball_y >= WIDTH -2 || ball_y <= 0)
		ball_vy = -ball_vy;

	Sleep(150);	//睡眠时间
}


void updateWithInput()//与用户输入有关的更新
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == 'a')
			position_y--;
		if (input == 'd')
			position_y++;
		left = position_y - RIDUS;
		right = position_y + RIDUS;
	}
}
int main()
{
	srand((unsigned)time(NULL));
	startup();//初始化
	HideCursor();
	while (1)
	{
		show();//显示画面  
		updateWitoutIput();//与用户输入无关的更新
		updateWithInput(); //与用户输入有关的更新
	}
	return 0;
}

效果演示

在这里插入图片描述

总结

虽然完成了一个简单的消砖块游戏,但是很多的功能都未能实现,如砖块的个数、游戏暂停和结束后的开始等等,任需要继续的努力。

  • 8
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值