1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| import java.applet.*; import java.awt.*; import java.awt.event.*;
public class Tile_engine extends Applet implements KeyListener, Runnable {
private int keyPressed = -1; private int screenX = 0; private int screenY = 0; private final int TILE_MAX = 11; private final int TILE_SIZE = 32; private final int APPLET_WIDTH = 640; private final int APPLET_HEIGHT = 480; private final int SKY = 0; private final int CLOUD_1 = 1; private final int CLOUD_2 = 2; private final int RED_BRICKS = 3; private final int GRASS = 4; private final int DIRT = 5; private final int STONE = 6; private final int QBOX1 = 7; private final int QBOX2 = 8; private final int QBOX3 = 9; private final int QBOX4 = 10; private final int MAP_ROWS = 15; private final int MAP_COLS = 65; private Image background = null; private Image buffer = null; private Graphics bgBuff; private Graphics bufferg; private Image[] tile = new Image[TILE_MAX];
private final int map[][] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,6,6,6,6,6,0,0,0,0,0,1,2,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0,0,0,0,0,0,0,0,0,0,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,3,6,6,0,0,0,0,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,5,0,0,0,0,0,0,0,0,0,3,6,6,0,0,0,0,5,5,5,5,5,5,5,5,5,5,5,0,0,0,0,0,0,0,0,0,0,0}, {4,4,4,4,4,4,4,4,0,0,4,4,4,4,4,4,4,4,4,4,4,4,0,0,5,0,5,0,0,4,4,4,0,0,0,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,4,4,4,4,6,6,6,4,4,4,4}, {5,5,5,5,5,5,5,5,0,0,5,5,5,5,5,5,5,5,5,5,5,5,0,0,5,0,5,0,0,5,5,5,0,0,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0,0,0,5,5,5,5}}; public void init( ) { tile[SKY] = getImage(getDocumentBase(), "sky.png"); tile[CLOUD_1] = getImage(getDocumentBase(), "cloud1.png"); tile[CLOUD_2] = getImage(getDocumentBase(), "cloud2.png"); tile[RED_BRICKS] = getImage(getDocumentBase(), "redBricks.png"); tile[GRASS] = getImage(getDocumentBase(), "grass.png"); tile[DIRT] = getImage(getDocumentBase(), "dirt.png"); tile[STONE] = getImage(getDocumentBase(), "stone.png"); tile[QBOX1] = getImage(getDocumentBase(), "qbox1.png"); tile[QBOX2] = getImage(getDocumentBase(), "qbox2.png"); tile[QBOX3] = getImage(getDocumentBase(), "qbox3.png"); tile[QBOX4] = getImage(getDocumentBase(), "qbox4.png");
buffer = createImage(TILE_SIZE * MAP_COLS, TILE_SIZE * MAP_ROWS); bufferg = buffer.getGraphics();
addKeyListener(this);
if(buffer != null) { int tile_id; for(int y=0;y<MAP_ROWS;y++) { for(int x=0;x<MAP_COLS;x++) { tile_id = map[y][x]; bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this); } } }
}
public void start( ) { Thread th = new Thread(this); th.start(); }
public void run( ) { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true) { animate_tiles(); try { Thread.sleep(200); } catch(InterruptedException ex) { } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void animate_tiles( ) { int tile_id; for(int y=0;y<MAP_ROWS;y++) { for(int x=0;x<MAP_COLS;x++) { tile_id = map[y][x]; switch(tile_id) { case QBOX1: map[y][x] = QBOX2; break; case QBOX2: map[y][x] = QBOX3; break; case QBOX3: map[y][x] = QBOX4; break; case QBOX4: map[y][x] = QBOX1; break; default: break; } } } }
public void paint(Graphics g) {}
public void update(Graphics g) {
if(buffer != null) { int tile_id; for(int y=0;y<MAP_ROWS;y++) { for(int x=0;x<MAP_COLS;x++) { tile_id = map[y][x]; bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this); } } }
paint(bufferg); g.drawImage(buffer, screenX, screenY, this); repaint(); }
public void keyReleased( KeyEvent e ) { } public void keyPressed( KeyEvent e ) { } public void keyTyped( KeyEvent e ) { char c = e.getKeyChar(); if(c == 'z') { screenX+=5; } else if(c == 'c') { screenX-=5; } } } |