0

So I'm trying to build my first game loop and yes, I've watched a tutorial on this but can't seem to find what's wrong.

Every time I run my program, the image doesn't show on frame and also, it keeps telling me errors in the render method and where I called the draw method in my Ship class. Please help. (This is my first game project)

This is the Game class which is my main class

package main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{

public static final int WIDTH = 600;
public static final int HEIGHT = 800;

private boolean running = false;
private Thread thread;

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);


Ship ship;

public void init() {
    ship = new Ship(0, 500);

}



private synchronized void start() {
    if (running) {
        return;
    }

    running = true;
    thread = new Thread(this);
    thread.start();


}



private synchronized void stop() {
    if(!running) {
        return;
    }

    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {}

    System.exit(1);
}



// Game loop
public void run() {

    long lastTime = System.nanoTime();
    final double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;

    int updates = 0;
    int frames = 0;
    long timer = System.currentTimeMillis();


while (running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;



        if(delta >= 1) {
            tick();
            updates++;
            delta--;
        }

        //This render method right here tells me it has an error everytime I run the program
        render();

        frames++;

        if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;

            System.out.println("Ticks: "+updates+", Fps: "+frames);
            updates = 0;
            frames = 0;
        }

    }
    stop();
}

// where everything updates 
private void tick() {
    ship.tick();

}

// where everything renders/draws
private void render() {

    BufferStrategy bs = this.getBufferStrategy();

    if(bs == null) {
        createBufferStrategy(3);
        return;

    }

    Graphics g = bs.getDrawGraphics();
    //////////////////////////////////////////////////

    g.setColor(Color.blue);
    g.fillRect(0, 0, getWidth(), getHeight());

    // This has an error too whenever I run the program
    ship.draw(g);







    ////////////////////////////////////////////////
    g.dispose();
    bs.show();



}



public static void main(String[] args) {

    Game game = new Game();

    game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
    game.setMinimumSize(new Dimension(WIDTH, HEIGHT));

    JFrame frame = new JFrame();


    frame.add(game);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    game.start();

}





}

This is the Ship Class

package main;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;



public class Ship {

private int shipX;
private int shipY;

Image character = Toolkit.getDefaultToolkit().createImage("D:\\user's-ship-fast-speed.gif");


public Ship(int x, int y) {
    shipX = x;
    shipY = y;
}


public void tick() {

}

public void draw(Graphics g) {
    g.drawImage(character, shipX, shipY, null);
}
}
4

0 回答 0