/*************************** * * TURBO JÄNES * * Autor: Mart Karu * Start: 26.11.2001 * * ****************************/ import java.awt.*; import java.applet.*; import java.awt.*; import java.util.*; import java.io.*; import java.net.*; import java.awt.event.*; import java.util.Date; public class turborabbit extends Applet implements Runnable { int mapWidth = 18; int mapHeight = 11; int points = 0; int level = 1; long leveltime = 0; long totaltime = 0; // buffering Image offScreenImg; Graphics offScreenGFX; // images MediaTracker tracker = new MediaTracker(this); Image tileBack = null; Image tileWall = null; Image tileEat = null; Image tileDanger = null; Image tileExit = null; Image porgand = null; Image kapsas = null; Image imgPoints = null; Image levelLoader = null; //Image tile = null; //Image tile = null; public Image imgRabbit = null; public Image imgRabbitR = null; public Image imgRabbitL = null; public Image imgRabbitDead1 = null; Vector imgs = new Vector(); Vector fallingBoxes = new Vector(); // rabbit rabber r = null; // map int map[][] = new int[18][11]; boolean mapLoaded = false; boolean gameOver = false; waiter w = new waiter(); public void init() { // pic read & init tileBack = getImage(getCodeBase(), "images/back.gif"); tracker.addImage(tileBack ,0); imgs.insertElementAt(tileBack, 0); tileWall = getImage(getCodeBase(), "images/wall.gif"); tracker.addImage(tileWall,1); imgs.insertElementAt(tileWall, 1); tileEat = getImage(getCodeBase(), "images/eat.gif"); tracker.addImage(tileEat ,2); imgs.insertElementAt(tileEat, 2); tileDanger = getImage(getCodeBase(), "images/danger.gif"); tracker.addImage(tileDanger ,3); imgs.insertElementAt(tileDanger, 3); tileExit = getImage(getCodeBase(), "images/exit.gif"); tracker.addImage(tileExit,4); imgs.insertElementAt(tileExit, 4); porgand = getImage(getCodeBase(), "images/mopkobka.gif"); tracker.addImage(porgand,5); imgs.insertElementAt(porgand, 5); kapsas = getImage(getCodeBase(), "images/kapsas.gif"); tracker.addImage(kapsas,6); imgs.insertElementAt(kapsas, 6); imgRabbit = getImage(getCodeBase(), "images/rabbi.gif"); tracker.addImage(imgRabbit,7); imgPoints = getImage(getCodeBase(), "images/points.gif"); tracker.addImage(imgPoints,8); levelLoader = getImage(getCodeBase(), "images/levelloader.gif"); tracker.addImage(levelLoader ,9); imgRabbitDead1 = getImage(getCodeBase(), "images/rabbi_dead1.gif"); tracker.addImage(imgRabbitDead1 ,10); imgRabbitR = getImage(getCodeBase(), "images/rabbir.gif"); tracker.addImage(imgRabbit,11); imgRabbitL = getImage(getCodeBase(), "images/rabbil.gif"); tracker.addImage(imgRabbit,12); // load images into memory tracker.checkID(0,true); tracker.checkID(1,true); tracker.checkID(2,true); tracker.checkID(3,true); tracker.checkID(4,true); tracker.checkID(5,true); tracker.checkID(6,true); tracker.checkID(7,true); tracker.checkID(8,true); tracker.checkID(9,true); tracker.checkID(10,true); tracker.checkID(11,true); tracker.checkID(12,true); r = new rabber(this); r.start(); } public void start() { initLevel(); } public void initLevel(){ try{ mapLoaded = false; leveltime = new Date().getTime(); URL toTry = new URL(getCodeBase().toString() + "maps/level" + level + ".txt"); w = null; w = new waiter(); w.wait = 2000; w.go = false; w.start(); getMap(toTry); r.findPosition(); } catch (Exception e) {} } public void stop() { } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if(offScreenImg==null) { offScreenImg = createImage(this.getSize().width, this.getSize().height); offScreenGFX = offScreenImg.getGraphics(); } if((!tracker.checkAll())) { offScreenGFX.drawImage(levelLoader, 0, 0, this); offScreenGFX.drawImage(imgPoints, 0, 198, this); offScreenGFX.drawString("toimuvad laadimistööd, oodake...", 20,this.getSize().height/2); g.drawImage(offScreenImg, 0, 0, this); } else { if (gameOver) { offScreenGFX.setColor(new Color(0, 0, 0) ); offScreenGFX.setFont(new Font("Monospaced", Font.BOLD, 18) ); offScreenGFX.drawImage(levelLoader, 0, 0, this); offScreenGFX.drawImage(imgPoints, 0, 198, this); offScreenGFX.drawString("Mäng läbi!", 100, 45); g.drawImage(offScreenImg, 0, 0, this); repaint(); return; } if (mapLoaded && w.go) { drawMap(offScreenGFX); offScreenGFX.setColor(new Color(255, 112, 179) ); offScreenGFX.setFont(new Font("Monospaced", Font.BOLD, 10) ); offScreenGFX.drawImage(imgPoints, 0, 198, this); offScreenGFX.drawString(padZeros(points, 9), 258, 217); offScreenGFX.drawString(padZeros((new Date().getTime())-leveltime, 9), 258, 232); offScreenGFX.drawString(padZeros(totaltime, 9), 258, 247); offScreenGFX.drawString(padZeros(level, 9), 258, 262); r.drawRabbit(offScreenGFX); r.drawScore(offScreenGFX) ; g.drawImage(offScreenImg, 0, 0, this); } else { offScreenGFX.setColor(new Color(0, 0, 0) ); offScreenGFX.setFont(new Font("Monospaced", Font.BOLD, 18) ); offScreenGFX.drawImage(levelLoader, 0, 0, this); offScreenGFX.drawImage(imgPoints, 0, 198, this); offScreenGFX.drawString("Laeme", 100, 45); offScreenGFX.drawString("taset " + level, 100, 65); g.drawImage(offScreenImg, 0, 0, this); } } repaint(); } public String padZeros(long num, int amount) { String out = num + ""; while (out.length() < amount) { out = "0" + out; } return out; } public void run() { while(true) { //repaint(); /*try { Thread.sleep(50);}catch(InterruptedException e) {}*/ } } public static void main(String args[]) { Frame f = new Frame(); f.add(new turborabbit()); f.setSize(324,198); f.setVisible(true); } private void drawMap(Graphics g) { if ((tileBack != null) && (mapLoaded)) { for(int i=0; i0) points += pointsaddF; initLevel(); return; } if (map[r.rXPos][r.rYPos]==2) points += 11; if (map[r.rXPos][r.rYPos]==5) points += 100; if (map[r.rXPos][r.rYPos]==6) points += 200; map[r.rXPos][r.rYPos] = 0; } } public void destroy(){ for(int i=0;i 0)&&(!pihtas)&&parent.mapLoaded&&parent.w.go) { int code = e.getKeyCode(); switch(code) { case 38: // up if (okToGo(rXPos, rYPos-1))rYPos--; break; case 40: // down if (okToGo(rXPos, rYPos+1))rYPos++; break; case 37: // left if (okToGo(rXPos-1, rYPos))rXPos--; break; case 39: // right if (okToGo(rXPos+1, rYPos))rXPos++; break; } } parent.updateMap(); //parent.repaint(); } public boolean okToGo(int x, int y){ boolean ret = false; if ((x<0)||(x>parent.mapWidth)) return false; if ((y<0)||(y>parent.mapHeight)) return false; if ( (parent.map[x][y] != 1 ) && (parent.map[x][y] != 3 ) ) ret = true; return ret; } public void keyReleased(KeyEvent e) { } public void run() { while(true) { if (lives <=0) parent.gameOver = true; if (!pihtas) { showImage++; if(showImage >3) showImage = 0; activeImage = (Image)images.elementAt(showImage); try{Thread.sleep(50);} catch(Exception e) {} //parent.repaint(); } else { activeImage = parent.imgRabbitDead1; try{Thread.sleep(2000);} catch(Exception e) {} pihtas = false; parent.initLevel(); } } } public void drawRabbit(Graphics g) { if ((activeImage != null) && (parent.mapLoaded)) { g.drawImage(activeImage , rXPos*18+1, rYPos*18+1, (Applet)parent); g.setColor(new Color(0, 0, 0) ); g.setFont(new Font("Monospaced", Font.BOLD, 14) ); } } public void drawScore(Graphics g) { if ((parent.mapLoaded)) { for(int i = 0; iparent.mapHeight) return; if (((y+1)==parent.r.rYPos) && ((x)==parent.r.rXPos))return; if (parent.map[x][y+1] == 0) { parent.map[x][y] = 0; y++; parent.map[x][y] = 3; // äkki jänes sai pihta? if((x == parent.r.rXPos) && (y+1 == parent.r.rYPos)) parent.r.rabbitPihtas(); return; } if (parent.map[x][y+1] == 3) { if ((x+1)0) { if ( (parent.map[x-1][y+1] == 0) && (parent.map[x-1][y] == 0) && !(((x-1) == parent.r.rXPos) && ((y) == parent.r.rYPos)) && !(((x-1) == parent.r.rXPos) && ((y+1) == parent.r.rYPos)) ) { parent.map[x][y] = 0; y++; x--; parent.map[x][y] = 3; // äkki jänes sai pihta? if((x == parent.r.rXPos) && (y+1 == parent.r.rYPos)) parent.r.rabbitPihtas(); return; } } } //else stopMe(); } public void destroy() { stopMe(); } /*public void init(){}; public void start(){}; public void destroy(){};*/ } class waiter extends Thread{ boolean go = true; int wait = 1000; public void run() { go = false; try{Thread.sleep(wait);} catch (Exception e) {} go = true; } }