Java 刺激重力输入编码攻击
问候。我试图制作一个代码,显示一个蓝色的球从顶部落下,反弹向上,每次都降低其最大点,直到它停留在“地板”上。下面的代码不知何故允许球反弹,但无法降低其最大点。这个项目的最终目标是刺激重力环境,如果球每次反弹时都能改变颜色,那就太棒了。感谢您抽出时间:)Java 刺激重力输入编码攻击,java,Java,问候。我试图制作一个代码,显示一个蓝色的球从顶部落下,反弹向上,每次都降低其最大点,直到它停留在“地板”上。下面的代码不知何故允许球反弹,但无法降低其最大点。这个项目的最终目标是刺激重力环境,如果球每次反弹时都能改变颜色,那就太棒了。感谢您抽出时间:) import java.applet.applet; 导入java.awt.BasicStroke; 导入java.awt.Graphics; 导入java.awt.Graphics2D; 导入java.awt.*; 导入静态java.lang.
import java.applet.applet;
导入java.awt.BasicStroke;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.*;
导入静态java.lang.Math.sin;
公共类最终扩展小程序实现可运行{
螺纹t;
int y=0;
INTA=1;
int-h;
//int p=5;
//在init方法中设置AppletViewer的大小
公共void init(){
设置大小(400300);
}
公开作废开始(){
如果(t==null){
t=新线程(此为“新线程”);//在上创建新的边线程
//启动小程序。
t、 start();
}
}
公共停车场(){
如果(t!=null){
t=null;//小程序停止时,创建的线程被销毁。
}
}
//Runnable接口的run()方法的实现。
公开募捐{
Thread t1=Thread.currentThread();
而(t==t1){
重新油漆();
试一试{
Thread.sleep(100);//slepp 100毫秒
}捕获(例外e){
}
}
}
公共空间涂料(图g){
图形2d g2=(图形2d)g;
g、 setColor(Color.BLUE);
g、 椭圆形(100,h,20,20);
System.out.println(“y=“+y”);
System.out.println(“a=“+a”);
//System.out.println(“p=“+p”);
if(y<290){
a=a+1;
y=y+a;
h=y;
}如果(y>290),则为else{
a=a-1;
h=h-a;
y=y+a;
}
//如果(y>=574),则为else{
//a=a+1;
//h=h+a;
// }
}
}
也许可以尝试以下方法:
public class FINAL extends Applet implements Runnable
{
Thread t;
double a = 9.81; // Play with the acceleration
double h = 290.0;
string currDirection = "down"; // Add current direction
double timeMoving = 0.0; // Time in each direction
double speed = 0.0; // Add speed
double maxHeight = 290.0; // Add first maxHeight
// ...
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g.setColor(Color.BLUE);
g.fillOval(100, h, 20, 20);
//System.out.println("p = " +p);
// time is 100ms = 0.1s
timeMoving += 0.1;
if (currDirection == "down")
{
h = 1/2*a*Math.pow(timeMoving,2);
if((maxHeight-h) <= 0)
{
currDirection = "up";
speed += -1 * a*(timeMoving);
timeMoving = 0;
}
}
else if (currDirection == "up")
{
h = speed*timeMoving + 1/2*a*Math.pow(timeMoving,2);
double currentSpeed = speed + a*timeMoving;
if (currentSpeed >= 0)
{
maxHeight = h;
currDirection = "down";
speed = 0;
timeMoving = 0;
}
}
}
}
public类最终扩展Applet实现可运行
{
螺纹t;
双a=9.81;//玩加速游戏
双h=290.0;
字符串currDirection=“down”;//添加当前方向
double timeMoving=0.0;//每个方向的时间
双速=0.0;//增加速度
double maxHeight=290.0;//添加第一个maxHeight
// ...
公共空间涂料(图g)
{
图形2d g2=(图形2d)g;
g、 setColor(Color.BLUE);
g、 椭圆形(100,h,20,20);
//System.out.println(“p=“+p”);
//时间为100ms=0.1s
时间移动+=0.1;
如果(电流方向=“向下”)
{
h=1/2*a*Math.pow(时间移动,2);
如果((最大高度-h)=0)
{
最大高度=h;
currDirection=“向下”;
速度=0;
时间移动=0;
}
}
}
}
你是在找人帮你做物理计算吗?@RobbyCornelissen-nah,我知道动力学方程。我只是想让球像重力一样反弹。我想我知道你的想法,但是“h”是双精度的,由于某种原因,它不能应用于蓝色球的y轴:(如果h
是双精度的,你可以使用h=(int)h;
删除小数部分并将其转换为int
。尽管它仅适用于fillOval()
我猜是函数。我写答案的时候是早上7点,我事先没有睡觉,所以很可能会出现错误。虽然你想在脚本中加入真实的物理,因为这是让球运动感觉真实的原因。如果球落得太快/太慢,请使用加速度a
。是的!现在可以了。非常感谢:)没问题,很高兴能帮忙。
public class FINAL extends Applet implements Runnable
{
Thread t;
double a = 9.81; // Play with the acceleration
double h = 290.0;
string currDirection = "down"; // Add current direction
double timeMoving = 0.0; // Time in each direction
double speed = 0.0; // Add speed
double maxHeight = 290.0; // Add first maxHeight
// ...
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g.setColor(Color.BLUE);
g.fillOval(100, h, 20, 20);
//System.out.println("p = " +p);
// time is 100ms = 0.1s
timeMoving += 0.1;
if (currDirection == "down")
{
h = 1/2*a*Math.pow(timeMoving,2);
if((maxHeight-h) <= 0)
{
currDirection = "up";
speed += -1 * a*(timeMoving);
timeMoving = 0;
}
}
else if (currDirection == "up")
{
h = speed*timeMoving + 1/2*a*Math.pow(timeMoving,2);
double currentSpeed = speed + a*timeMoving;
if (currentSpeed >= 0)
{
maxHeight = h;
currDirection = "down";
speed = 0;
timeMoving = 0;
}
}
}
}