我正在做一个游戏,现在我只是在修补图像绘制、按键输入等。现在我所拥有的只是一个用箭头键移动的皮卡丘(游戏与口袋妖怪无关,pikachu只是我选择的图像)我想确保游戏不会因为软件功能而在其他电脑上运行得更快(因为pikachu从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();
}
}
}
1条答案
按热度按时间uoifb46i1#
您通常会看到delta计时或fixed timestep在游戏循环中实现。
增量计时使用
delta
基于测量每帧之间的时间的变量。将所有变换乘以delta
使游戏在多台计算机上以相同的速度运行。这是平滑且易于实现的,但是您必须记住将所有转换乘以delta
.另一方面,固定时间步长以某种方式控制帧之间的时间,从而消除了对增量变量的需要。这是众所周知的有点波折/难以实施,但你不再需要申请
delta
到处都是。虽然您可以自己实现它,但切换到实际的游戏库(如libgdx或slick2d)会更容易,因为它们可以为您处理它(以及更多)。
另外,使用太多线程也可能导致性能不稳定。这个动作可以用一种更干净的方式来完成,同时在一个基本的游戏循环中进行更新;