Java - 如何使图像代表游戏中要绘制的图块?

发布于 2024-12-26 03:04:16 字数 170 浏览 4 评论 0原文

我试图弄清楚如何让我的游戏使用图像来代表每个点,在特定点绘制特定的图块。因此,如果该图像的一个像素是红色,则将在游戏中绘制指定的图片(图块),并且每个绿色像素代表不同的指定图像。我见过制作游戏的人这样做,但我不知道怎么做,也不知道它的名字。

如果您需要更多信息,我可以尝试解释我想做的更多事情。有人可以帮忙吗?

I am trying to figure out how to make my game draw a certain tile in a specific spot using an image to represent each spot. So if a pixel of that image was the color red a specified picture(tile) would be draw in the game, and each pixel that was green stood for a different specified image. I have seen people who make games do this but I dont know how to do it and I dont know the name for it.

If you need more info I can try to explain what I want to do more. Could someone please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉月流沐 2025-01-02 03:04:16

从长远来看,这实际上可能会更慢。我绝对建议您使用字节数组来表示图块,即字节[宽度][高度]。如果单个字节不再提供足够的信息,它将更快、更容易管理并且更容易扩展到 spriteData[宽度][高度] 之类的东西。

但是,如果您坚持使用图像来存储游戏数据,则可以使用类似以下内容的内容:

File file = new File("mygamedata.jpg");
BufferedImage image = ImageIO.read(file);

// Getting pixel color at position x, y (width, height)
int colour =  image.getRGB(x ,y); 
int red    = (colour & 0x00ff0000) >> 16;
int green  = (colour & 0x0000ff00) >> 8;
int blue   =  colour & 0x000000ff;
System.out.println("Red colour component = " + red);
System.out.println("Green colour component = " + green);
System.out.println("Blue colour component = " + blue); 

每个组件都在 (0...255) 范围内,您可以使用它来确定正确的图块,即

Graphics2D gfx = (Graphics2D) offScreenImage.getImage();
if (red == 120 && green == 0 && blue == 0) {
  gc.drawImage(tile[whichTileYouWantForRed], x, y, null); // where x and y is the pixel you read.
}

或者,您可以完全跳过提取组件,而只需使用颜色,即

if (colour == 0x00ff0000) {
  gc.drawImage(tile[whichTileYouWantForRed], x, y, null); // where x and y is the pixel you read.
} 

(无论如何,这会稍微快一点,实际上是您想要的。)

That may actually be slower in the long run. I would definitely recommend you use a byte array to represent the tiles, i.e. byte[width][height]. It will be faster, easier to manage and easier to extend to something like spriteData[width][height] if a single byte does not supply enough information anymore.

However, if you insist on using an image to store game data, you can use something like the following:

File file = new File("mygamedata.jpg");
BufferedImage image = ImageIO.read(file);

// Getting pixel color at position x, y (width, height)
int colour =  image.getRGB(x ,y); 
int red    = (colour & 0x00ff0000) >> 16;
int green  = (colour & 0x0000ff00) >> 8;
int blue   =  colour & 0x000000ff;
System.out.println("Red colour component = " + red);
System.out.println("Green colour component = " + green);
System.out.println("Blue colour component = " + blue); 

Each component will be in the range (0...255) and you can use that to determine the correct tile, i.e.

Graphics2D gfx = (Graphics2D) offScreenImage.getImage();
if (red == 120 && green == 0 && blue == 0) {
  gc.drawImage(tile[whichTileYouWantForRed], x, y, null); // where x and y is the pixel you read.
}

Alternatively, you can skip extracting the components altogether, and simply use colour, i.e.

if (colour == 0x00ff0000) {
  gc.drawImage(tile[whichTileYouWantForRed], x, y, null); // where x and y is the pixel you read.
} 

(Which will be slightly faster anyway and actually what you want.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文