gpt4 book ai didi

java - 渲染 8 种单色的 Sprite

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:32 26 4
gpt4 key购买 nike

我不太擅长编程,但我正在努力学习,我一直在关注 YouTube 上的教程,并且 YouTube 用户使用 4 种单色颜色,但我发现很难在这种限制下制作 Sprite ,所以我尝试将其增加到 8 种颜色,但当我尝试更改它时,它只会循环前 4 种颜色。下面是颜色类、屏幕类和图 block 类。我可以上传您需要的任何其他类(class)。

package com.alan.game.level.tiles;

import com.alan.game.gfx.Colours;
import com.alan.game.gfx.Screen;
import com.alan.game.level.Level;

public abstract class Tile {

public static final Tile[] tiles = new Tile[256];
public static final Tile VOID = new BasicTile(0, 0, 0, Colours.get(000, -1, -1, -1, -1, -1, -1, -1));
public static final Tile STONE = new BasicTile(1, 1, 0, Colours.get(000, 002, 003, 004, 000, 000, 000, 000));
public static final Tile GRASS = new BasicTile(2, 2, 0, Colours.get(-1, 131, 141, -1, -1, -1,- 1, -1));

protected byte id;
protected boolean solid;
protected boolean emitter;

public Tile(int id, boolean isSolid, boolean isEmitter){
this.id = (byte) id;
if(tiles[id] != null) throw new RuntimeException("Duplicate tile id on " + id);
this.solid = isSolid;
this.emitter = isEmitter;
tiles[id] = this;
}

public byte getId(){
return id;
}

public boolean isSolid(){
return solid;
}

public boolean isEmitter(){
return emitter;
}
public abstract void render(Screen screen, Level level, int x, int y);
}

package com.alan.game.gfx;

public class Screen {

public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;

public int[] pixels;

public int xOffset = 0;
public int yOffset = 0;

public int width;
public int height;

public SpriteSheet sheet;

public Screen(int width, int height, SpriteSheet sheet){
this.width = width;
this.height = height;
this.sheet = sheet;

for(int i = 0; i < MAP_WIDTH * MAP_WIDTH; i++){
pixels = new int[width * height];
}
}

public void setOffset(int xOffset, int yOffset){
this.xOffset = xOffset;
this.yOffset = yOffset;
}
public void render(int xPos, int yPos, int tileIndex, int colour){
render(xPos, yPos, tileIndex, colour, false, false);
}

public void render(int xPos, int yPos, int tileIndex, int colour, boolean mirrorX, boolean mirrorY){
xPos -= xOffset;
yPos -= yOffset;

int xTile = tileIndex % 32;
int yTile = tileIndex / 32;
int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
for(int y = 0; y < 8; y++){
int ySheet = y;
if(mirrorY) ySheet = 7 - y;
if(y + yPos < 0 || y + yPos >= height) continue;
for(int x = 0; x < 8; x++){
int xSheet = x;
if(mirrorX) xSheet = 7 - x;
if(x + xPos < 0 || x + xPos >= width) continue;
int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) & 255;
if(col < 255) pixels[(x + xPos) + (y + yPos) * width] = col;
}
}
}
}

package com.alan.game.gfx;

public class Colours {

public static int get(int colour1, int colour2, int colour3, int colour4, int colour5, int colour6, int colour7, int colour8){

return (get(colour8) << 56) + (get(colour7) << 48) + (get(colour6) << 40) + (get(colour5) << 32) + (get(colour4) << 24) + (get(colour3) << 16) + (get(colour2) << 8) + get(colour1);
}

private static int get(int colour){
if(colour < 0) return 255;
int r = colour / 100 % 10;
int g = colour / 10 % 10;
int b = colour % 10;
return r * 36 + g * 6 + b;
}
}

最佳答案

我也遇到了同样的问题。我首先想到的一个问题是我们在颜色 8 上进行了位移 56。一个整数最多可以包含 32 位。因此,通过将其移动 56,我们就完全摆脱了那里的所有数据(如果我是正确的)。

所以我尝试将其更改为返回 long,但这也无法解决任何问题。我知道这不是一个真正的答案,但我希望这能重新引起一些关注,因为我现在也陷入了困境!

关于java - 渲染 8 种单色的 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31562139/

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