import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import java.io.*; import java.util.*; public class Muna extends MIDlet implements CommandListener { Form fMain = new Form("main"); Form fName = new Form("name"); Form fLives = new Form("lives"); Form fScores = new Form("hiscores"); TextField tfName = new TextField("Enter your name:", "", 20, TextField.ANY); TextField tfLives = new TextField("Number of lives:", "5", 20, TextField.NUMERIC); Command cNew = new Command("New game", Command.SCREEN, 1); Command cSettings = new Command("Settings", Command.SCREEN, 1); Command cOK = new Command("Continue", Command.OK, 1); Command cScores = new Command("Hiscores", Command.SCREEN, 1); Command cScoresClear = new Command("Clear", Command.OK, 1); Command cYes = new Command("Yes", Command.OK, 1); Command cNo = new Command("No", Command.BACK, 1); Command cBack = new Command("Back", Command.BACK, 1); Command cQuit = new Command("Quit", Command.SCREEN, 1); Command cExit = new Command("Quit", Command.EXIT, 1); Command cMain = new Command("Main menu", Command.SCREEN, 1); String sMain[] = { cNew.getLabel(), cSettings.getLabel(), cScores.getLabel(), cQuit.getLabel() }; String sSettings[] = { "Name", "Speed", "Difficulty", "Lives" }; String sLevels[] = { "5", "4", "3", "2", "1" }; String sScoresClear = "Are you sure you want to delete all entries?"; List lMain = new List("main", List.IMPLICIT, sMain, null); List lSettings = new List("settings", List.IMPLICIT, sSettings, null); List lSpeed = new List("Choose your level:", List.IMPLICIT, sLevels, null); List lDifficulty = new List("Choose your level:", List.IMPLICIT, sLevels, null); int iSpeed = 3, iDifficulty = 3, iLives = 5; String sName = ""; Display dThis = Display.getDisplay(this); String rsName = "munaScore"; Canvas canv; protected void startApp() throws MIDletStateChangeException {} protected void pauseApp() {} protected void destroyApp(boolean obligation) throws MIDletStateChangeException{} public Muna() { fName.append(tfName); fLives.append(tfLives); fMain.append("Welcome to Muna :)"); fMain.addCommand(cNew); fMain.addCommand(cSettings); fMain.addCommand(cScores); fMain.addCommand(cExit); fMain.setCommandListener(this); dThis.setCurrent(fMain); } public void commandAction (Command command, Displayable p2) { if (command == cNew) { canv = new MunaPyydja(iSpeed, iDifficulty, iLives, sName); canv.addCommand(cMain); canv.setCommandListener(this); dThis.setCurrent(canv); } else if (command == cSettings) show(lSettings); else if (command == cOK) { if (dThis.getCurrent() == lSettings) { int iSelected = lSettings.getSelectedIndex(); if (iSelected == 0) { tfName.setString(sName); show(fName); } else if (iSelected == 1) { // setSelectedIndex() t88tas koolis wtk2.1'ga, kodus 1.0.3'ga mitte lSpeed.setSelectedIndex(iSpeed == -1 ? 2 : 5 - iSpeed, true); show(lSpeed); } else if (iSelected == 2) { lDifficulty.setSelectedIndex(iDifficulty == -1 ? 2 : 5 - iDifficulty, true); show(lDifficulty); } else if (iSelected == 3) { tfLives.setString(Integer.toString(iLives)); show(fLives); } } else if (dThis.getCurrent() == lSpeed) iSpeed = Integer.parseInt(sLevels[lSpeed.getSelectedIndex()]); else if (dThis.getCurrent() == lDifficulty) iDifficulty = Integer.parseInt(sLevels[lDifficulty.getSelectedIndex()]); else if (dThis.getCurrent() == fName) sName = tfName.getString(); else if (dThis.getCurrent() == fLives) iLives = Integer.parseInt(tfLives.getString()); else dThis.setCurrent(fMain); } else if (command == cScores) { int iWidth = 15; HighScore h = new HighScore(rsName); Vector scores = h.scoreGetAll(); while(fScores.size() != 0) fScores.delete(0); //fScores.append(fillSpaces("Name", iWidth) + "Score\n"); for (int i = 0; i < scores.size(); i++) { String[] s = (String[])scores.elementAt(i); fScores.append("\n" + fillSpaces(s[0], iWidth) + s[1]); } show(fScores, cBack, cScoresClear); } else if (command == cScoresClear) { while(fScores.size() != 0) fScores.delete(0); fScores.append(sScoresClear); show(fScores, cNo, cYes); } else if (command == cYes) { HighScore h = new HighScore(rsName); h.clear(); dThis.setCurrent(fMain); } else if (command == cExit || command == cQuit) { notifyDestroyed(); } else if (command == cMain) { //if (canv.isAlive()) // canv.sureMahaRaisk(); dThis.setCurrent(fMain); } if (command == cBack || command == cOK || command == cNo) { Displayable dCurrent = dThis.getCurrent(); if (command == cBack && (dCurrent == lSettings || dCurrent == fScores)) dThis.setCurrent(fMain); else if (dCurrent == lSpeed || dCurrent == lDifficulty || dCurrent == fLives || dCurrent == fName || dCurrent == fScores) { dThis.setCurrent(lSettings); // valib elemendi, kust ta tuli //lSettings.setSelectedIndex(i, true); <- stiilis 'where sSettings[int i] == dCurrent.toString()' } } } public void show(Displayable d, Command c1, Command c2) { d.addCommand(c1); d.addCommand(c2); d.setCommandListener(this); dThis.setCurrent(d); } public void show(Displayable d) { d.addCommand(cBack); d.addCommand(cOK); d.setCommandListener(this); dThis.setCurrent(d); } public String fillSpaces(String s, int iLength) { while (s.length() < iLength) s = s.concat(" "); return s; } } // asi annab koolis 2.1 versiooniga nullpointerexceptionit, kodus (1.0.3 versiooniga) ei anna class MunaPyydja extends Canvas implements Runnable { // m6ned defaultsid int w = getWidth(), h = getHeight(), pauseMuna = 100, pauseMunadeVahe = 1000, iLives = 5, iAddPoints = 1; String sName = ""; static Purre purded[] = new Purre[4]; static Vector munad = new Vector(); Pyydja pyydja; String rsName = "munaScore"; boolean bDisplayed = true; protected void paint(Graphics g) { int w1 = w / 5, h1 = h / 5; g.setColor(255, 255, 255); g.fillRect(0, 0, w, h); g.setColor(0, 0, 0); pyydja.joonista(g, pyydja.pool, pyydja.yleval); for (int i = 0; i < 4; i++) purded[i].joonista(g); for (int i = 0; i < munad.size(); i++) ((Muna)munad.elementAt(i)).joonista(g); Font f = g.getFont(); int TOP = Graphics.TOP, BOTTOM = Graphics.BOTTOM, RIGHT = Graphics.RIGHT, LEFT = Graphics.LEFT, HCENTER = Graphics.HCENTER; g.drawString(sName, w / 2, 0, TOP|HCENTER); if (pyydja.elusid > 0) { String a = "Punkte: " + pyydja.punkte; g.drawString(a, 0, h, BOTTOM|LEFT); a = "Elusid: " + pyydja.elusid; g.drawString(a, w, h, BOTTOM|RIGHT); } else { String a = "Mäng läbi!"; g.drawString(a, 0, h - f.getHeight(), BOTTOM|LEFT); a = "Punkte said " + pyydja.punkte; g.drawString(a, 0, h, BOTTOM|LEFT); } } public MunaPyydja() { new Thread(this).start(); } public MunaPyydja(int speed, int difficulty, int lives, String name) { // default: MunaPyydja(3, 3, 5, "") sName = name; iLives = lives; iAddPoints = 10; //iAddPoints -= (speed - difficulty); //iAddPoints += difficulty; iAddPoints -= (3 - speed); iAddPoints -= (5 - difficulty); iAddPoints -= (lives > iAddPoints ? iAddPoints : lives); pauseMuna = 300 - (speed * 50); // 250 - 50, intervalliga 50 pauseMunadeVahe = 2500 - (difficulty * 500); // 2000 - 0, intervalliga 500 System.out.println("iSpeed: " + speed + "; iDifficulty: " + difficulty + "; iLives: " + lives + "; sName: '" + name + "'"); new Thread(this).start(); } public void run() { purded[0] = new Purre(0, h / 10, 2, 1); purded[1] = new Purre(w, h / 10, 1, 1); purded[2] = new Purre(0, h / 3, 2, 0); purded[3] = new Purre(w, h / 3, 1, 0); pyydja = new Pyydja(iLives); int last = 1, ri = 1; while (pyydja.elusid > 0 && bDisplayed == true) { if (munad.size() < pyydja.mune) { // et kaks muna yhe purde pealt j2rjest ei tuleks while (ri == last) { Random r = new Random(); ri = Math.abs(r.nextInt() % 4); } last = ri; munad.addElement(new Muna(purded[ri])); ((Muna)munad.lastElement()).start(); } // et munade ilmumise vahel ka aega oleks try { Thread.sleep(pauseMunadeVahe); } catch(Exception e) { e.printStackTrace(); } } try { while (munad.size() > 0) munad.removeElementAt(0); } catch(Exception e) { e.printStackTrace(); } HighScore h = new HighScore(rsName); h.scoreAdd(sName, pyydja.punkte); System.out.println("Got " + pyydja.punkte + " points"); bDisplayed = false; } protected void keyPressed(int keyCode) { if(getGameAction(keyCode) == Canvas.LEFT) { if (pyydja.pool != 1) pyydja.pool = 1; else pyydja.pyydjaX -= 5; } if(getGameAction(keyCode) == Canvas.RIGHT) { if (pyydja.pool != 2) pyydja.pool = 2; else pyydja.pyydjaX += 5; } if(getGameAction(keyCode) == Canvas.UP) { if (pyydja.yleval != 1) pyydja.yleval = 1; else pyydja.pyydjaY -= 5; } if(getGameAction(keyCode) == Canvas.DOWN) { if (pyydja.yleval != 0) pyydja.yleval = 0; else pyydja.pyydjaY += 5; } if(getGameAction(keyCode) == Canvas.FIRE) pyydja.keskele(); repaint(); } class Purre { int w = getWidth(), h = getHeight(), laius = w / 7, k6rgus = laius, serv; // suund: 1 - vasakule; 2 - paremale public int purreX, purreY, suund, pikk; public Purre(int a, int b, int c, int d) { purreX = a; purreY = b; suund = c; pikk = d; } public void joonista(Graphics g) { if (pikk == 1) { laius = w / 4; k6rgus = laius; } serv = (suund == 1 ? purreX - laius : purreX + laius); g.drawLine(purreX, purreY, serv, purreY + k6rgus); } } class Muna extends Thread { int w = getWidth(), h = getHeight(), kukub = 5; // muna asukoht ning tipu asukoht skaalal 360 - neid saab muuta public int munaX, munaY, tipp, laius = w / 10, k6rgus = laius / 2; public Purre purre; public Muna (Purre p) { purre = p; munaX = purre.purreX - (purre.suund == 1 ? laius : 0); munaY = purre.purreY - (purre.suund == 1 ? k6rgus : 0); } public void joonista (Graphics g) { tipp = (tipp + 45) % 180; /* int ww = 0, hh = 0; // ww = laius * Math.cos(tipp) if (tipp == 45) ww = laius - (3 * (laius / 10)); // cos(45) = 0.7 => 0.7 * laius else if (tipp == 90) ww = laius - (0 * laius / 10); else if (tipp == 135) ww = laius - (3 * laius / 10); else if (tipp == 0) ww = laius - (0 * laius / 10); hh = ww / 2; g.drawArc(munaX, munaY, ww, hh, tipp, tipp + 180); g.drawArc(munaX, munaY, ww, hh, tipp, tipp - 180); */ g.drawArc(munaX, munaY, laius, k6rgus, tipp, tipp + 180); g.drawArc(munaX, munaY, laius, k6rgus, tipp, tipp - 180); } void liigu () { // 1 - vasakule; 2 - paremale boolean yleServa = (purre.suund == 1 && munaX + laius < purre.serv || purre.suund == 2 && munaX > purre.serv ? true : false); if (yleServa) munaY += kukub; else { if (purre.suund == 1) munaX--; else if (purre.suund == 2) munaX++; munaY++; } repaint(); } public void run() { boolean korviSees = false; while (munaY + k6rgus <= getHeight() && !korviSees) { liigu(); int MunaJaKorviVahe = munaY - pyydja.korviNurkY; if (MunaJaKorviVahe > 0 && MunaJaKorviVahe <= kukub && munaX >= pyydja.korviNurkX && munaX + laius <= pyydja.korviNurkX + pyydja.korvLaius) korviSees = true; if (korviSees) { pyydja.punkte += iAddPoints; pyydja.mune++; } else if (munaY + k6rgus >= getHeight()) { pyydja.elusid--; break; } try { Thread.sleep(pauseMuna); } catch(Exception e) { e.printStackTrace(); } } MunaPyydja.munad.removeElement(this); } } class Pyydja { int w = getWidth(), h = getHeight(); // v22rtused, mida saab muuta public int pyydjaX, pyydjaY, pool = 1, yleval = 0, punkte = 0, elusid = 5, mune = 1, korviNurkX = 0, korviNurkY = 0; // v22rtused, mis v2lja arvutatakse int algusKehaX, algusKehaY, l6ppKehaX, l6ppKehaY, algusJalgX, algusJalgY, l6ppJalg1X, l6ppJalg2X, l6ppJalg1Y, l6ppJalg2Y, algusK2siX, algusK2siY, l6ppK2si1X, l6ppK2si2X, l6ppK2si1Y, l6ppK2si2Y; // staatilised v22rtused int korvLaius = w / 5, korvK6rgus = h / 4, peaRaadius = w / 10, pikkusKeha = h / 5, pikkusJalg = h / 5, pikkusK2si = w / 10; public Pyydja (int lives) { elusid = lives; keskele(); } public Pyydja() { keskele(); } public void keskele () { pyydjaX = (w / 2) - (peaRaadius / 2); // et pyydja v6imalikult all seisaks //pyydjaY = h - peaRaadius - pikkusKeha - pikkusJalg; // et pyydja purrete keskel seisaks: // pea purrete keskel pyydjaY = (MunaPyydja.purded[0].purreY + MunaPyydja.purded[2].purreY) / 2; // k2e algus purrete keskel pyydjaY += pikkusKeha / 2; } public void joonista (Graphics g, int pool, int yleval, int x, int y) { pyydjaX = x; pyydjaY = y; joonista(g, pool, yleval); } public void joonista (Graphics g, int pool, int yleval) { algusKehaX = pyydjaX + peaRaadius / 2; algusKehaY = pyydjaY + peaRaadius; l6ppKehaX = pyydjaX + peaRaadius / 2; l6ppKehaY = algusKehaY + pikkusKeha; algusJalgX = l6ppKehaX; algusJalgY = l6ppKehaY; l6ppJalg1X = algusJalgX - w / 20; l6ppJalg2X = algusJalgX + w / 20; l6ppJalg1Y = algusJalgY + pikkusJalg; l6ppJalg2Y = algusJalgY + pikkusJalg; algusK2siX = algusKehaX; algusK2siY = algusKehaY + (pikkusKeha / 2); l6ppK2si1X = algusK2siX - (pool == 1 ? (yleval == 1 ? 2 * pikkusK2si : 3 * pikkusK2si) : pikkusK2si / 2); l6ppK2si2X = algusK2siX + (pool == 2 ? (yleval == 1 ? 2 * pikkusK2si : 3 * pikkusK2si) : pikkusK2si / 2); l6ppK2si1Y = algusK2siY - (pool == 1 && yleval == 1 ? h / 15 : -(h / 10)); l6ppK2si2Y = algusK2siY - (pool == 2 && yleval == 1 ? h / 15 : -(h / 10)); g.drawArc(pyydjaX, pyydjaY, peaRaadius, peaRaadius, 0, 360); g.drawLine(algusKehaX, algusKehaY, l6ppKehaX, l6ppKehaY); g.drawLine(algusJalgX, algusJalgY, l6ppJalg1X, l6ppJalg1Y); g.drawLine(algusJalgX, algusJalgY, l6ppJalg2X, l6ppJalg2Y); g.drawLine(algusK2siX, algusK2siY, l6ppK2si1X, l6ppK2si1Y); g.drawLine(algusK2siX, algusK2siY, l6ppK2si2X, l6ppK2si2Y); // korv vastavasse k2tte if (pool == 1) korv(g, l6ppK2si1X - (korvLaius / 2), l6ppK2si1Y - (korvK6rgus / 2)); else if (pool == 2) korv(g, l6ppK2si2X - (korvLaius / 2), l6ppK2si2Y - (korvK6rgus / 2)); } public void korv(Graphics g, int x, int y) { korviNurkX = x; korviNurkY = y; g.drawLine(x, y, x + korvLaius, y); g.fillArc(x, y - (korvK6rgus / 2), korvLaius, korvK6rgus, 180, 180); } } protected void hideNotify() { bDisplayed = false; } } class HighScore { RecordStore rs; RecordEnumeration re; public HighScore(String rsName) { try { rs = RecordStore.openRecordStore(rsName, true); re = rs.enumerateRecords(null, null, false); } catch(Exception e) { e.printStackTrace(); } } public String getValue(int i) { if (i < 1) return ""; String sRet = ""; try { sRet = rs.getRecord(i).toString(); } catch(Exception e) { e.printStackTrace(); } return sRet; } public int getScore(String s) { return Integer.parseInt(s.substring(s.indexOf("::") + 2)); } public String getName(String s) { return s.substring(0, s.indexOf("::")); } public int getScore(int i) { if (i < 1) return -1; String s = ""; try { s = rs.getRecord(i).toString(); } catch(Exception e) { e.printStackTrace(); } return Integer.parseInt(s.substring(s.indexOf("::") + 2)); } public String getName(int i) { if (i < 1) return ""; String s = ""; try { s = rs.getRecord(i).toString(); } catch(Exception e) { e.printStackTrace(); } return s.substring(0, s.indexOf("::")); } public void scoreAdd(String name, int points) { String sScore = name + "::" + Integer.toString(points); try { byte[] b = getBytes(sScore); int smallestId = rs.addRecord(b, 0, b.length); if (rs.getNumRecords() > 10) { int smallestScore = points; while (re.hasNextElement()) { int cur = re.nextRecordId(); String s = Bytes2String(rs.getRecord(cur)); if (getScore(s) < smallestScore) { smallestId = cur; smallestScore = getScore(s); } } rs.deleteRecord(smallestId); } } catch(Exception e) { e.printStackTrace(); } } public void clear() { try { while (re.hasNextElement()) rs.deleteRecord(re.nextRecordId()); } catch(Exception e) { e.printStackTrace(); } } public Vector scoreGetAll() { Vector vReturn = new Vector(); try { while (re.hasNextElement()) { String s = Bytes2String(re.nextRecord()); String[] s1 = { getName(s), String.valueOf(getScore(s)) }; vReturn.addElement(s1); } } catch(Exception e) { e.printStackTrace(); } sort(vReturn); return vReturn; } public byte[] getBytes (String s) { byte[] bReturn = "".getBytes(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeUTF(s); bReturn = bos.toByteArray(); dos.close(); } catch(Exception e) { e.printStackTrace(); } return bReturn; } public String Bytes2String (byte[] bBuffer) { String sReturn = ""; try { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bBuffer)); sReturn = dis.readUTF(); dis.close(); } catch(Exception e) { e.printStackTrace(); } return sReturn; } public void sort(Vector v) { boolean sorted = false; while (!sorted) { sorted = true; for (int i = 0; i < v.size() - 1; i++) { // sort scores String[] cur = (String[])v.elementAt(i); String[] next = (String[])v.elementAt(i + 1); if (Integer.parseInt(next[1]) > Integer.parseInt(cur[1])) { sorted = false; v.setElementAt(next, i); v.setElementAt(cur, i + 1); } } } } protected void Finalize() { try { rs.closeRecordStore(); } catch(Exception e) { e.printStackTrace(); } } }