青少年编程知识记录 codecoming

C++小游戏—弹跳小球

首先,要注意屏幕直角坐标系的问题,不然的话,后面移动过程中一定会出错。

然后,利用printf函数在屏幕坐标(x,y)处显示一个静止的小球字符‘O’,注意屏幕坐标系的原点在左上方,参考代码



#include<cstdio>  int main(){  	int x=10;  	int y=10;  	for(int i=0;i<x;i++)  		printf("\n");  	for(int j=0;j<y;j++)  		printf(" ");   		printf("O\n");  	return 0;   }



我们可以把它写成一个函数,这样调用更加方便。

#include<cstdio>    //展示小球   void showBall(int x,int y) {  	for(int i=0; i<x; i++)  		printf("\n");  	for(int j=0; j<y; j++)  		printf(" ");  	printf("O\n");  }  int main() {  	int x=10;  	int y=10;  	showBall(x,y);  	return 0;  }

小球下落过程,通过改变小球坐标的变量,即让小球的x坐标增加,让小球下落。在每次显示之前,使用清屏函数system("cls"),参考

#include<cstdio>  #include<cstdlib>   //展示小球   void showBall(int x,int y) {  	for(int i=0; i<x; i++)  		printf("\n");  	for(int j=0; j<y; j++)  		printf(" ");  	printf("O\n");  }  int main() {  	int x=10;  	int y=10;  	for(int x=1;x<10;x++)  	{  		system("cls");  		showBall(x,y); //这里的x被for循环初始化了   	 }   	return 0;  }

效果:

如果觉得移动的快了,可以加上sleep函数用来降低移动速度。



在上一步代码的基础中,增加记录速度的变量speed,小球的新位置x=旧位置x + 速度speed。判断小球到达上下边界时,速度改变方向,即改变speed的正负号。 



#include<cstdio>  #include<cstdlib>   //展示小球   void showBall(int x,int y) {  	for(int i=0; i<x; i++)  		printf("\n");  	for(int j=0; j<y; j++)  		printf(" ");  	printf("O\n");  }  int main() {  	int x=10;  	int y=10;  	int speed=1; //初始值速度为1  	while(1){  		x=x+speed;  		system("cls");  		showBall(x,y);  	}     	return 0;  }

上面这个状态小球会一直处于下落状态。我们可以使用if加上判断是否到达边界。

#include<cstdio>  #include<cstdlib>   //展示小球   void showBall(int x,int y) {  	for(int i=0; i<x; i++)  		printf("\n");  	for(int j=0; j<y; j++)  		printf(" ");  	printf("O\n");  }  int main() {  	int x=10;  	int y=10;  	int speed=1; //初始值速度为1  	while(1){  		if(x>10 || x<1) //加入边界判断   			speed= -1*speed; //让增量取反   		x=x+speed;  		system("cls");  		showBall(x,y);  	}   	return 0;  }









现在我们的小球只能上下移动,我们现在要让小球斜着移动。这样我们需要加入上下左右四个边界的判断。参考代码:

#include<cstdio>  #include<cstdlib>   //展示小球   void showBall(int x,int y) {  	for(int i=0; i<x; i++)  		printf("\n");  	for(int j=0; j<y; j++)  		printf(" ");  	printf("O\n");  }  int main() {  	int x=10;  	int y=10;  	int speed_x=1; //初始值x速度为1  	int speed_y=1; //初始值y速度为1  	int left=0; //左边界  	int right=20; //右边界   	int top=0; //上边界  	int bottom=10; //下边界  	   	while(1){  		if(x>bottom || x<top) //上下边界判断   			speed_x= -1*speed_x; //让增量取反   		if(y>right || y<left)  //左右边界判断   			speed_y= -1*speed_y;    		x=x+speed_x;  		y=y+speed_y;  		system("cls");  		showBall(x,y);  	}   	return 0;  }











我们可以把这个判断边界的也写成函数形式。

#include<cstdio>  #include<cstdlib>  #include<windows.h>  int x=10; //小球x坐标   int y=10; //小球y坐标   int speed_x=1; //初始值x速度为1  int speed_y=1; //初始值y速度为1  int left=0; //左边界  int right=20; //右边界  int top=0; //上边界  int bottom=10; //下边界  //展示小球  void showBall(int x,int y) {  	for(int i=0; i<x; i++)  		printf("\n");  	for(int j=0; j<y; j++)  		printf(" ");  	printf("O\n");  }  //判断边界函数  void border(int left,int right,int top,int bottom ) {  	if(x>bottom || x<top) //上下边界判断  		speed_x= -1*speed_x; //让增量取反  	if(y>right || y<left)  //左右边界判断  		speed_y= -1*speed_y;  	x=x+speed_x;  	y=y+speed_y;  }    int main() {  	while(1) {  		border(left,right,top,bottom);   		Sleep(500);  //加入延时函数   		system("cls");  		showBall(x,y);  	}  	return 0;  }



(adsbygoogle = window.adsbygoogle || []).push({});

作者:亿万年的星光 分类:趣味小程序 浏览: