首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >渲染8种单色的精灵

渲染8种单色的精灵
EN

Stack Overflow用户
提问于 2015-07-22 19:43:38
回答 1查看 69关注 0票数 1

我不擅长编程,但我正在努力学习,我一直在youtube上遵循一个教程,youtuber使用了4种单色颜色,但我发现很难用这种限制来制作精灵,所以我试图将其提升到8种颜色,但当我尝试更改它时,它只是循环前4种颜色。下面是颜色类、屏幕类和瓷砖类。我可以上传你需要的任何其他类。

代码语言:javascript
复制
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;
}
}
EN

回答 1

Stack Overflow用户

发布于 2016-02-21 07:51:01

我遇到了同样的问题。首先,我认为这是一个问题,我们在颜色8上进行了56位移位。一个整数最多可以包含32位。因此,通过移动它56,我们完全摆脱了我们在那里的任何数据(如果我是正确的话)。

所以我试着把它改成返回一个long类型,但是这也解决不了任何问题。我知道这不是一个真正的答案,但我希望这能引起一些关注,因为我现在也被困在这个问题上了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31562139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档