2017-03-01 26 views
-1

我在我的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); 
    } 

} 
+0

你調用'createLevel1()'之前它上面for循環? – Chris

+1

我建議你發佈一個MCVE http://stackoverflow.com/help/mcve,以便我們可以運行它。 –

+0

@DM我發佈了一個MCVE –

回答

0

Bricks.java

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

應該是:

this.x = x; 
this.y = y; 
+0

謝謝!我不敢相信我搞砸了。我在編寫該部分時得到了我的老師的幫助,而且我最初開始輸入this.x,並且他告訴我要執行x = this.x等... –

+0

調試時,您應該嘗試使用brick render方法進行打印。那很快就會導致你遇到問題。 –