gpt4 book ai didi

java - (Java) 2D 游戏出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:54:50 24 4
gpt4 key购买 nike

我的 2D 游戏遇到问题,我正在按照教程进行操作。我已经多次扫描我的代码,但似乎无法解决它。抱歉,如果我的问题听起来很愚蠢,但我确实欢迎任何帮助。这是我的来源:

package game;


import gfx.SpriteSheet;

import java.awt.BorderLayout;
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 java.awt.image.DataBufferInt;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable
{
private static final long serialVersionUD = 1L;

public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH/12*9;
public static final int SCALE = 3;
public static final String NAME = "Game";

private JFrame frame;

public boolean running = false;
public int tickCount = 0;

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

private SpriteSheet spriteSheet = new SpriteSheet("SpriteSheet.png");

public Game()
{
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame = new JFrame(NAME);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();

frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private synchronized void start()
{
running = true;
new Thread(this).start();
}
private synchronized void stop()
{
running = false;
}

public void run()
{
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60D;

int ticks = 0;
int frames = 0;

long lastTimer = System.currentTimeMillis();
double delta = 0;
while(running)
{
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;


while(delta >= 1)
{
ticks++;
tick();
delta -= 1;
shouldRender = true;
}


try {
Thread.sleep(2);
} catch (InterruptedException ex) {

}

if(shouldRender)
{
frames++;
render();
}

if(System.currentTimeMillis() - lastTimer >= 1000)
{
lastTimer += 1000;
System.out.println(frames + ", " + ticks);
frames = 0;
ticks = 0;
}

}
}
//Updates all of internal variables and logic and stuff.
public void tick()
{
tickCount++;

for(int i = 0; i < pixels.length; i++)
{
pixels [i] = i + tickCount;
}
}
//prints the updated logic.
public void render()
{
BufferStrategy bs = getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());

g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

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

}

//Main method
public static void main(String[]args)
{
new Game().start();
}



}
<小时/>
package gfx;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet
{
public String path;
public int width;
public int height;
public int[] pixels;

public SpriteSheet(String path)
{
BufferedImage image = null;

try
{
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e)
{
e.printStackTrace();
}

if(image == null)
{
return;
}

this.path = path;
this.width = image.getWidth();
this.height = image.getHeight();

pixels = image.getRGB(0, 0, width, height, null, 0, width);

for(int i = 0; i < pixels.length; i++)
{
pixels[i] = (pixels[i] & 0xff / 64);
}

for(int i = 0; i < 8; i++)
{
System.out.println(pixels[i]);
}
}


}
<小时/>

这是我正在使用的视频教程的链接:Tutorial

我在 64 位操作系统(Windows)上使用 eclipse。再次强调,我们将不胜感激任何帮助,谢谢。

编辑:错误信息:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at gfx.SpriteSheet.<init>(SpriteSheet.java:21)
at game.Game.<init>(Game.java:34)
at game.Game.main(Game.java:144)

抱歉,忘记了错误消息。 XD

最佳答案

错误消息告诉您出了什么问题:您的图像路径错误。图像路径必须相对于类文件位置给出。

关于java - (Java) 2D 游戏出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com