我正在用Java设计游戏。我使用了java-gaming.org处找到的一些代码,似乎工作奇怪。
当我按照给定的方式运行循环时,它将正确更新我的画布。但是,我并不希望它始终更新我的背景"可以这么说。为了解决这个问题,我只运行了一次。现在它似乎运行,但不显示我画的图像。
我上传了运行一次(损坏)的程序截图,程序连续运行(正常运行)。在imgur上查看。
tldr; 一切似乎都在运行,但它并没有在我的框架中显示任何内容。见下面的代码。
package main;
//imports...
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameEngine implements Runnable {
//declare variables...
final static int B_HEIGHT = 20;
final static int B_WIDTH = 20;
private int cellWidth = GameTester.WIDTH / B_WIDTH;
private int cellHeight = GameTester.HEIGHT / B_HEIGHT;
protected boolean init = true;
JFrame frame;
Canvas canvas;
BufferStrategy bufferStrategy;
Board gameBoard;
Generator gen;
long desiredFPS = 60;
long desiredDeltaLoop = (1000 * 1000 * 1000) / desiredFPS;
boolean running = true;
public GameEngine(int width, int height) {
//create the gamespace...
gameBoard = new Board(B_WIDTH, B_HEIGHT);
gen = new Generator();
gen.addWalls(gameBoard);
agents.Champion champ = new agents.Champion();
items.Item item = new items.Item();
gameBoard.placeChampion(champ);
gameBoard.placeItem(item);
//frame magic...
frame = new JFrame("Basic Game");
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(width, height));
panel.setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, width, height);
canvas.setIgnoreRepaint(true);
panel.add(canvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
canvas.requestFocus();
}
//actual game mechanics...
public void run() {
long beginLoopTime;
long endLoopTime;
long currentUpdateTime = System.nanoTime();
long lastUpdateTime;
long deltaLoop;
//game loop...
while (running) {
beginLoopTime = System.nanoTime();
//this draws things
render();
lastUpdateTime = currentUpdateTime;
currentUpdateTime = System.nanoTime();
//update magic...
update((int) ((currentUpdateTime - lastUpdateTime) / (1000 * 1000)));
endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;
if (deltaLoop <= desiredDeltaLoop) {
try {
Thread.sleep((desiredDeltaLoop - deltaLoop) / (1000 * 1000));
} catch (InterruptedException e) {
// Do nothing
}
}
}
}
//creates g object...
private void render() {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, GameTester.WIDTH, GameTester.HEIGHT);
render(g);
g.dispose();
bufferStrategy.show();
}
// does nothing important...
private double x = 0;
protected void update(int deltaTime) {
if (GameTester.dbglvl > 1) {
System.out.println("update called");
}
x += deltaTime * 0.2;
while (x > 500) {
x -= 500;
}
}
// ACTUAL GAME DRAWING MECHANISM...
protected void render(Graphics2D g) {
//first run
if (init) {
renderinit(g);
} else {
//every other time after first run
}
}
//first run (generates game board)
protected void renderinit(Graphics2D g) {
BufferedImage img = null;
//loop through 2d array
for (int row = 0; row < gameBoard.getWidth(); row++) {
for (int col = 0; col < gameBoard.getHeight(); col++) {
//get image path
String str = gameBoard.getSpace(row, col).getPath();
//debug info
if (GameTester.dbglvl > 1) {
System.out.println(gameBoard.getSpace(row, col).getPath());
}
//get the image to be rendered
try {
img = ImageIO.read(new File(str));
} catch (IOException e) {
if (GameTester.dbglvl > 0) {
System.out
.println("image get failed - image specified: "
+ str + " ");
}
}
if (GameTester.dbglvl > 1) {
System.out.println("Image Destination: (" + row * cellWidth
+ ", " + col * cellHeight + ")");
}
//draws given image
g.drawImage(img, row * cellWidth, // DX1
col * cellHeight, // DY1
(row * cellWidth) + cellWidth, // DX2
(col * cellHeight) + cellHeight, // DY2
0, // SX1
0, // SY1
img.getWidth(), // SX2
img.getHeight(), // SY2
null);
}
}
//done with first time run
init = false;
if (GameTester.dbglvl > 0) {
System.out.println("Board Rendered");
}
}
}