Adding a view of the map in Minicraft
In this tutorial we are going to add a view of the map of the level we are in, which shows us where we are located and where are the stairs and the resources.
Code to add to the LevelGen class
public static void showMap(byte[] map, Player player) { int w = 128; int h = 128; BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); int[] pixels = new int[w * h]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int i = x + y * w; if (map[i] == Tile.water.id) pixels[i] = 0x000080; if (map[i] == Tile.grass.id) pixels[i] = 0x208020; if (map[i] == Tile.rock.id) pixels[i] = 0xa0a0a0; if (map[i] == Tile.dirt.id) pixels[i] = 0x604040; if (map[i] == Tile.sand.id) pixels[i] = 0xa0a040; if (map[i] == Tile.tree.id) pixels[i] = 0x003000; if (map[i] == Tile.lava.id) pixels[i] = 0xff2020; if (map[i] == Tile.cloud.id) pixels[i] = 0xa0a0a0; if (map[i] == Tile.stairsDown.id) pixels[i] = 0xffffff; if (map[i] == Tile.stairsUp.id) pixels[i] = 0xffffff; if (map[i] == Tile.cloudCactus.id) pixels[i] = 0xff00ff; } } pixels[(player.x>>4) + (player.y>>4) * 128] = 0xffaa00; img.setRGB(0, 0, w, h, pixels, 0, w); JOptionPane.showMessageDialog(null, null, "Another", JOptionPane.YES_NO_OPTION, new ImageIcon(img.getScaledInstance(w * 4, h * 4, Image.SCALE_AREA_AVERAGING))); }
Code to add to the render() method of the class Game
if (input.map.clicked) { LevelGen.showMap(level.tiles, player); }
Code to add to the class InputHandler
public Key map = new Key();
if (ke.getKeyCode() == KeyEvent.VK_M) map.toggle(pressed);
<< Previous | Index>> |