我不擅长编程,但我正在努力学习,我一直在youtube上遵循一个教程,youtuber使用了4种单色颜色,但我发现很难用这种限制来制作精灵,所以我试图将其提升到8种颜色,但当我尝试更改它时,它只是循环前4种颜色。下面是颜色类、屏幕类和瓷砖类。我可以上传你需要的任何其他类。
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;
}
}发布于 2016-02-21 07:51:01
我遇到了同样的问题。首先,我认为这是一个问题,我们在颜色8上进行了56位移位。一个整数最多可以包含32位。因此,通过移动它56,我们完全摆脱了我们在那里的任何数据(如果我是正确的话)。
所以我试着把它改成返回一个long类型,但是这也解决不了任何问题。我知道这不是一个真正的答案,但我希望这能引起一些关注,因为我现在也被困在这个问题上了!
https://stackoverflow.com/questions/31562139
复制相似问题