简体   繁体   English

简单播放器不会在Java 2D游戏中显示

[英]Simple player will not show in Java 2D game

I have having issues making my player show up. 我的播放器出现问题。 I created the Player class and implemented its draw() method in the render function of the game loop but there is no player. 我创建了Player类,并在游戏循环的render函数中实现了其draw()方法,但没有玩家。 No errors either. 也没有错误。 Can someone help me find out why the player is not showing? 有人可以帮我找出播放器未显示的原因吗?

Also there is a border on the left and bottom of the window and I cannot get rid of it. 窗口的左侧和底部也有一个边框,我无法摆脱它。 Does someone know how? 有人知道吗?

here is the game class 这是游戏课

@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable {

    private static Game _instance;
    private static final String TITLE = "ProjectG";
    public static final int WIDTH = 240;
    public static final int HEIGHT = WIDTH * 3 / 4;
    private static final int SCALE = 4;
    public static final Dimension SIZE = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);

    private static final int UPDATE_RATE = 60;
    private static final int RENDER_RATE = 60;

    private JFrame _frame;
    private Thread _thread;
    private boolean _running = false;

    private int _tps = 0;
    private int _fps = 0;
    private int _totalTicks = 0;

    private BufferedImage image;
    private Graphics2D g;

    private Player player;

    public Game() {
        _instance = this;

        setPreferredSize(SIZE);
        setMinimumSize(SIZE);
        setMaximumSize(SIZE);

        _frame = new JFrame(TITLE);
        _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _frame.setLayout(new BorderLayout());
        _frame.add(_instance, BorderLayout.CENTER);
        _frame.pack();

        _frame.setResizable(false);
        _frame.setLocationRelativeTo(null);
        _frame.setVisible(true);

        player = new Player();
    }

    public synchronized void start() {
        _running = true;
        _thread = new Thread(this, TITLE + "_main");
        _thread.start();
    }

    public synchronized void stop() {
        _running = false;

        if (_thread != null) {
            try {
                _thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void run() {
        double lastUpdateTime = System.nanoTime();
        double lastRenderTime = System.nanoTime();

        final int ns = 1000000000;
        final double nsPerUpdate = (double) ns / UPDATE_RATE;
        final double nsPerRender = (double) ns / RENDER_RATE;
        final int maxUpdatesBeforeRender = 1;

        int lastSecond = (int) lastUpdateTime / ns;
        int tickCount = 0;
        int renderCount = 0;

        image = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);
        g = (Graphics2D) image.getGraphics();

        while (_running) {
            long currTime = System.nanoTime();
            int tps = 0;

            while ((currTime - lastUpdateTime) > nsPerUpdate && tps < maxUpdatesBeforeRender) {
                update();
                tickCount++;
                _totalTicks++;
                tps++;
                lastUpdateTime += nsPerUpdate;
            }

            if (currTime - lastUpdateTime > nsPerUpdate) {
                lastUpdateTime = currTime - nsPerUpdate;
            }

            float interpolation = Math.min(1.0F, (float) ((currTime - lastUpdateTime) / nsPerUpdate));
            render(interpolation);
            draw();
            renderCount++;
            lastRenderTime = currTime;

            int currSecond = (int) (lastUpdateTime / ns);
            if (currSecond > lastSecond) {
                _tps = tickCount;
                _fps = renderCount;
                tickCount = 0;
                renderCount = 0;
                lastSecond = currSecond;
                System.out.println(_tps + " TPS " + _fps + " FPS");
            }

            while (currTime - lastRenderTime < nsPerRender && currTime - lastUpdateTime < nsPerUpdate) {
                Thread.yield();
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                currTime = System.nanoTime();
            }
        }
    }

    public void update() {
        player.update();
    }

    public void render(float interpolation) {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
        }

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE);
        g.setColor(Color.BLACK);
        g.drawString("TPS: " + _fps + " FPS: " + _fps, 10, 20);

        player.draw(g);
    }

    public void draw() {
        Graphics g2 = this.getGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
    }
}

and here is the player class 这是球员课

public class Player {

    private int x;
    private int y;
    private int r;

    private int dx;
    private int dy;
    private int speed;

    private boolean left;
    private boolean right;
    private boolean up;
    private boolean down;

    private int lives;

    public Player() {
        x = Game.WIDTH / 2;
        y = Game.HEIGHT / 2;

        dx = 0;
        dy = 0;
        speed = 5;
        lives = 3;
    }

    public void update() {
        if (left) {
            dx = -speed;
        } else if (right) {
            dx = speed;
        } else if (up) {
            dy = -speed;
        } else if (down) {
            dy = speed;
        }

        x += dx;
        y += dy;

        if (x < r)
            x = r;
        if (y < r)
            y = r;
        if (x > Game.WIDTH - r)
            x = Game.WIDTH - r;
        if (y > Game.HEIGHT - r)
            y = Game.HEIGHT - r;

        dx = 0;
        dy = 0;
    }

    public void draw(Graphics2D g) {

        g.setColor(Color.BLACK);
        g.fillOval(x - r, y - r, 2 * r, 2 * r);

        g.setStroke(new BasicStroke(3));
        g.setColor(Color.BLACK.brighter());
        g.drawOval(x - r, y - r, 2 * r, 2 * r);
        g.setStroke(new BasicStroke(1));
    }

    public int getLives() {
        return lives;
    }
}

and here is the Main class if needed 这是主班,如果需要的话

public class Main {

    private static Game _game;

    public static void main(String[] args) {
        _game = new Game();
        _game.start();
    }
}

If you never initialize r it will default to 0 , and lines like the following. 如果您从不初始化r ,它将默认为0 ,并且如下所示。

g.drawOval(x - r, y - r, 2 * r, 2 * r);

won't actually draw anything. 实际上不会画任何东西。

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

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