游戏速度问题

时间:2010-03-17 15:54:18

标签: java performance

HI ..我做了一个小游戏。但是这个游戏适用于速度不同的每台计算机。我认为这是关于分辨率。我使用paintcomponent中的所有东西。如果我改变屏幕尺寸,游戏会变慢或变快。如果我在具有不同分辨率的另一台计算机上运行此游戏,它也会有所不同。 这是我的游戏 http://rapidshare.com/files/364597095/ShooterGame.2.6.0.jar

和这里的代码

public class Shooter extends JFrame implements KeyListener, Runnable {

JFrame frame = new JFrame();
String player;
Font startFont, startSubFont, timerFont,healthFont;
Image img;
Image backGround;
Graphics dbi;
URL url1 = this.getClass().getResource("Images/p2.gif");
URL url2 = this.getClass().getResource("Images/p3.gif");
URL url3 = this.getClass().getResource("Images/p1.gif");
URL url4 = this.getClass().getResource("Images/p4.gif");
URL urlMap = this.getClass().getResource("Images/zemin.jpg");
Player p1 = new Player(5, 150, 10, 40, Color.GREEN, url3);
Computer p2 = new Computer(750, 150, 10, 40, Color.BLUE, url1);
Computer p3 = new Computer(0, 0, 10, 40, Color.BLUE, url2);
Computer p4 = new Computer(0, 0, 10, 40, Color.BLUE, url4);
ArrayList<Bullets> b = new ArrayList<Bullets>();
ArrayList<CBullets> cb = new ArrayList<CBullets>();
Thread sheap;
boolean a, d, w, s;
boolean toUp, toDown;
boolean GameOver;

boolean Level2;
boolean newGame, resart, pause;
int S, E;
int random;
int cbSpeed = 0;
long timeStart, timeEnd;
int timeElapsed;
long GameStart, GameEnd;
int GameScore;
int Timer = 0;
int timerStart, timerEnd;

public Shooter() {


    sheap = new Thread(this);
    sheap.start();
    startFont = new Font("Tiresias PCFont Z", Font.BOLD + Font.ITALIC, 32);
    startSubFont = new Font("Tiresias PCFont Z", Font.BOLD + Font.ITALIC, 25);
    timerFont = new Font("Tiresias PCFont Z", Font.BOLD + Font.ITALIC, 16);
     healthFont = new Font("Tiresias PCFont Z", Font.BOLD + Font.ITALIC, 16);
    setTitle("Shooter 2.5.1");
    setBounds(350, 250, 800, 600);

    // setResizable(false);
    setBackground(Color.black);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addKeyListener(this);
    a = d = w = s = false;
    toUp = toDown = true;
    GameOver = true;
    newGame = true;
    Level2 = false;
    S = E = 0;

    setVisible(true);

}

public void paint(Graphics g) {

    img = createImage(getWidth(), getHeight());
    dbi = img.getGraphics();
    paintComponent(dbi);
    g.drawImage(img, 0, 0, this);

}

public void paintComponent(Graphics g) {
   repaint();
    timeStart = System.currentTimeMillis();
    backGround = Toolkit.getDefaultToolkit().getImage(urlMap);
    g.drawImage(backGround, 0, 0, null);
    g.setColor(Color.red);
   g.setFont(healthFont);
    g.drawString("" + player + " Health : " + p1.health, 30, 50);
    g.setColor(Color.red);
    g.drawString("Computer Health : " + (p2.health + p3.health + p4.health), 600, 50);
    g.setColor(Color.BLUE);
    g.setFont(timerFont);
    g.drawString("Time  : " + Timer, 330, 50);
    if (newGame) {

        g.setColor(Color.LIGHT_GRAY);
        g.setFont(startFont);
        g.drawString("Well Come To Shoot Game", 200, 190);
        g.drawString("Press ENTER To Start", 250, 220);
        g.setColor(Color.LIGHT_GRAY);
        g.setFont(startSubFont);
        g.drawString("Use W,A,S,D and Space For Fire", 200, 250);
        g.drawString("GOOD LUCK", 250, 280);

       newGame();

    }
    if (!GameOver) {

        for (Bullets b1 : b) {

            b1.draw(g);
        }
        for (CBullets b2 : cb) {

            b2.draw(g);

        }
        update(); // Here MOvements for Player and For Fires
    }
    if (p1.health <= 0) {
        g.setColor(p2.col);
        g.setFont(startFont);
        g.drawString("Computer Wins ", 200, 190);
        g.drawString("Press Key R  to Restart ", 200, 220);
        GameOver = true;

    } else if (p2.health <= 0 && p3.health <= 0 && p4.health <= 0) {

        g.setColor(p1.col);
        g.setFont(startFont);
        g.drawString(""+player+" Wins ", 200, 190);
        g.drawString("Press Key R  to Resart ", 200, 220);
        GameOver = true;


        g.setColor(Color.MAGENTA);
        g.drawString(""+player+"`s  Score is " + Timer, 200, 120);


    }

    if (Level2) {
        if (p3.health >= 0) {
            p3.draw(g);

            for (CBullets b3 : cb) {
                b3.draw(g);
            }
        } else {
            p3.x = 1000;
        }

        if (p4.health >= 0) {
            p4.draw(g);

            for (CBullets b4 : cb) {
                b4.draw(g);
            }
        } else {
            p4.x = 1000;
        }
    }
    if (p1.health >= 0) {
        p1.draw(g);

    }
    if (p2.health >= 0) {
        p2.draw(g);
    } else {
        p2.x = 1000;
    }

}

public void update() {


    if (w && p1.y > 54) {
        p1.moveUp();
    }

    if (s && p1.y < 547) {
        p1.moveDown();
    }
    if (a && p1.x > 0) {
        p1.moveLeft();
    }

    if (d && p1.x < 200) {
        p1.moveRight();
    }
    random = 1 * (int) (Math.random() * 100);

    if (random > 96) {
        if (p2.health >= 0) {
            CBullets bo = p2.getCBull();

            bo.xVel =-1-cbSpeed;

            cb.add(bo);
        }
        if (Level2) {
            if (p3.health >= 0) {
                CBullets bo1 = p3.getCBull();
                bo1.xVel = -2-cbSpeed;
                cb.add(bo1);
            }
            if (p4.health >= 0) {
                CBullets bo2 = p4.getCBull();
                bo2.xVel = -4-cbSpeed;
                cb.add(bo2);
            }
        }

    }

    if (S == 1) {

        if (p1.health >= 0) {
            Bullets bu = p1.getBull();
            bu.xVel = 5;
            b.add(bu);
            S += 1;
        }
    }

    //Here Also Problem .. When COmputer have More fire then it gaves Array Exeption . Or Player have More Fire
for (int i = cb.size() -1; i >= 0 ; i--) {
        boolean bremoved = false;
    for (int j = b.size() -1 ; j >=0 ; j--) {
        if (b.get(j).rect.intersects(cb.get(i).rect) ||
            cb.get(i).rect.intersects(b.get(j).rect)) {
              bremoved = true;
            b.remove(j);
        }
    }
    if(bremoved)
        cb.remove(i);
}






    for (int i = 0; i < b.size(); i++) {
        b.get(i).move();

        if (b.get(i).rect.intersects(p2.rect)) {

            if (p2.health >= 0) {
                p2.health--;

                b.remove(i);
           //     System.out.println("Hited P2");
                i--;

                continue;
            }
        }

        if (b.get(i).rect.intersects(p3.rect)) {
            if (p3.health >= 0) {
                p3.health--;

                b.remove(i);
             //   System.out.println("Hited P3");
                i--;

                continue;
            }
        }
        if (b.get(i).rect.intersects(p4.rect)) {
            if (p4.health >= 0) {
                p4.health--;

                b.remove(i);
             //   System.out.println("Hited P4");
                i--;

                continue;
            }
        }
        if (b.get(i).rect.x > 790) {
            b.remove(i);

        }
    }

    for (int j = 0; j < cb.size(); j++) {
        cb.get(j).move();

        if (cb.get(j).rect.intersects(p1.rect) && cb.get(j).xVel < 0) {

            p1.health--;
            cb.remove(j);
            j--;
            continue;
        }
    }

    timeEnd = System.currentTimeMillis();
    timeElapsed = (int) (timeEnd - timeStart);
}

public void level2() {

    if (p2.health <= 10) {

        Level2 = true;
        cbSpeed = 4;
        p3.x = 750;
        p4.x = 750;

        p2.speed = 10;
        p3.speed = 20;
        p4.speed = 30;
    }
}

public void keyTyped(KeyEvent e) {
}

public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()) {
        case KeyEvent.VK_ENTER:
            newGame = false;

            break;
        case KeyEvent.VK_P:
            pause = true;

            break;
        case KeyEvent.VK_R:
            resart = true;

            break;
        case KeyEvent.VK_A:
            a = true;

            break;

        case KeyEvent.VK_D:
            d = true;
            break;
        case KeyEvent.VK_W:
            w = true;
            break;
        case KeyEvent.VK_S:
            s = true;
            break;

        case KeyEvent.VK_SPACE:
            S += 1;

            break;
    }
}

public void keyReleased(KeyEvent e) {
    switch (e.getKeyCode()) {

        case KeyEvent.VK_A:
            a = false;
            break;
        case KeyEvent.VK_D:

            d = false;
            break;
        case KeyEvent.VK_W:
            w = false;
            break;
        case KeyEvent.VK_S:
            s = false;
            break;

        case KeyEvent.VK_SPACE:
            S = 0;

            break;

    }
}

public void newGame() {

    p1.health = 20;
    p2.health = 20;
    p3.health = 20;
    p4.health = 20;
    p3.x = 0;
    p4.x = 0;
    p2.x = 750;

    Level2 = false;
    cbSpeed = 0;
    p2.speed = 9;
    b.removeAll(b);
    cb.removeAll(cb);

    timerStart = (int) System.currentTimeMillis();

    GameOver = false;

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            KeyListener k = new Shooter();
        }
    });

}

@Override
public void run() {
    player = JOptionPane.showInputDialog(frame, "Enter Player Name", "New Player", JOptionPane.DEFAULT_OPTION);

    while (true) {

        timerEnd = (int) System.currentTimeMillis();

        if (resart) {
            newGame();
            resart = false;
        }
        if (pause) {
            Thread.currentThread().notify();
        }
        try {

            if (!GameOver) {
Timer = timerEnd - timerStart;
                level2();
                if (p1.y < p2.y && p2.y>60) {
                    p2.moveUp();
                }
                if (p1.y < p3.y && p3.y>43) {
                    p3.moveUp();
                }
                if (p1.y < p4.y && p4.y>43) {
                    p4.moveUp();

                }
                if (p1.y > p2.y && p2.y<535) {
                    p2.moveDown();
                }
                if (p1.y > p3.y && p3.y<535) {
                    p3.moveDown();
                }
                if (p1.y > p4.y && p4.y<530) {
                    p4.moveDown();
                }

            }
            if (timeElapsed < 125) {
                Thread.currentThread().sleep(125);
            }
        } catch (InterruptedException ex) {
            System.out.print("FInished");

        }

    }

}

}

4 个答案:

答案 0 :(得分:4)

很难从你的挥霍中分辨出来,但我会继续前进然后回答:问题似乎是你不丢帧;只是拖延他们。基本上,你需要将你的游戏逻辑与你的绘图逻辑分开,并绘制尽可能多的帧。这两个应该在不同的线程上,但话说再次,线程是邪恶的。

答案 1 :(得分:3)

您正在寻找的主题是“基于时间的运动”。我没有阅读您的源代码,但您描述的行为看起来像基于帧的移动是问题所在。

这是一个简短的教程,涵盖了基于时间的运动: http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial10.php

这篇文章还更详细地介绍了这个问题,重点关注物理学,但也适用于相当简单的游戏: http://gafferongames.com/game-physics/fix-your-timestep/

编辑:正如另一个答案所述,分离渲染和游戏逻辑会有很大帮助。

答案 2 :(得分:0)

您需要跟踪自上次“勾号”(可能是屏幕重绘)以来的毫秒数,并且只在经过一定时间后更新您的世界状态(例如位置)。否则你的游戏将以屏幕重绘的速度运行,这是不一致的。

假设您希望以25 fps的速度运行,这意味着您应该每秒最多更新游戏状态25次,即每40秒。

答案 3 :(得分:0)

HI再次..我对我的游戏进行了一些修改,比如在线程中绘制和移动对象。现在它在每个计算机的相同速度上工作。如果我改变大小。但问题是我使用了J3DTimer.getValue()。这个库我没有加载Java3d。问题是如果在机器上没有这个库或安装我的游戏不起作用。我怎么能解决这个问题?我应该说每个人都设置Java#d? :))另外我使用了standart System.currentTimeMillis();但现在我的游戏工作很慢......有什么想法?