简体   繁体   English

Mac上的简单2D Java游戏落后

[英]Simple 2D java game lags on mac

I have created a simple top-down 2D java game over the past week and a half. 在过去的一周半时间里,我创建了一个简单的自上而下的2D Java游戏。 It runs perfectly on my windows computer, and a few of my friends have run it fine on their computers as well. 它可以在我的Windows计算机上完美运行,我的一些朋友也可以在他们的计算机上很好地运行它。

Recently, I have tried loading the game up on my mac, but it seems to lag horribly and slows down my whole computer. 最近,我尝试将游戏加载到我的Mac上,但它似乎严重滞后并降低了我的整个计算机的速度。 This only happens when the game is running and returns to normal when I exit it. 这只会在游戏运行时发生,而在我退出游戏时会恢复正常。

I have attached one of my classes below that could cause the problem, although I believe it most likely has something to do with my computer. 尽管我相信它很可能与我的计算机有关,但是我在下面附加了可能导致此问题的我的一个课程。

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 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;
    }
}

It's a problem related with your OS, improve your code following these questions already asked: 这是与您的操作系统有关的问题,请按照已经提出的以下问题来改进代码:

Link 1 链接1

Link 2 连结2

Also try to update your Java version and if possible make your code shorter. 另外,请尝试更新Java版本,并尽可能缩短代码长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM