0
私は過去1週間半の単純なトップダウンの2D Javaゲームを作成しました。それは私のWindowsコンピュータ上で完璧に動作し、私の友人のいくつかは彼らのコンピュータでそれをうまく実行しました。単純な2D Javaゲームの遅れは
最近、私はMacでゲームを読み込もうとしましたが、ひどく遅れているように見え、コンピュータ全体が遅くなります。これはゲームの実行中にのみ発生し、終了すると正常に戻ります。
私のクラスには問題が発生する可能性がありますが、それは自分のコンピュータと関係がありそうです。
public class Game implements Runnable {
//DISPLAY
private Display display;
private int width, height;
public String title;
//THREADS
private boolean running = false;
private Thread thread;
//BUFFERING AND HRAPHICS
private BufferStrategy bs;
private Graphics g;
// Statess
public States gameState, island;
private States menuState;
// Input
private KeyManager keyManager;
private MouseManager mouseManager;
//UI
// Handler
private Handler handler;
//TIMER
Timer myTimer = new Timer();
public static int seconds = 0;
int count = 0;
//PAUSE
private boolean paused = false;
public Game(String title, int width, int height) {
this.width = width;
this.height = height;
this.title = title;
keyManager = new KeyManager();
mouseManager = new MouseManager();
}
private void init() {
display = new Display(title, width, height);
display.getFrame().addKeyListener(keyManager);
display.getFrame().addMouseListener(mouseManager);
display.getFrame().addMouseMotionListener(mouseManager);
display.getCanvas().addMouseListener(mouseManager);
display.getCanvas().addMouseMotionListener(mouseManager);
Assets.init();
handler = new Handler(this);
gameState = new GameState(handler, 9, 79, 1502, 79);
island = new Island(handler, 20, 190, 1450, 190);
menuState = new MenuState(handler);
States.setState(menuState);
}
private void tick() {
keyManager.tick();
setPaused();
if(!paused){
States.getState().tick();
if(count == 0){
if(States.getState() != menuState){
myTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
seconds++;
}
}, 1000, 1000);
count = 1;
}
}
//System.out.println(seconds);
}
render();
}
public void paused(Graphics g){
g.drawString("Paused", 750, 200);
}
private void render() {
bs = display.getCanvas().getBufferStrategy();
if (bs == null) {
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
// Clear Screen
g.clearRect(0, 0, width, height);
// Draw Here!
//Rendering
//State render
States.getState().render(g);
//UI
if(States.getState() != menuState){
if(paused)
paused(g);
}
// End Drawing!
bs.show();
g.dispose();
}
public void run() {
init();
int fps = 60;
double timePerTick = 1000000000/fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
long timer = 0;
//int ticks = 0;
while (running) {
now = System.nanoTime();
delta += (now - lastTime)/timePerTick;
timer += now - lastTime;
lastTime = now;
if (delta >= 1) {
tick();
//ticks++;
delta--;
}
if (timer >= 1000000000) {
//ticks = 0;
timer = 0;
}
}
stop();
}
public States getGameState(){
return gameState;
}
public KeyManager getKeyManager() {
return keyManager;
}
public MouseManager getMouseManager() {
return mouseManager;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
//TIMER
public synchronized void start() {
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if (!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public boolean isPaused() {
return paused;
}
public void setPaused() {
if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_U))
paused = !paused;
}
}
Display.java
public class Display {
private String title;
private int height;
private int width;
private Canvas canvas;
private JFrame frame;
public Display(String title, int width, int height) {
this.title = title;
this.height = height;
this.width = width;
createDisplay();
}
public void createDisplay() {
// Make frame
frame = new JFrame(title);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// canvas to draw graphics
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
canvas.setFocusable(false);
// Display canvas through jframe
frame.add(canvas);
frame.pack();
}
public Canvas getCanvas(){
return canvas;
}
public JFrame getFrame(){
return frame;
}
}
可読性を向上させる –