本文介绍了如何让球从画布的两侧反弹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在画布的所有四面墙周围制作一个球(椭圆形),当发生这种情况时,我还想在每次弹跳后改变球的颜色和速度(当然是随机的)。 P.S我希望球继续在四面墙周围的画布上弹跳。谢谢你的帮助!
我的尝试:
这个是我试过的代码。它从左到右穿过x轴但不会在从右到左的路上停下来。我也不知道如何让它从顶部和底部反弹
I want to make a ball(an ellipse) bounce around all four walls of the canvas and as this happens I would also like to change the colour of the ball and speed after each bounce(randomly of course). P.S I want the ball to continue bouncing around the canvas off all four walls. Thanks for the help!
What I have tried:
This is the code I have tried. It gets the ball across the x-axis from left to right but doesn't stop on the way back from right to left. I also don't know how to get it to bounce off the top and bottom sides
// The position of the ball
var x = 25;
// How far the ball moves every time
var speed = 3;
var draw = function() {
background(47, 222, 126);
fill(48, 46, 48);
ellipse(x, 200, 50, 50);
// Moves the ball
x = x + speed;
if (x > 375) {
speed = -5;
if (x < 214) {
speed = 5;
}
}
};
推荐答案
这篇关于如何让球从画布的两侧反弹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!