我一直在youtube上关注本教程,用于在java中创建游戏,在本教程的某一点上,您应该加载64x64 PNG图像并读取每个像素的RGB值并创建类因此。我没有错误但是,我得到的只是一个空白屏幕。该程序应该在像素为蓝色时创建Player类的实例,在像素为红色时创建块类。
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
private boolean isRunning = false;
private Thread thread;
private Handler handler;
private BufferedImage level = null;
public Game(){
new Window(1000, 563, "Wizard Game", this);
start();
handler = new Handler();
this.addKeyListener(new KeyInput(handler));
BufferedImageLoader loader = new BufferedImageLoader();
level = loader.LoadImage("wizard_level.png");
loadLevel(level);
}
private void start(){
isRunning = true;
thread = new Thread(this);
thread.start();
}
private void stop(){
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 60;
while(isRunning){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
delta --;
}
if(isRunning)
render();
frames ++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
public void tick(){
handler.tick();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 1000, 563);
handler.render(g);
g.dispose();
bs.show();
}
//loading level
private void loadLevel(BufferedImage image){
int w = image.getWidth();
int h = image.getHeight();
for(int xx = 0; xx > w; xx++){
for(int yy = 0; yy < h; yy++){
int pixel = image.getRGB(xx, yy);
int red = (pixel >> 16) & 0xff;
int blue = (pixel >> 8) & 0xff;
int green = (pixel) & 0xff;
if(red == 255)
handler.addObject(new Block(xx*32, yy*32, ID.Block));
if(blue == 255)
handler.addObject(new Wizard(xx*32, yy*32, ID.Player, handler));
}
}
}
public static void main(String args[]){
new Game();
}
}
答案 0 :(得分:0)
在
ufw allow 9042
ufw status
行上,假设您level = loader.LoadImage("wizard_level.png");
忘记路径开头的level = loader.LoadImage("/wizard_level.png");
,否则编译器会将其作为字符串读取< / p>