C++入门——仿真小球自由落体运动和抛物线运动

参考

  1. 《C和C++游戏趣味编程》 童真

仿真自由落体的小球

实现小球受重力影响加速下落后,碰到地面反弹的效果

代码如下:

#include <graphics.h>
#include <conio.h>
#include <stdio.h>

int main()
{
	float y = 100;                             // 小球的y坐标
	float vy = 0;                              // 小球y方向的速度,初速度为0
	float g = 0.5;                             // 小球y方向的加速度
	initgraph(600, 600);                       // 初始化游戏画面,600x600
	while (1)
	{
		vy += g;                               // 利用加速度g更新vy速度
		y += vy;                               // 利用y方向速度vy更新y坐标
		if (y >= 580)						   // 当碰到地面时
		{
			vy = -0.95 * vy;                   // y方向速度改变方向,并受阻力影响,绝对值变小
		}
		if (y > 580)                           // 防止小球穿过地面
		{
			y = 580;
		}
		cleardevice();                         // 清除掉之前的绘制内容
		fillcircle(300, y, 20);                // 在坐标(300, y)处画一个半径为20的圆
		Sleep(10);                             // 暂停10毫秒
	}
	_getch();                                  // 等待按键
	closegraph();                              // 关闭窗口
	return 0;
}

实现小球抛物线运动

增加变量x表示小球的x坐标,vx表示x方向的速度,vx初始化为10。尝试实现小球抛物线运动。并实现小球在窗口中四处反弹的效果

代码如下:

#include <graphics.h>
#include <conio.h>
#include <stdio.h>

int main()
{
	float x = 0;                               // 小球的x坐标
	float y = 100;                             // 小球的y坐标
	float vx = 10;                              // 小球x方向的速度,初速度为1
	float vy = 0;                              // 小球y方向的速度,初速度为0
	float g = 0.5;                             // 小球y方向的加速度
	initgraph(600, 600);                       // 初始化游戏画面,600x600
	while (1)
	{
		x += vx;                               // 利用x方向速度vx更新x坐标
		if (x >= 580)                          // 当碰到右侧墙壁时
		{
			vx = -0.95 * vx;                   // x方向速度改变方向,并受阻力影响,绝对值变小
		}
		if (x > 580)                           // 放在小球穿过右侧墙面
		{
			x = 580;
		}
		if (x <= 20)                           // 当碰到左侧墙壁时
		{
			vx = -0.95 * vx;                   // x方向速度改变方向,并受阻力影响,绝对值变小
		}
		if (x < 20)
		{
			x = 20;                            // 防止小球穿过左侧墙面
		}

		vy += g;                               // 利用加速度g更新vy速度
		y += vy;                               // 利用y方向速度vy更新y坐标
		if (y >= 580)						   // 当碰到地面时
		{
			vy = -0.95 * vy;                   // y方向速度改变方向,并受阻力影响,绝对值变小
		}
		if (y > 580)                           // 防止小球穿过地面
		{
			y = 580;
		}
		cleardevice();                         // 清除掉之前的绘制内容
		fillcircle(x, y, 20);                // 在坐标(x, y)处画一个半径为20的圆
		Sleep(10);                             // 暂停10毫秒
	}
	_getch();                                  // 等待按键
	closegraph();                              // 关闭窗口
	return 0;
}
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Vue实现购物车小球抛物线动画效果的方法: 1. 首先,在Vue组件中定义一个数组,用于存储购物车中的商品信息。 2. 在购物车页面中,为每个商品添加一个按钮,点击按钮时触发一个方法,将该商品的信息添加到数组中。 3. 在页面中使用`v-for`指令遍历数组,将每个商品的信息渲染到页面上。 4. 在每个商品的元素上添加一个动画效果,使其在添加到购物车时产生抛物线运动的效果。 5. 在Vue组件中定义一个方法,用于计算抛物线的路径。该方法接收商品元素的位置信息和购物车元素的位置信息作为参数,根据这些信息计算出抛物线的路径。 6. 在添加商品到购物车的方法中,调用计算抛物线路径的方法,并将路径信息传递给动画效果。 7. 使用CSS动画或JavaScript动画库来实现抛物线动画效果。 下面是一个示例代码: ```html <template> <div> <div class="product" v-for="product in products" :key="product.id"> <img :src="product.image" alt="product image"> <button @click="addToCart(product)">Add to Cart</button> </div> <div class="cart"> <div class="ball" v-for="item in cartItems" :key="item.id" ref="ball"></div> </div> </div> </template> <script> export default { data() { return { products: [ { id: 1, image: 'product1.jpg' }, { id: 2, image: 'product2.jpg' }, { id: 3, image: 'product3.jpg' } ], cartItems: [] }; }, methods: { addToCart(product) { this.cartItems.push(product); this.animateBall(product); }, animateBall(product) { const ball = this.$refs.ball[this.cartItems.length - 1]; const productRect = product.$el.getBoundingClientRect(); const cartRect = this.$el.querySelector('.cart').getBoundingClientRect(); const startX = productRect.left + productRect.width / 2; const startY = productRect.top + productRect.height / 2; const endX = cartRect.left + cartRect.width / 2; const endY = cartRect.top + cartRect.height / 2; const curve = BezierEasing(0.42, 0, 0.58, 1); const duration = 1000; anime({ targets: ball, translateX: [startX, endX], translateY: [startY, endY], scale: [1, 0.5], opacity: [1, 0], easing: curve, duration: duration }); } } }; </script> <style> .product { display: inline-block; margin: 10px; } .cart { position: relative; width: 100px; height: 100px; background-color: #ccc; } .ball { position: absolute; width: 20px; height: 20px; background-color: red; border-radius: 50%; } </style> ``` 请注意,上述代码中的`product.image`和`product.$el`需要根据实际情况进行替换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值