我想将程序限制为某些FPS,否则它将在一台计算机上运行得更快,而在另一台计算机上运行得更慢。
我想要像pygame中一样的东西-FPSClock = pygame.time.Clock()FPSClock.tick(30)
Java 3D中是否有类似的东西?我是Java 3D的初学者,我尝试编写Pong游戏以提高自己的技能。但是,我被困住了,意识到在Java中没有适当的延迟的方法,因为您将不知道最后一帧的时间是什么。
我还发现KeyEvent在我的代码中不起作用。到目前为止,我的代码-
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.awt.event.KeyListener.*;
import java.awt.event.WindowAdapter;
import java.awt.*;
import java.applet.Applet;
public class Pong implements KeyListener {
static float ballx = 0.0f;
static float bally = 0.0f;
static float ballz = 0.0f;
static Transform3D transform2 = new Transform3D();
static TransformGroup transformGroup2 = new TransformGroup();
public static void main(String[] args) {
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group = new BranchGroup();
Appearance appearance = new Appearance();
Color3f color = new Color3f(0.1f, 1.8f, 0.1f);
ColoringAttributes coloringAttributes = new ColoringAttributes(color, ColoringAttributes.NICEST);
appearance.setColoringAttributes(coloringAttributes);
Box box = new Box(0.05f, 0.2f, 0.03f, appearance);
Transform3D transform = new Transform3D();
TransformGroup transformGroup = new TransformGroup();
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Vector3f vector = new Vector3f(-0.9f, 0.0f, 0.0f);
transform.setTranslation(vector);
transformGroup.setTransform(transform);
transformGroup.addChild(box);
Box box1 = new Box(0.05f, 0.2f, 0.03f, appearance);
Transform3D transform1 = new Transform3D();
TransformGroup transformGroup1 = new TransformGroup();
transformGroup1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Vector3f vector1 = new Vector3f(0.9f, 0.0f, 0.0f);
transform1.setTranslation(vector1);
transformGroup1.setTransform(transform1);
transformGroup1.addChild(box1);
group.addChild(transformGroup);
Sphere sphere = new Sphere(0.1f);
group.addChild(transformGroup1);
Vector3f vector2 = new Vector3f(0.0f,0.0f,0.0f);
transform2.setTranslation(vector2);
transformGroup2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transformGroup2.setTransform(transform2);
transformGroup2.addChild(sphere);
Color3f lightColor = new Color3f(1.8f, 1.9f, 0.1f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
Vector3f lightDirection = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light = new DirectionalLight(lightColor, lightDirection);
light.setInfluencingBounds(bounds);
transformGroup2.addChild(light);
group.addChild(transformGroup2);
Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
transformGroup2.addChild(ambientLightNode);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(group);
}
public void keyPressed(KeyEvent event) {
if (event.getKeyChar() == 'a') {
ballx = ballx 0.1f;
transform2.setTranslation(new Vector3f(ballx, bally, ballz));
transformGroup2.setTransform(transform2);
}
}
public void keyReleased(KeyEvent event) {
}
public void keyTyped(KeyEvent event) {
}
}
Java当然不是我的技能领域,但是从以前在Java中进行一些游戏编程开始,我相信您必须编写自己的游戏循环来控制游戏的FPS。因此,要设置自定义FPS,您必须编写一个游戏循环。我发现非常有用的文章是http://www.java-gaming.org/index.php?topic=24220.0。它解释了哪些循环是不好的做法,并显示了良好的游戏循环的示例。
编辑:针对您的Key方法问题,我相信您必须在游戏循环中调用这些方法,以便它们可以不断检查键的状态。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句