缓冲运动

<!DOCTYPE html>

<html>


<head>

<meta charset="UTF-8">

<title></title>

<style type="text/css">

#div1 {

width: 100px;

height: 100px;

background-color: red;

position: absolute;

top: 50px;

/*left: 0;*/

left: 600px;

}

#div2{

width: 1px;

height: 300px;

background-color: black;

position: absolute;

top: 0;

left: 300px;

}

</style>

</head>


<body>

<input type="button" name="btn1" id="btn1" value="开始运动" />

<div id="div1"></div>

<div id="div2"></div>

<script type="text/javascript">

var oBtn = document.getElementById("btn1");

var oDiv = document.getElementById("div1");

oBtn.onclick = function() {

startMove();

};


// 缓冲运动

// 先快后慢

// 距离越大速度越快,距离越小速度越慢(距离和速度成正比)


var timer = null;


function startMove() {

clearInterval(timer);

timer = setInterval(function() {

var speed = (300 - oDiv.offsetLeft) / 10;

// 如果大于0,向上取整,如果小于0,向下取整

speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

if (oDiv.offsetLeft == 300) {

clearInterval(timer);

} else {

oDiv.style.left = oDiv.offsetLeft + speed + 'px';

}

document.title = oDiv.offsetLeft + "," + speed;

}, 20);

}

</script>

</body>


</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于窗体中小球运动的问题。实现这个功能需要用到窗体控件和绘图API。 以下是实现的大致步骤: 1. 创建一个窗体,并在窗体中绘制一个矩形框。 2. 在窗体中添加一个定时器,每隔一段时间(比如10毫秒)就触发定时器事件,在定时器事件中更新小球的位置。 3. 在窗体的Paint事件中使用双缓冲绘制小球和矩形框。 4. 在更新小球位置时,需要检测小球是否与矩形框四壁发生碰撞,如果发生碰撞,需要改变小球的运动方向。 5. 在绘制小球时,可以使用GDI+来实现更加生动的效果,比如添加阴影、渐变等。 下面是示例代码:(假设窗体大小为500x500,小球半径为20) ```csharp public partial class Form1 : Form { private Timer timer; private int ballX = 250; // 小球初始位置 private int ballY = 250; private int ballVx = 5; // 小球初始速度 private int ballVy = 5; private int ballRadius = 20; private int boxWidth = 400; // 矩形框大小 private int boxHeight = 400; public Form1() { InitializeComponent(); this.DoubleBuffered = true; // 开启双缓冲 this.timer = new Timer(); this.timer.Interval = 10; // 设置定时器间隔 this.timer.Tick += Timer_Tick; this.timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { // 更新小球位置 ballX += ballVx; ballY += ballVy; // 检测小球是否与矩形框四壁碰撞 if (ballX - ballRadius < 0 || ballX + ballRadius > boxWidth) { ballVx = -ballVx; } if (ballY - ballRadius < 0 || ballY + ballRadius > boxHeight) { ballVy = -ballVy; } // 重绘窗体 this.Invalidate(); } private void Form1_Paint(object sender, PaintEventArgs e) { // 绘制矩形框 e.Graphics.DrawRectangle(Pens.Black, 50, 50, boxWidth, boxHeight); // 绘制小球 Brush brush = new SolidBrush(Color.Red); e.Graphics.FillEllipse(brush, ballX - ballRadius, ballY - ballRadius, ballRadius * 2, ballRadius * 2); } } ``` 这样,就实现了一个简单的窗体中小球运动的功能,并且使用了双缓冲消除了闪烁。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值