软件编程
位置:首页>> 软件编程>> C语言>> C语言实现简单弹跳小球

C语言实现简单弹跳小球

作者:无限的菜鸟  发布时间:2022-07-03 12:54:34 

标签:C语言,弹跳小球

本文实例为大家分享了C语言实现简单弹跳小球的具体代码,供大家参考,具体内容如下

本节利用 printf 函数 实现一个在屏幕上弹跳的小球,内容简单容易入门,这也是以后我们在设计更多游戏中可能用到的东西。

绘制静止的小球

我们将以如图坐标系进行游戏,即以窗口左上角为原点。

C语言实现简单弹跳小球

我们希望在坐标(x , y)处显示静止的小球:

#include <stdio.h>
int main(){
?? ?int i,j; // 这两个量是循环中要使用的,一定先声明
?? ?int x = 5;
?? ?int y = 10;
?? ?//输出小球上方空行【x坐标】
?? ?for(i=0; i<x; i++)
?? ??? ?printf("\n");
?? ?//输出小球左边的空格【y坐标】
?? ?for(j=0; j<y; j++)
?? ??? ?printf(" ");
?? ?printf("o");
?? ?printf("\n");
?? ?return 0;
}

随后我们可以将此文件命名为 ball.c
再使用GCC编译:

gcc ball.c -o ball.out

可以直接用cc来代替gcc
-o ball.out 表示输出文件为 ball.out
当然可以不写这些,输出会成为a.out
如果是Windows系统,会输出exe文件
这里是我编译成功的结果:

C语言实现简单弹跳小球

里面的CGame.c是源文件,而那个绿油油的 ball.out 就是编译输出的文件。
然后运行它就可以了。

小球下落

如何让小球下落?根据我们之前规定的坐标系,可以知道只需让小球x坐标不断增加即可。但我们同时不想让每个x坐标上都留着原来的小球,所以需要使用清屏函数 system(&ldquo;cls&rdquo;)
注意要包含头文件<stdlib.h>

#include <stdio.h>
#include <stdlib.h>
int main(){
?? ?int i,j;
?? ?int x = 1;
?? ?int y = 10;
?? ?for(x=1; x<10; x++)
?? ?{
?? ??? ?system("cls"); //每次循环先清屏
?? ??? ?//输出小球上方空行【x坐标】
?? ??? ?for(i=0; i<x; i++)
?? ??? ??? ?printf("\n");
?? ??? ?//输出小球左边的空格【y坐标】
?? ??? ?for(j=0; j<y; j++)
?? ??? ??? ?printf(" ");
?? ??? ?printf("o");
?? ??? ?printf("\n");?? ?
?? ?}
?? ?return 0;
}

同样我们编译、运行。

上下弹跳的小球

接下来,我们要在上一步代码的基础上增加一个记录小球速度的变量 velocity 从而把小球的下落从简单的循环改为小球坐标x = x + velocity,当速度为正值时小球下落(x坐标增加),反之小球上升(x坐标减小)。

#include <stdio.h>
#include <stdlib.h>
int main(){
?? ?int i,j; // 这两个量是循环中要使用的,一定先声明
?? ?int x = 5;
?? ?int y = 10;
?? ?int height = 20; //边界高度,到达边界时改变方向
?? ?int velocity = 1;
?? ?
?? ?while(1){
?? ??? ?x = x + velocity;
?? ??? ?system("cls"); //每次循环先清屏
?? ??? ?//输出小球上方空行【x坐标】
?? ??? ?for(i=0; i<x; i++)
?? ??? ??? ?printf("\n");
?? ??? ?//输出小球左边的空格【y坐标】
?? ??? ?for(j=0; j<y; j++)
?? ??? ??? ?printf(" ");
?? ??? ?printf("o");
?? ??? ?printf("\n");
?? ??? ?
?? ??? ?if(x == height)?? ??? ?//到达下边界
?? ??? ??? ?velocity = -velocity; // 下落改为上升
?? ??? ?if(X == 0)?? ??? ??? ?//到达上边界
?? ??? ??? ?velocity = -velocity; // 上升改为下落
?? ?}
?? ?return 0;
}

注意这里是个死循环,小球不断在x = 0到x = 20 中间上下弹跳,并且保持恒定速度。

斜方向弹跳

既然已经实现了上下弹跳,那么如何实现斜方向弹跳呢?
这只是很简单的速度合成而已,我们上一步已经使用 velocity 来表示小球在 x 方向的速度了,那么我们只要再加一个 y 方向的速度即可。
用什么来表示呢?speed?
一定注意,为了确保代码可读性 ,也就是说让别人好接手的情况下,注释是一方面,变量命名也是很重要的一方面。
我们可以使用 velocity_x 表示 x 方向速度,用 velocity_y 表示 y 方向速度。

那么速度控制就会变成:到达上、下边界改变x速度(即 velocity_x 的符号),到达左、右边界改变y速度(即 velocity_y 的符号)。

#include <stdio.h>
#include <stdlib.h>
int main(){
?? ?int i,j;
?? ?int x = 1;
?? ?int y = 5;

?? ?int velocity_x = 1;
?? ?int velocity_y = 1;
?? ?int left = 0;//左边界坐标
?? ?int right = 20;//右边界坐标
?? ?int top = 0;//上边界坐标
?? ?int bottom = 10;//下边界坐标

?? ?while(1)
?? ?{
?? ??? ?x = x + velocity_x;
?? ??? ?y = y + velocity_y;
?? ??? ?system("cls"); //每次循环先清屏
?? ??? ?//输出小球上方空行【x坐标】
?? ??? ?for(i=0; i<x; i++)
?? ??? ??? ?printf("\n");
?? ??? ?//输出小球左边的空格【y坐标】
?? ??? ?for(j=0; j<y; j++)
?? ??? ??? ?printf(" ");
?? ??? ?printf("o");
?? ??? ?printf("\n");
?? ?
?? ??? ?if((x == top)||(x == bottom))
?? ??? ??? ?velocity_x = -velocity_x;
?? ??? ?if((y == left)||(y == right))
?? ??? ??? ?velocity_y = -velocity_y;
?? ?}
?? ?return 0
}

然后你就可以得到一个十分鬼畜(事实如此)的小球弹跳动画了。

控制小球弹跳速度

我们刚才说过,编译之后运行我们会得到一个十分鬼畜的小球弹跳动画,那么为了减缓这个动画的刷新速度(至少不会快到抽象),我们将使用 sleep 函数来在每个循环建立一个暂停机制。

sleep(50); //表示到达此处程序暂停 50ms

小结

完整代码:

#include <stdio.h>
#include <stdlib.h>
int main(){
?? ?int i,j;
?? ?int x = 1;
?? ?int y = 5;

?? ?int velocity_x = 1;
?? ?int velocity_y = 1;
?? ?int left = 0;//左边界坐标
?? ?int right = 20;//右边界坐标
?? ?int top = 0;//上边界坐标
?? ?int bottom = 10;//下边界坐标

?? ?while(1)
?? ?{
?? ??? ?x = x + velocity_x;
?? ??? ?y = y + velocity_y;
?? ??? ?system("cls"); //每次循环先清屏
?? ??? ?//输出小球上方空行【x坐标】
?? ??? ?for(i=0; i<x; i++)
?? ??? ??? ?printf("\n");
?? ??? ?//输出小球左边的空格【y坐标】
?? ??? ?for(j=0; j<y; j++)
?? ??? ??? ?printf(" ");
?? ??? ?printf("o");
?? ??? ?printf("\n");
?? ??? ?sleep(50); ?//到达此处程序暂停 50ms
?? ?
?? ??? ?if((x == top)||(x == bottom))
?? ??? ??? ?velocity_x = -velocity_x;
?? ??? ?if((y == left)||(y == right))
?? ??? ??? ?velocity_y = -velocity_y;
?? ?}
?? ?return 0
}

到此为止,我们最基本的实现就完工了!

思考题目:

1. 能不能去掉sleep函数来实现速度减慢?
2. 能不能通过在循环中改变velocity_x来模拟一个自由落体效果?

以后我们会进一步展开游戏编写,希望大家最终能如愿以偿地成为C语言大师!

来源:https://blog.csdn.net/weixin_54597744/article/details/113825174

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com