gpt4 book ai didi

java - 使用 while 循环弹跳球,我需要将每次弹跳减少 30% 直到 0,然后退出循环以避免无限循环

转载 作者:行者123 更新时间:2023-12-01 12:33:16 24 4
gpt4 key购买 nike

我的程序是创建一个弹跳球,每次上下移动负 30%...以表明球已停止在静止位置。

我还想让球在到达弹跳顶部时逐渐减速,并在下降回到原始位置时逐渐加速。

所以我设置了第一部分,我只是无法进行无限循环,并且在每次弹跳后将向上 y 停止位置减少 30%。

当我写这个问题时,我意识到,我需要使第一个 while 循环中的 y 值增加 30%,门楣达到 400,正确吗?

如何在两个 while 循环周围创建一个循环来一遍又一遍地重复,而不是无限循环?

感谢任何意见、评论或想法!

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;

public class MY_Proj04 extends JApplet
{
int x, y;
Color Background;
public void init()
{
x = 100;
y = 400;
Background = getBackground();
}

public void paint(Graphics g)
{
// I tryed putting a while loop around the two following while loops and
// doing y = y * 30/100, I did this because the fill oval can't take a double
// as one of its parameters.

// 1st while loop
while(y >= 0) // Ball goes up to (0,100)
{
g.setColor(Background);
// fill the 500 by 500 square with it background color
// any old draw will be covered
g.fillRect(0, 0, 500, 500);
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
for(long i = 1; i < 5000000; i++); //speed of ball
y -=1;
}

// 2nd while loop
while(y <= 400) // ball goes down to 400,100
{
g.setColor(Background);
// fill the 500 by 500 square with it background color
// any old draw will be covered
g.fillRect(0, 0, 500, 500);
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
for(long i = 1; i < 5000000; i++); //speed of ball
y += 1;
}
}
}

最佳答案

我以不同的方式尝试了这个程序。它的工作。对于球的速度,我使用 Thread.sleep() 而不是您使用的循环。另外,更正是代码中的第一个 while 循环将球移回起始位置。为了使其弹跳小于之前的高度,您需要增加 RHS 值(在代码中固定为 0)。还有一件事,为了让球看起来像弹跳,我做了一些改变。

代码如下:

import java.applet.*;
import java.awt.*;

/**<applet code="ball" height = "768" width = "1366" ></applet>*/

public class ball extends Applet implements Runnable
{
int x,y,height,width,a;
float z;
public void start()
{
x=100; y=400; h=50; w=50; a=0;
z = y;
Thread th=new Thread(this);
th.start();
}

public void run()
{
while(Math.round(z)!=0)
{
while(y>=a) //Ball bounces till it reaches point a. Which in turn is increasing with each bounce till it is equal to 400.
{
y--;
repaint();
try{Thread.sleep(3);} // Speed of the ball.
catch(Exception e){}
}
while(y<=400) // This loop gives ball a bouncing look each time it hits the floor (i.e. 400).
{
y++;
repaint();
if(y==400)
{
while(h!=40)
{
y++;
height--;
x--;
width+=2;
repaint();
try{Thread.sleep(3);}
catch(Exception e){}
}
while(h!=50)
{
y--;
height++;
x++;
widt--=2;
repaint();
try{Thread.sleep(3);}
catch(Exception e){}
}
}
try{Thread.sleep(3);}
catch(Exception e){}
}
z=z*(0.7f); //This line and the next line calculates the value of a for the next bounce.
a=Math.round(y-z); //round() function rounds the floating value to nearest whole number and converts it into an integer.
}
}

public void paint(Graphics g)
{
g.fillOval(x,y,width,height);
}
}

关于java - 使用 while 循环弹跳球,我需要将每次弹跳减少 30% 直到 0,然后退出循环以避免无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25776702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com