当前位置: 首页 > news >正文

控制台字符动画-小球弹跳

在这里插入图片描述
在这里插入图片描述

// 清除屏幕
#define CLEAR() printf("\033[2J")// 上移光标
#define MOVEUP(x) printf("\033[%dA", (x))// 下移光标
#define MOVEDOWN(x) printf("\033[%dB", (x))// 左移光标
#define MOVELEFT(y) printf("\033[%dD", (y))// 右移光标
#define MOVERIGHT(y) printf("\033[%dC",(y))// 定位光标
#define MOVETO(x,y) printf("\033[%d;%dH", (x), (y))// 光标复位
#define RESET_CURSOR() printf("\033[H")// 隐藏光标
#define HIDE_CURSOR() printf("\033[?25l")// 显示光标
#define SHOW_CURSOR() printf("\033[?25h")//反显
#define HIGHT_LIGHT() printf("\033[7m")
#define UN_HIGHT_LIGHT() printf("\033[27m")
// 代码来源 https://blog.csdn.net/qq_40797283/article/details/132315990
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <thread>
#include "cursor.h"using namespace std;
using namespace std::chrono_literals;#define G 9.8fstruct PosInfo {float x;float y;
};struct AccelerateInfo {float accelerateX;float accelerateY;
};struct VelocityInfo {float velocityX;float velocityY;
};constexpr int32_t BoxWidth = 60;
constexpr int32_t BoxHeight = 20;
constexpr chrono::duration<long double> Time = 10ms;
constexpr float CoefficientRestitution = 0.9f;
PosInfo curPos{10.0f, .0f};
AccelerateInfo curAccelerate{.0f, G};
VelocityInfo curVelocity {3.0f, .0f};void PrintBoxAndBall(int32_t width, int32_t height) {for(int32_t i =0;i<height;i++) {printf(i==height - 1 ? "└" : "│");for(int32_t j=1;j<width-1;j++) {if (i == (int32_t)curPos.y && j == (int32_t)curPos.x) {putchar('*');} else {printf(i==height - 1 ? "─" : " ");}}printf(i==height - 1 ? "┘\n" : "│\n");}
}void UpdateBallPositionInfo(float time) {curVelocity.velocityX += curAccelerate.accelerateX * time;curVelocity.velocityY += curAccelerate.accelerateY * time;curPos.x += curVelocity.velocityX * time;curPos.y += curVelocity.velocityY * time;if (curPos.x > BoxWidth) {curPos.x = BoxWidth * 2 - curPos.x;curVelocity.velocityX = -curVelocity.velocityX * CoefficientRestitution;} else if (curPos.x < .0f) {curPos.x = -curPos.x;curVelocity.velocityX = -curVelocity.velocityX * CoefficientRestitution;}if (curPos.y > BoxHeight) {curPos.y = BoxHeight * 2 - curPos.y;curVelocity.velocityY = -curVelocity.velocityY * CoefficientRestitution;}printf("vx:%6.2f, vy:%6.2f, x:%6.2f, y:%6.2f\n", curVelocity.velocityX, curVelocity.velocityY, curPos.x, curPos.y);
}int main() {HIDE_CURSOR();while(1) {PrintBoxAndBall(BoxWidth, BoxHeight);UpdateBallPositionInfo(Time.count());MOVEUP(BoxHeight + 1);std::this_thread::sleep_for(Time);}SHOW_CURSOR();return 0;
}
http://www.dtcms.com/a/474955.html

相关文章:

  • 轻松Linux-11.线程(上)
  • 使用 MQ 解决分布式事务一致性问题
  • 中国石化工程建设公司网站保山市建设厅官方网站
  • 电子商务网站建设需要哪些步骤聚成网站建设
  • 前端开发指南,前端开发需要学什么
  • 一个网站设计的费用搭建影视网站违法
  • 深入解析 YOLO v2
  • 网站建设开票计量单位网站建设宣传素材
  • wordpress插件怎么使用兰州搜索引擎优化
  • Mysql初阶第七讲:Mysql复合查询
  • 代码随想录 101.对称二叉树
  • 深圳网站建设哪家比较专业大学城网站开发公司
  • 制作静态链接库并使用
  • 西方设计网站vs2010网站开发与发布
  • 网站维护工作的基本内容施工企业质量管理体系认证几年
  • 网站建设教程pdf下载企业官网网页设计
  • 重启MySQL,为何重启后MySQL数据“回滚”了?
  • 非洲购物网站排名文创产品设计包括哪些方面
  • Linux 文件内容查看与编辑
  • 2022 CSP-J复赛题
  • 【cubeide】IIC通信
  • python学习之访问模式和文件定位操作
  • CTF攻防世界WEB精选基础入门:simple_php
  • CodexField 热度登顶:内容资产化赛道的加速信号
  • 00_k8s容器编排系统
  • 广西柳州网站制作公司厦门做网站排名
  • 算法学习 02
  • 广西建设监理协会官网站珠海响应式网站建设推广公司
  • 机器学习实践项目(一)- Rossman商店销售预测 - 特征工程
  • 网站首页轮播图片wordpress不支持中文