java - 创建游戏循环线程

标签 java game-loop

嗨,我刚刚搞乱了多边形和 awt。我创建了一个 Jframe,可以绘制多边形,并让其中一个通过按键移动。

我想知道如何启动一个游戏循环线程(以及将其放在哪里!)来独立更新 jframe。

通过谷歌搜索,我相信我应该有一个用于用户输入的线程,一个用于游戏本身的线程。

目前我已经在板类上实现了 KeyListener(代码如下所示),我应该将其放入自己的类中并使其实现可运行吗?就代码而言,我在 keypressed() 方法中重新绘制了 JFrame所以我可以看到它正确移动

一天的大部分时间都在做这件事,我自己非常非常困惑:) 一如既往,非常感谢任何帮助!

另外,当我从在线教程中学习它时,我应该使用 JPanel 而不是 JFrame 并使用 PaintComponent() 而不是 Paint() 吗?

    import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;

import javax.swing.JFrame;

public class Board extends JFrame implements KeyListener{
    AffineTransform identity = new AffineTransform();
    Graphics2D g2d;

    Ship ship = new Ship();

    public static final int ALIENS = 3;
    Alien[] alien = new Alien[ALIENS];


    Board(){
        super("The Board");
        setSize(1280,1024);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setBackground(Color.BLACK);
        for(int x=0;x<ALIENS;x++){
            alien[x]=new Alien();
        }
    }

    public void paint(Graphics g){
        super.paint(g);
        addKeyListener(this);
        //draw ship
        g2d = (Graphics2D)g;    
        g2d.setTransform(identity);
        g2d.translate(ship.getxPos(),ship.getyPos());
        g2d.scale(2,2);
        g2d.setColor(Color.ORANGE);
        g2d.fill(ship.getShape());
        g2d.setColor(Color.BLACK);
        g2d.draw(ship.getShape());

        // draw aliens
        for(int x=0;x<ALIENS;x++){
            //if alien alive
            if(alien[x].isAlive()){
                //draw alien
                g2d = (Graphics2D)g;    
                g2d.setTransform(identity);
                g2d.translate(alien[x].getxPos(),alien[x].getyPos());
                g2d.scale(2,2);
                g2d.setColor(Color.BLUE);
                g2d.fill(alien[x].getShape());
                g2d.setColor(Color.BLACK);
                g2d.draw(alien[x].getShape());
            }
        }
    }//end paint


    /*****************************************************
     * key listener events
     *****************************************************/
    public void keyReleased(KeyEvent k) { }
    public void keyTyped(KeyEvent k) { }
    public void keyPressed(KeyEvent k) {
        int keyCode = k.getKeyCode();

        switch (keyCode) {

        case KeyEvent.VK_A:
            //move ship left
            if(ship.getxPos()<20){
                ship.setxPos(20);
            }else
            ship.setxPos(ship.getxPos()-1);
            break;
        case KeyEvent.VK_D:
            if(ship.getxPos()>1260){
                ship.setxPos(1260);
            }else
            ship.setxPos(ship.getxPos()+1);
        }
       repaint();
    }//end keypressed event

    public static void main(String[] args){
        new Board();
    }
}

最佳答案

这些答案在一定程度上取决于您想要创建的游戏类型。

From googling I'm led to believe that I should have one thread for user input and one for the game itself.

您创建一个主游戏循环,它在自己的线程中运行。在伪代码中

while (running) {
    update game model
    draw game
    wait x milliseconds
} 

您的用户输入将直接更新游戏模型。如果计算机需要采取行动或对您的行动使用react,游戏循环会更新游戏模型。然后,游戏循环读取游戏模型并根据模型中的值绘制游戏。

At the moment I have implemented KeyListener on the board class(code shown below),should I put that out into its own class and make it implement runnable?

是的,您应该将 KeyListener 放入其自己的类中。不,您不必将其设为单独的线程。

为了避免将来的麻烦,您的 Swing 组件应该在事件调度线程上定义和使用。

具体操作方法如下。

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

Should I use JPanel instead of JFrame and paintComponent() instead of paint()?

是的。

您应该在 JFrame 内有一个 JPanel。 JPanel 是您使用paintComponent 方法执行绘图游戏伪代码的地方。

有些人可能会不同意我的观点,但我发现如果游戏中的每个对象都有一个绘制自身的绘制方法,那就最好了。

public void draw(Graphics g)

游戏模型还有一个绘制方法,用于绘制模型中的所有对象。

JPanel PaintComponent 方法如下所示:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    gameModel.draw(g);
}

关于java - 创建游戏循环线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15120128/

相关文章:

java - 需要帮助将 DCGAN 转换为 Java for Tensorflow for Java

java - 在字符串中查找重复模式

Java 循环内循环

java - 重置游戏循环/重新启动游戏 java swing

java - Swing 动画暂停和恢复

java - 'extends Object' 有目的还是多余的?

java - zeromq 与 Python 与 Java 的 node.js 性能

javascript - 为什么 setTimeout 游戏循环在 touchstart 事件触发时出现延迟?

c++ - std::chrono 不同的结果 - 固定时间步长循环

java - 有没有办法在游戏循环中进行随机滴答?