砖阵列没有形成

时间:2017-03-01 21:47:07

标签: java arrays

我在我的12年级编程课程中制作的Brick Breaker游戏遇到了麻烦。

目前,似乎只有Bricks.render中的砖块才能绘制。当我输出砖的X和Y应该是什么时,它将每块砖放在正确的位置。所以我相信我错了。

MCVE:

Game.java

package main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable {

// variables
private static final long serialVersionUID = 1L;

public static final int WIDTH = 480, HEIGHT = 600, SCALE = 1;
public final String TITLE = "Brick Breaker";

private static boolean running = false;
private Thread thread;
private int fpsDisplay = 0;

Bricks[] level1 = new Bricks[16];

// creates a new Game (which creates the game window)
public static void main(String[] args) {
    new Game();
}

public Game() {
    new GameWindow(WIDTH, HEIGHT, SCALE, TITLE, this);
    System.out.println("[" + TITLE + " has started.]");
}

// called in GameWindow, starts the thread and game loop
public synchronized void start() {
    if (running)
        return;
    thread = new Thread(this);
    thread.start();
    running = true;
}

public void init() {
    this.createlevel1();
}

// used for updating what's happening in the game
private void tick() {

}

// renders the graphics
private void render() {
    // --------- INIT GRAPHICS --------- //
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    Graphics2D g2d = (Graphics2D) g;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    FontMetrics fontMetrics = g2d.getFontMetrics();
    // --------- INIT GRAPHICS --------- //

    // clearing background
    super.paint(g);

    g2d.setColor(Color.black);
    g2d.drawString(Integer.toString(fpsDisplay) + " FPS",
            WIDTH - 9 - fontMetrics.stringWidth(Integer.toString(fpsDisplay) + " FPS"), 14);

    // render level 1
    for (int i = 0; i < 16; i++) {
        level1[i].render(g);
    }
    // render level 1

    // ---------
    g.dispose();
    g2d.dispose();
    bs.show();
    // ---------
}

public void createlevel1() {
    int brickCount = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            level1[brickCount] = new Bricks(50 + i * 100, 50 + j * 100);
            System.out.println("X: " + (50 + i * 100) + " Y: " + (50 + j * 100));
            brickCount++;
        }
    }
}

// 60 tps game loop
public void run() {
    init();
    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--;
        }
        render();
        frames++;
        if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            System.out.println("Ticks: " + updates + ", FPS: " + frames);
            fpsDisplay = frames;
            updates = 0;
            frames = 0;
        }
    }
    stop();
}

// called at the end of the loop, stops the thread
public synchronized void stop() {
    try {
        thread.join();
        running = false;
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

Bricks.java

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

@SuppressWarnings("unused")
public class Bricks {
    private int x, y, w, h;
    private Boolean isAlive;
    private Color randColour;

    public Bricks(int x, int y) {
        x = this.x;
        y = this.y;
        w = 75;
        h = 25;

        isAlive = true;
        randColour = Color.blue;
    }

    public void render(Graphics g) {
        g.setColor(randColour);
        g.fillRect(x, y, w, h);
    }

}

GameWindow.java

package main;

import java.awt.Canvas;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class GameWindow extends Canvas {

    private static final long serialVersionUID = -5131420759044457697L;

    public GameWindow(int WIDTH, int HEIGHT, int SCALE, String TITLE, Game game) {

        JFrame frame = new JFrame(TITLE);

        frame.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        frame.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        frame.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

        try { 
            frame.setIconImage(ImageIO.read(new File("res/Brick-icon.png")));
        } catch (IOException e) {
            frame.setIconImage(null);
            e.printStackTrace();
            System.out.println("ICON ERROR: Icon image not found. (Default Icon is set.)");
        }

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

        // displays the window 400ms later to allow for the game to fully load
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        frame.setVisible(true);
    }   

}

1 个答案:

答案 0 :(得分:0)

Bricks.java

x = this.x;
y = this.y;

应该是:

this.x = x;
this.y = y;
相关问题