我有一个包括这个游戏中所有角色的png图片,但是黑白的,经典的坦克大战中即使是相同类型的坦克,如果它们的颜色不相同的话,也要两种图片,感觉这样有点浪费空间,能不能够在游戏运行的时候再动态给这些图片上色呢???不过我感觉没有。请问各位前辈能不能够介绍几个提供经典坦克大战素材和程序编程思想的网站吗。我急着要,先谢过各位前辈先。
------解决方案--------------------------------------------------------
可以通过改变png图片的调色板来改变图片的颜色.这个算法的原理是:找到调色板区,根据原有格式修改之后,生成新的crc校验码,然后替换原来的调色板。这样就可以用一个png图片,创建多个变色副本。实例代码如下:
- Java code
public class PalettedImage { public Image getPalettedImage(byte[] data, int[] originalColors, int[] palettedColors) { byte[] tempData = new byte[data.length]; System.arraycopy(data, 0, tempData, 0, data.length); Image img = null; int[] parameter = { 0, 0, 0 }; analyze(tempData, parameter); for (int i = 0; i < originalColors.length; i++) { replaceColor(tempData, parameter, originalColors+, palettedColors+); } fillData(tempData, parameter); try { img = Image.createImage(tempData, 0, data.length); } catch (Exception e) { System.out.println("getPalettedImage && " + e.toString()); } return img; } private void analyze(byte[] data, int[] para) { int offset = 8; int chunkLen = 0; while (data[offset + 4] != 0x50 || data[offset + 5] != 0x4c || data[offset + 6] != 0x54 || data[offset + 7] != 0x45) { chunkLen = readInt(data, offset); offset += (4 + 4 + chunkLen + 4); } chunkLen = readInt(data, offset); para[2] = chunkLen / 3; para[0] = offset + 8; para[1] = offset + 8 + chunkLen; } private int readInt(byte[] data, int offset) { return ((data[offset] & 0xFF) << 24) | ((data[offset + 1] & 0xFF) << 16) | ((data[offset + 2] & 0xFF) << 8) | (data[offset + 3] & 0xFF); } private void replaceColor(byte[] data, int[] para, int oldColor, int newColor) { byte rr = (byte) ((oldColor >> 16) & 0xff); byte gg = (byte) ((oldColor >> 8) & 0xff); byte bb = (byte) (oldColor & 0xff); for (int i = 0, offset = para[0], temp = 0; i < para[2]; i++, offset += 3) { if (rr == data[offset] && gg == data[offset + 1] && bb == data[offset + 2]) { data[offset] = (byte) ((newColor >> 16) & 0xff); data[offset + 1] = (byte) ((newColor >> 8) & 0xff); data[offset + 2] = (byte) (newColor & 0xff); break; } } } private void fillData(byte[] data, int[] para) { int checksum = update_crc(data, para[0] - 4, para[2] * 3 + 4); data[para[1]] = (byte) ((checksum >> 24) & 0xff); data[para[1] + 1] = (byte) ((checksum >> 16) & 0xff); data[para[1] + 2] = (byte) ((checksum >> 8) & 0xff); data[para[1] + 3] = (byte) ((checksum) & 0xff); } private int update_crc(byte[] buf, int off, int len) { int c = 0xffffffff; int n, k; int xx; int[] crc_table = new int[256]; for (n = 0; n < 256; n++) { xx = n; for (k = 0; k < 8; k++) { if ((xx & 1) == 1) { xx = 0xedb88320 ^ (xx >>> 1); } else { xx = xx >>> 1; } } crc_table[n] = xx; } for (n = off; n < len + off; n++) { c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >>> 8); } return (c ^ 0xffffffff); }}
------解决方案--------------------------------------------------------
坦克大战 尚学堂那个视频教程就很不错啊
楼主可以去看下
------解决方案--------------------------------------------------------