C语言写简单的小游戏-挡板接小方块

1.写出一个大致的框架,绘图边界和画布。实现小功能,球可以自由的弹。接触到边界和挡板则反向。

	#include 
	#include 
	#include 


	//清屏,用于代替系统的"cls"清屏功能
	void gotoxy(int x , int y)
	{
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		COORD pos;
		pos.X = x;
		pos.Y = y;
		SetConsoleCursorPosition(handle,pos);
	}

	//隐藏光标
	//void HideCursor()
	//{
	//	CONSOLE_CURSOR_INFO concur_info = {1, 0};
	//	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),concur_info);
	//} 

	//作为全局变量进行定义
	int width,high;//作为画布的大小

	int pos_x,pos_y; //作为滑板的定位
	int radius;  //挡板的半径
	int left,right;  //挡板的左右边界

	int ball_x,ball_y;  //作为球的定位
	int ball_vx,ball_vy;  //作为球的运动速度

	int target_x,target_y;  //作为目标的定位

	void init()  //画面数据初始化
	{
		width = 20;
		high = 25;

		ball_x = 1;
		ball_y = width/2;

		ball_vx =1;
		ball_vy =1;
		
		pos_x = width/2;
		pos_y = high;
		radius = 5;
		left = pos_x - radius;
		right = pos_y + radius;

		//HideCursor();  //隐藏光标
	}

	void show()  //画面展示
	{
		gotoxy(0,0);  //自定义的清屏函数,用于代替系统的“cls”
		for(int i = 0;i <= high ; i++)
		{
			for (int j = 0; j <= width ; j++)
			{
				if((ball_x ==i)  && (ball_y == j))
					printf("*");
				else if (j == width)
					printf("||");
				else if(i == high)
					printf("=");
				else
					printf(" ");
			}
			printf("\n");
		}
	}

	void userInput()  //用户输入
	{
		
	}

	void userWithOut()  //游戏限制和规则
	{
		//球根据球速的变化,更新球的位置
		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;
	}

	int main()
	{
		//初始化
		init();
		while(1)
		{
			//显示画面,绘图
			show();
			//用户输入
			userInput();
			//与用户无关
			userWithOut();
		}
		

	}

 2.实现挡板功能,用于接球。

#include 
#include 
#include 
#include 


	//清屏,用于代替系统的"cls"清屏功能
	void gotoxy(int x , int y)
	{
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		COORD pos;
		pos.X = x;
		pos.Y = y;
		SetConsoleCursorPosition(handle,pos);
	}

	//隐藏光标
	void HideCursor()
	{
		CONSOLE_CURSOR_INFO concur_info = {1, 0};
		SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&concur_info);
	} 

	//作为全局变量进行定义
	int width,high;//作为画布的大小

	int pos_x,pos_y; //作为滑板的定位
	int radius;  //挡板的半径
	int left,right;  //挡板的左右边界

	int ball_x,ball_y;  //作为球的定位
	int ball_vx,ball_vy;  //作为球的运动速度
	int ball_num;   //积分

	int target_x,target_y;  //作为目标的定位

	void init()  //画面数据初始化
	{
		width = 40;
		high = 42;

		ball_x = 1;
		ball_y = width/2;

		ball_vx =1;
		ball_vy =1;
		ball_num = 0;
		
		pos_y = width/2;
		pos_x = high;
		radius = 2;
		left = pos_y - radius;
		right = pos_y + radius;

		HideCursor();  //隐藏光标
	}

	void show()  //画面展示
	{
		gotoxy(0,0);  //自定义的清屏函数,用于代替系统的“cls”
		int i,j;
		for( i= 0;i <= high ; i++)
		{
			for (j = 0; j <= width ; j++)
			{
				if((ball_x ==i)  && (ball_y == j))
					printf("o");
				else if (j == width)
					printf("|");
				else if(i == high)
					printf("-");
				else if ((i == high-1) && (j>=left) && (j<=right))
					printf("*");
				else
					printf(" ");
			}
			printf("\n");
			//Sleep(20);
		}
		printf("反弹总分:%d \n",ball_num);
	}

	void userInput()  //用户输入
	{
		char input;
		if(kbhit())
		{
			input =getch();

			if(input =='a')
			{
				pos_y--;
				left = pos_y - radius;
				right = pos_y + radius;
			}
			if(input =='d')
			{
				pos_y++;
				left = pos_y - radius;
				right = pos_y + radius;
			}
		}
		
	}

	void userWithOut()  //游戏限制和规则
	{
		if(ball_x == high-1)
		{
			if ((ball_y>= left) && (ball_y<=right))
			{
				ball_num++;
				//ball_vy = -ball_vy;
				ball_vx = -ball_vx;
			}
			else
			{
				printf("Game Over!!!");
				exit(0);
			}
		}

		//球根据球速的变化,更新球的位置
		ball_x = ball_x +ball_vx;   //倾斜弹球d
		ball_y = ball_y +ball_vy;

		//挡板不能国过界限
		if(pos_y < 0)
			pos_y = 0;
		else if(pos_y>width)
			pos_y = width;



		//碰到了球的边界,则反向
		if((ball_x == 0) || (ball_x == high-1))
			ball_vx = -ball_vx;
		if((ball_y == 0) || (ball_y == width-1))
			ball_vy = -ball_vy;


	}

	int main()
	{
		//初始化
		init();
		while(1)
		{
			//显示画面,绘图
			show();
			//用户输入
			userInput();
			//与用户无关
			userWithOut();
		}
		

	}

 

你可能感兴趣的:(C语言游戏)