C++实现简易反弹小球游戏的示例代码
作者:畅游星辰大海 时间:2023-06-15 08:18:50
前言
我们利用printf 函数实现一个在屏幕上弹跳的小球,如图所示。弹跳的小球游戏比较简单、容易入门,也是反弹球消砖块、接金币、台球等很多游戏的基础。
完成游戏前需要掌握的语法知识:标识符、变量、常量、运算符与表达式,以及 printf、scanf、if-else、while、for 语句的用法。
正文部分我们会逐步进行讲解,前一部分是后一部分的基础,大家不要跳过阅读。
一、显示静止的小球
首先利用 printf 函数在屏幕坐标(x,y)处显示一个静止的小球字符'o',注意屏幕坐标系的原点在左上角,如图
#include<stdio.h>
int main()
{
int i,j;
int x=5;
int y=10;
//输出小球上面的空行
for(i=0;i<x;i++)
printf("\n");
//输出小球左边的空格
for(j=0;j<y;j++)
printf(" ");
printf("o");//输出小球
printf("\n");
return 0;
}
二、小球下落
改变小球的坐标变量,即让小球的i坐标增加,从而让小球下落。在每次显示之前使用了清屏函数system("cls"),注意需要包含新的头文件#include<stdlib.h>。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j;
int x=1;
int y=10;
for(x=1;x<10;i++)
{
system("cls");//清屏函数
//输出小球上面的空行
for(i=0;i<x;i++)
printf("\n");
//输出小球左边的空格
for(j=0;j<y;j++)
printf(" ");
printf("o");//输出小球
printf("\n");
return 0;
}
return 0;
}
三、上下弹跳的小球
在上一步代码的基础上增加记录速度的变量 velocity,小球的新位置x=旧位置x+速度velocity。当判断小球到达上、下边界时改变方向,即改变velocity 的正负号。
#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");// 清屏函数
//输出小球上面的空行
for(i=0;i<x;i++)
printf("\n");
//输出小球左边的空格
for(j=0;j<y;j++)
printf(" ");//输出小球
printf("o");
printf("\n");
if(x==height)
velocity=-velocity;
if(x==0)
velocity=-velocity;
}
return 0;
}
四、斜着弹跳的小球
下面让程序更有趣,使小球斜着弹跳,主要思路是增加x、y两个方向的速度控制变量velocity_x、velocity_y,初值为1;velocity_x碰到上、下边界后改变正负号,velocity_y碰到左、右边界后改变正负号。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j;
int x=0;
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");
//
for(i=0;i<x;i++)
printf("\n");
//
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 函数(#include<windows.h>)。比如 sleep(10)表示程序执行到此处暂停 10ms,从而控制小球弹跳的速度。
六、完整代码
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
int i,j;
int x=0;
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");
//
for(i=0;i<x;i++)
printf("\n");
//
for(j=0;j<y;j++)
printf(" ");
printf("o");
printf("\n");
Sleep(10);
if(x==top||x==bottom)
velocity_x=-velocity_x;
if(y==left||y==right)
velocity_y=-velocity_y;
}
return 0;
}
来源:https://blog.csdn.net/m0_73222051/article/details/127188725
标签:C++,反弹小球,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何在android中使用html作布局文件
2023-01-14 07:49:10
C#观察者模式(Observer Pattern)实例教程
2021-07-13 02:53:39
java中不定长参数的实例用法
2021-06-17 02:49:35
Intellij IDEA全局替换快捷键整理
2022-06-17 14:51:39
![](https://img.aspxhome.com/file/2023/2/132002_0s.png)
基于json解析神器 jsonpath的使用说明
2022-06-14 08:57:18
![](https://img.aspxhome.com/file/2023/5/97815_0s.jpg)
Spring Data Jpa框架最佳实践示例
2021-11-25 00:43:01
深入了解java.util.Arrays的使用技巧
2023-01-10 18:01:32
导入项目出现Java多个工程相互引用异常A cycle was detected in the build path of project的解决办法
2023-06-26 16:27:17
![](https://img.aspxhome.com/file/2023/5/101935_0s.png)
C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题实例
2023-04-01 00:39:00
![](https://img.aspxhome.com/file/2023/2/85742_0s.png)
C#资源释放方法实例分析
2022-03-22 04:46:20
C++实现经典24点纸牌益智游戏
2023-04-22 01:05:02
![](https://img.aspxhome.com/file/2023/0/109780_0s.jpg)
C# 获取系统进程的用户名
2022-12-06 02:38:03
C#使用Process类调用外部exe程序
2023-02-12 16:54:20
C#中的文件路径获取函数和文件名字获取函数小结
2023-10-18 10:28:20
mybatis批量添加,批量更新之前如何判断是否已经存在
2022-01-03 20:33:38
![](https://img.aspxhome.com/file/2023/2/78962_0s.jpg)
Android使用FontMetrics对象计算位置坐标
2023-02-06 15:35:20
![](https://img.aspxhome.com/file/2023/6/110736_0s.png)
Android自定义wheelview实现滚动日期选择器
2021-11-22 16:04:19
![](https://img.aspxhome.com/file/2023/6/129036_0s.png)
在unity脚本中控制Inspector面板的参数操作
2023-07-22 11:39:58
![](https://img.aspxhome.com/file/2023/9/122729_0s.png)
C#中关于double.ToString()的用法
2021-12-06 13:12:22
Android滑动组件悬浮固定在顶部效果
2022-12-13 19:24:32
![](https://img.aspxhome.com/file/2023/2/118952_0s.gif)