移动速度不同?

发布于 2024-12-10 21:23:50 字数 1255 浏览 0 评论 0原文

嘿伙计们,在我的 2D 游戏中,移动速度各不相同。我在台式机上制作游戏,运行得很好,但后来我在笔记本电脑上玩,播放器的移动速度比台式机慢。

这是我当前的游戏循环:

    public void gameLoop() throws IOException {

    isRunning = true;


    while(isRunning == true) {

        Graphics2D g2d = (Graphics2D) strategy.getDrawGraphics();
        //main game loop
        //updates everything and draws
        //main componenets
        //e.g. Player
        // clear the screen
        g2d.setColor(Color.lightGray);
        g2d.fillRect(0,0,640,496);

        //drawing
        player.paint(g2d);//paint player
        map.paint(g2d);//paint each wall in wall list

        g2d.dispose();
        strategy.show();

        //update
        try { player.updateMovement(); } catch (Exception e) {};

        try { Thread.sleep(4); } catch (Exception e) {};


    }
}

这是我的player.updateMovement() 方法:

    public void updateMovement() {

    if(!down || !up) {
        ny = 0;
    }

    if(!left || !right) {
        nx = 0;
    }

    if(left) {
        nx = -1;
    }

    if(right) {
        nx = 1;
    }

    if(up) {
        ny = -1;
    }

    if(down) {
        ny = 1;
    }



if ((nx != 0) || (ny != 0)) {
    x += nx;
    y += ny;
    }
}

如何解决此问题?

Hey guys on my 2D game the movement speed varies.. I was making my game on my Desktop and it ran fine but then I went on my laptop and the Player moved slower then the Desktop.

Here is my current game loop:

    public void gameLoop() throws IOException {

    isRunning = true;


    while(isRunning == true) {

        Graphics2D g2d = (Graphics2D) strategy.getDrawGraphics();
        //main game loop
        //updates everything and draws
        //main componenets
        //e.g. Player
        // clear the screen
        g2d.setColor(Color.lightGray);
        g2d.fillRect(0,0,640,496);

        //drawing
        player.paint(g2d);//paint player
        map.paint(g2d);//paint each wall in wall list

        g2d.dispose();
        strategy.show();

        //update
        try { player.updateMovement(); } catch (Exception e) {};

        try { Thread.sleep(4); } catch (Exception e) {};


    }
}

Here is my player.updateMovement() method:

    public void updateMovement() {

    if(!down || !up) {
        ny = 0;
    }

    if(!left || !right) {
        nx = 0;
    }

    if(left) {
        nx = -1;
    }

    if(right) {
        nx = 1;
    }

    if(up) {
        ny = -1;
    }

    if(down) {
        ny = 1;
    }



if ((nx != 0) || (ny != 0)) {
    x += nx;
    y += ny;
    }
}

How can I fix this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2024-12-17 21:23:50

您可以有一个固定速率的绘图循环:每次迭代都应该持续相同的时间,如果还剩下一些时间,则休眠该时间。例如,如果我们固定一个周期为 41.6 毫秒(即 24 fps),并且某个迭代持续 20 毫秒,那么您应该睡眠 41.6 - 20 = 21.6 毫秒。

如果你的代码太重而无法在低端 PC 上运行,那么你可以增加周期,以便每台机器都能处理它。

顺便说一句,您还可以优化您的代码。

您可以在 games.stackexange.com 上找到更多信息

You could have a fixed-rate drawing loop: every iteration should last the same, and if there is some time left, sleep that amount of time. For instance, if we fix a period of 41.6 ms (which is 24 fps), and a certain iteration lasts 20 ms, then you should sleep 41.6 - 20 = 21.6 ms that pass.

If your code is too heavy to run in that time on a low end PC, then you can increase the period so that every machine can cope with it.

By the way, you could also optimize your code.

You can find more information on gaming.stackexange.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文