【C语言】数组反弹球消砖块

一、模板

简单的模板,目前由头文件,全局变量,清屏函数,初始化数据,显示画面,与用户有关的更新,与用户无关的更新,以及主函数组成。
如此可以简化代码结构,方便于修改,便于改bug,思路也会更加清晰

//头文件
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

//全局变量
int...
...

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 startup()		//数据的初始化 
{
}

void show()		//显示画面 
{
}

void updateWithoutInput()		//与用户输入无关的更新
{
}

void updateWithInput()		//与用户输入有关的更新 
{
}

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

二、反弹球

实现小球反弹的效果。其中,二维数组 int canvas[High][Width]存储游戏画布中的所有元素,0输入空格,1输入小球‘0’;小球坐标为(ball_x,ball_y),则canvas[ball_x][ball_y] = 1,数组的其他元素为0.在updateWithInput()函数中小球更新位置时先将原来位置所在元素设为0,再将新位置所在元素设为1.
在这里插入图片描述

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

#define High 15
#define Width 20		//游戏画面的 尺寸 

//全局变量 
int ball_x,ball_y;		//小球的坐标 
int ball_vx,ball_vy;		//小球的速度 
int canvas[High][Width] = {0};		//二维数组存储游戏画布中对应的元素 
									//0为空格,1为小球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 startup()		//数据的初始化 
{
	ball_x = 0;
	ball_y = Width/2;
	ball_vx = 1;
	ball_vy = 1;
	canvas[ball_x][ball_y] = 1;
}

void show()		//显示画面 
{
	gotoxy(0,0);		//光标移动到原点位置,以下重画清屏 
	int i,j;
	for (i=0;i<High;i++)
	{
		for (j=0;j<Width;j++)
		{
			if(canvas[i][j]==0)
				printf(" ");		//输出空格 
			else if(canvas[i][j]==1)
				printf("0");		//小时小球 0			
		}
		printf("|\n");		//显示右边界 
	}
	for(j=0;j<Width;j++)
		printf("-");		//显示下边界 
}

void updateWithoutInput()		//与用户无关的更新 
{
	canvas[ball_x][ball_y] = 0;
	
	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	
	if( (ball_x==0) || (ball_x==High-1) )
		ball_vx = -ball_vx;
	if( (ball_y==0) || (ball_y==Width-1) )
		ball_vy = -ball_vy;
		
	canvas[ball_x][ball_y] = 1;
	
	Sleep(50);
}

void updateWithInput()		//与用户输入有关的更新 
{
}

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

三、增加挡板

当二维数组canvas[High][Width]中的元素为2时输出挡板‘*’。当在updateWithInput()函数中控制移动时每帧仅移动一个单位,同样需要将原来数组元素值设为0,再将新位置所在的元素设为2。
在这里插入图片描述

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

#define High 15
#define Width 20		//游戏画面的 尺寸 

//全局变量 
int ball_x,ball_y;		//小球的坐标 
int ball_vx,ball_vy;		//小球的速度 
int position_x,position_y;		//
int ridus;
int left,right; 
int canvas[High][Width] = {0};		//二维数组存储游戏画布中对应的元素 
//0为空格,1为小球 0,2为挡板 

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 startup()		//数据的初始化 
{
	ball_x = 0;
	ball_y = Width/2;
	ball_vx = 1;
	ball_vy = 1;
	canvas[ball_x][ball_y] = 1;
	
	ridus = 5;
	position_x = High-1;
	position_y = Width/2;
	left = position_y - ridus;
	right = position_y = ridus;
	
	int k;
	for ( k=left;k<=right;k++)
		canvas[position_x][k] = 2;
}

void show()		//显示画面 
{
	gotoxy(0,0);		//光标移动到原点位置,以下重画清屏 
	int i,j;
	for (i=0;i<High;i++)
	{
		for (j=0;j<Width;j++)
		{
			if(canvas[i][j]==0)
				printf(" ");		//输出空格 
			else if(canvas[i][j]==1)
				printf("0");		//输出小球 0
			else if(canvas[i][j]==2)
				printf("*");		//输出挡板*			
		}
		printf("|\n");		//显示右边界 
	}
	for(j=0;j<Width;j++)
		printf("-");		//显示下边界 
	printf("\n");
}

void updateWithoutInput()		//与用户无关的更新 
{
	if (ball_x == High-2)
	{
		if( (ball_y>=left) && (ball_y<=right) )		//被挡板挡住             
		{
			printf("\a");		//响铃 
		}
		else		//没有挡板被挡住 
		{
			printf("游戏失败\n");
			system("pause");
			exit(0);
		}
	}
	
	canvas[ball_x][ball_y] = 0;
	
	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	
	if( (ball_x==0) || (ball_x==High-2) )
		ball_vx = -ball_vx;
	if( (ball_y==0) || (ball_y==Width-1) )
		ball_vy = -ball_vy;
		
	canvas[ball_x][ball_y] = 1;
	
	Sleep(50);
}

void updateWithInput()		//与用户输入有关的更新 
{
	char input;
	if(kbhit())		//判断是否有输入 
	{
		input = getch();		//根据用户的不同输入来移动,不必输入回车 
		if ((input == 'a') && (left>0))
		{
			position_y--;		//位置左移 
			left = position_y-ridus;
			right = position_y+ridus;
			canvas[position_x][left] = 2;
		}
		if ((input == 'd') && (right<Width-1))
		{
			position_y++;		//位置右移 
			left = position_y-ridus;
			right = position_y+ridus; 
			canvas[position_x][right] = 2;
		}
	}
}

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

四、消砖块和计数功能

增加砖块,当二维数组canvas[High][Width]中的元素值为3时输入挡板‘#’。
由于采用数组,在startup中可以很方便的初始化多个砖块。在updateWithoutInput()中判断小球碰到砖块后对应数组元素值由3变为0,即该砖块消失
加入两个全局变量

int ball_number;		//反弹小球的次数
int score;		//消掉的砖块的个数 

只后在show函数中加入两个printf显示这两个变量

五、最终成果

在这里插入图片描述

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

#define High 15
#define Width 20		//游戏画面的 尺寸 

//全局变量 
int ball_x,ball_y;		//小球的坐标 
int ball_vx,ball_vy;		//小球的速度 
int position_x,position_y;		//挡板的中心坐标 
int ridus;		//挡板的半径大小 
int left,right; 		//挡板的左右位置 
int ball_number;		//反弹小球的次数
int score;		//消掉的砖块的个数 
int canvas[High][Width] = {0};		//二维数组存储游戏画布中对应的元素 
//0为空格,1为小球 0,2为挡板 ,3为砖块# 

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 startup()		//数据的初始化 
{	
	ridus = 5;
	position_x = High-1;
	position_y = Width/2;
	left = position_y - ridus;
	right = position_y + ridus;
	
	ball_x = position_x-1;
	ball_y = position_y;
	ball_vx = -1;
	ball_vy = 1;
	canvas[ball_x][ball_y] = 1;
	ball_number = 0;
	score=0;	
	
	int k,i;
	for ( k=left;k<=right;k++)		//挡板 
		canvas[position_x][k] = 2;
	
	for(k=0;k<Width;k++)		//加几排砖块 
		for(i=0;i<High/4;i++) 
			canvas[i][k] = 3;
}

void show()		//显示画面 
{
	gotoxy(0,0);		//光标移动到原点位置,以下重画清屏 
	int i,j;
	for (i=0;i<High;i++)
	{
		for (j=0;j<Width;j++)
		{
			if(canvas[i][j]==0)
				printf(" ");		//输出空格 
			else if(canvas[i][j]==1)
				printf("0");		//输出小球 0
			else if(canvas[i][j]==2)
				printf("*");		//输出挡板*	
			else if(canvas[i][j]==3)
				printf("#");		//输出砖块#	
		}
		printf("|\n");		//显示右边界 
	}
	for(j=0;j<Width;j++)
		printf("-");		//显示下边界 
	printf("\n");
	printf("反弹的小球数:%d\n",ball_number/7);
	printf("消掉的砖块数:%d\n",score);	
}

void updateWithoutInput()		//与用户无关的更新 
{
	if (ball_x == High-2)
	{
		if( (ball_y>=left) && (ball_y<=right) )		//被挡板挡住             
		{
			ball_number ++;
		}
		else		//没有挡板被挡住 
		{
			printf("游戏失败\n");
			system("pause");
			exit(0);
		}
	}
	
	static int speed = 0;
	if(speed<7)
		speed++;
	if(speed == 7)
	{
	
		speed = 0;
	
		canvas[ball_x][ball_y] = 0;
		
		ball_x = ball_x + ball_vx;
		ball_y = ball_y + ball_vy;
		canvas[ball_x][ball_y] = 1;
		
		if( (ball_x==0) || (ball_x==High-2) )
			ball_vx = -ball_vx;
		if( (ball_y==0) || (ball_y==Width-1) )
			ball_vy = -ball_vy;
			
		if(canvas[ball_x-1][ball_y]==3)
		{
			ball_vx = -ball_vx;
			canvas[ball_x-1][ball_y]=0;
			printf("\a");
			score++;			
		}	
	}
}

void updateWithInput()		//与用户输入有关的更新 
{
	char input;
	if(kbhit())		//判断是否有输入 
	{
		input = getch();		//根据用户的不同输入来移动,不必输入回车 
		if (input == 'a' && left>0)
		{
			canvas[position_x][right] = 0;
			position_y--;		//位置左移 
			left = position_y-ridus;
			right = position_y+ridus;
			canvas[position_x][left] = 2;
		}
		if (input == 'd' && right<Width-1)
		{
			canvas[position_x][left] = 0;
			position_y++;		//位置右移 
			left = position_y-ridus;
			right = position_y+ridus; 
			canvas[position_x][right] = 2;
		}
	}
}

int main()
{
	startup();		//数据的初始化 
	while(1)		//游戏循环执行 
	{
		show();		//显示画面 
		updateWithoutInput();		//与用户输入无关的更新 
		updateWithInput();		//与用户输入有关的更新 
	}
	return 0;
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值