我正在制作游戏,而现在我只是在修补图像,键输入等等。现在我所拥有的只是皮卡丘用箭头键移动(游戏与口袋妖怪无关,皮卡丘只是我选择的形象)我想确保游戏因其软件功能而在其他计算机上运行速度不快(如皮卡丘那样从A点到B点比另一台计算机更快,而不仅仅是有点像其他游戏一样滞后)
我相信我做得对,因为我听说没有把引擎绑在框架上,框架正在做的唯一事情是显示它的X,Y等。其他一切都是在框架之外完成的。
这是我的代码:
package net.trenterprises;
import java.awt.MouseInfo;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Main implements KeyListener {
static JLabel LabelImage = null;
static ArrayList<String> KeysPressed = new ArrayList<String>();
static int UP = 38;
static int DOWN = 40;
static int LEFT = 37;
static int RIGHT = 39;
public static void main(String[] args) {
Window TestWindow = new Window("Mouse Location", null);
KeyListener listener = new Main();
TestWindow.addKeyListener(listener);
TestWindow.setLayout(null);
int MouseX = 0;
int MouseY = 0;
JLabel MouseLabel = new JLabel("???");
MouseLabel.setSize(MouseLabel.getPreferredSize());
TestWindow.add(MouseLabel);
try {
BufferedImage CUPCAKES = (ImageIO.read(new URL("http://img3.wikia.nocookie.net/__cb20140328192412/pokemon/images/thumb/0/0d/025Pikachu.png/200px-025Pikachu.png")));
LabelImage = new JLabel(new ImageIcon(CUPCAKES));
TestWindow.add(LabelImage);
LabelImage.setSize(LabelImage.getPreferredSize());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LabelImage.setBounds(0, 0, LabelImage.getSize().width, LabelImage.getSize().height);
new PikaMoveUp().start();
new PikaMoveDown().start();
new PikaMoveLeft().start();
new PikaMoveRight().start();
while(true) {
MouseX = (MouseInfo.getPointerInfo().getLocation().x);
MouseY = (MouseInfo.getPointerInfo().getLocation().y);
MouseLabel.setText("Mouse X: " + MouseX + "\n Mouse Y: " + MouseY);
MouseLabel.setSize(MouseLabel.getPreferredSize());
MouseLabel.setBounds(0, 0, MouseLabel.getSize().width, MouseLabel.getSize().height);
}
}
public static int getPikaX() {
return LabelImage.getX();
}
public static int getPikaY() {
return LabelImage.getY();
}
public static void setPikachuPosition(int x, int y) {
LabelImage.setBounds(x, y, LabelImage.getSize().width, LabelImage.getSize().height);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int Char = (int) e.getKeyChar();
int Keycode = (int) e.getKeyCode();
if(Char == 65535) {
if(Keycode == UP) {
if(!KeysPressed.contains(Keycode + "")) {
KeysPressed.add(Keycode + "");
}
}
if(Keycode == DOWN) {
if(!KeysPressed.contains(Keycode + "")) {
KeysPressed.add(Keycode + "");
}
}
if(Keycode == LEFT) {
if(!KeysPressed.contains(Keycode + "")) {
KeysPressed.add(Keycode + "");
}
}
if(Keycode == RIGHT) {
if(!KeysPressed.contains(Keycode + "")) {
KeysPressed.add(Keycode + "");
}
}
}
}
@Override
public void keyReleased(KeyEvent e) {
int Char = (int) e.getKeyChar();
int Keycode = (int) e.getKeyCode();
if(Char == 65535) {
if(Keycode == UP) {
while(KeysPressed.contains(Keycode + "")) {
KeysPressed.remove(Keycode + "");
}
}
if(Keycode == DOWN) {
while(KeysPressed.contains(Keycode + "")) {
KeysPressed.remove(Keycode + "");
}
}
if(Keycode == LEFT) {
while(KeysPressed.contains(Keycode + "")) {
KeysPressed.remove(Keycode + "");
}
}
if(Keycode == RIGHT) {
while(KeysPressed.contains(Keycode + "")) {
KeysPressed.remove(Keycode + "");
}
}
}
}
class PikaMoveUp extends Thread implements Runnable {
public void run() {
while(true) {
try {
while(Main.KeysPressed.contains(Main.UP + "")) {
Thread.sleep(5);
Main.setPikachuPosition(Main.getPikaX(), Main.getPikaY() - 1);
}
}
catch(Exception E) {
E.printStackTrace();
}
}
}
}
class PikaMoveDown extends Thread implements Runnable {
public void run() {
while(true) {
try {
while(Main.KeysPressed.contains(Main.DOWN + "")) {
Thread.sleep(5);
Main.setPikachuPosition(Main.getPikaX(), Main.getPikaY() + 1);
}
}
catch(Exception E) {
E.printStackTrace();
}
}
}
}
class PikaMoveLeft extends Thread implements Runnable {
public void run() {
while(true) {
try {
while(Main.KeysPressed.contains(Main.LEFT + "")) {
Thread.sleep(5);
Main.setPikachuPosition(Main.getPikaX() - 1, Main.getPikaY());
}
}
catch(Exception E) {
E.printStackTrace();
}
}
}
}
class PikaMoveRight extends Thread implements Runnable {
public void run() {
while(true) {
try {
while(Main.KeysPressed.contains(Main.RIGHT + "")) {
Thread.sleep(5);
Main.setPikachuPosition(Main.getPikaX() + 1, Main.getPikaY());
}
}
catch(Exception E) {
E.printStackTrace();
}
}
}
答案 0 :(得分:1)
您通常会在游戏循环中看到delta-timing或fixed-timestep。
Delta计时使用基于测量每帧之间时间的delta
变量。您可以将所有转换乘以delta
,以使游戏在多台计算机上以相同的速度运行。这很顺利且易于实现,但您必须记住将{strong>所有转换乘以delta
。
delta
。
虽然你可以自己实现它,但是更容易切换到像LibGDX或Slick2D这样的实际游戏库,它们可以为你处理它(以及很多更多)。
此外,使用如此多的线程也可能导致性能不稳定。在基本游戏循环中更新时,可以以更清洁的方式完成移动;
if(Main.KeysPressed.contains(Main.LEFT + ""))
Main.setPikachuPosition(Main.getPikaX() - 1, Main.getPikaY());