/* BUGS: kiirus ei muutu; tase 2 ujub läbi; pausi ei oska panna; seina tundlikkust peaks vähendama; */ import java.awt.*; import java.applet.*; public class Uss extends Applet implements Runnable { Image punkt[]=new Image[400]; Image taust; Image offI; Graphics offG; int x[]= new int[400]; int y[]= new int[400]; int suund=1; int game=1; int tase; int z; int n; int arv=0; int skoor=0; int lisa=5; Button b1= new Button("Tase 1"); Button b2= new Button("Tase 2"); Button b3= new Button("Tase 3"); String tempo; String s; String t; boolean go[]=new boolean[400]; boolean left=false; boolean right=false; boolean up=false; boolean down=false; boolean algus=false ; Thread setTime; public void init() { add(b1); add(b2); add(b3); setBackground(Color.black); taust = getImage(getCodeBase(), "taust.gif"); for (z=0 ; z < 200 ; z++) { punkt[z] = getImage(getCodeBase(), "punkt1.gif"); } } public void update(Graphics g) // ei saa aru, mis teeb, aga ilma selleta raibe vilgub { Dimension d = this.size(); if(offI == null) { offI = createImage(d.width, d.height); offG = offI.getGraphics(); } offG.clearRect(0, 0, d.width, d.height); paint(offG); g.drawImage(offI, 0, 0, null); } public void paint(Graphics g) { g.drawImage(taust, 0, 0, this); g.setColor(Color.white); if(algus) { g.setFont(new Font("Verdana", 1, 12)); t = "Skoor >> "+skoor+""; g.drawString(t, 65, 220); } if(game==1) { g.setFont(new Font("Verdana", 1, 14)); s = "Vali Tase >>>"; g.drawString(s, 58, 75); b1.move(213, 60); b2.move(213, 100); b3.move(213, 140); } if((game==2)||(game==3)) { if(!algus) { g.setFont(new Font("Verdana", 1, 11)); t = "Kasuta nooleklahve!"; g.drawString(t, 35, 220); } for (z=0 ; z <= n ; z++) { g.drawImage(punkt[z],x[z],y[z],this); } } if(game==3) { g.setFont(new Font("Verdana", 1, 13)); s="Mäng Läbi!"; g.drawString(s, 65, 60); } } public void run() { for(z=4 ;z <200 ; z++) { go[z]=false; } for(z=0 ; z<4 ; z++) { go[z]=true; x[z]=91; y[z]=91; } n=3; game=2; skoor=0; b1.move(70, -100); b2.move(70, -100); b3.move(70, -100); left=false; right=false; up=false; down=false; locateRandom(4); while(true) { if (game==2) { if ((x[0]==x[n])&&(y[0]==y[n])) { go[n]=true; locateRandom((n+1)); skoor+=lisa; } for(z = 399 ; z > 0 ; z--) { if (go[z]) { x[z] = x[(z-1)]; y[z] = y[(z-1)]; if ((z>4)&&(x[0]==x[z])&&(y[0]==y[z])) { game=3; } } } if(left) // liikumine 10 pixeli kaupa { x[0]-=10; } if(right) { x[0]+=10; } if(up) { y[0]-=10; } if(down) { y[0]+=10; } } if(y[0]>191) // serva kontrollimine, game lopetamine { y[0]=191; game=3; } if(y[0]<1) { y[0]=1; game=3; } if(x[0]>191) { x[0]=191; game=3; } if(x[0]<1) { x[0]=1; game=3; } if (game==3) { if (arv <(1500/tase)) { arv++; } else { arv=0; game=1; repaint(); setTime.stop(); } } repaint(); try { setTime.sleep(tase); // tase defineeritud boolean actionis, (mängu kiirus) } catch(InterruptedException e){} } } public void locateRandom(int nurk) { suund=(int)(Math.random()*20); x[nurk]=((suund*10)+1) ; // pöördenurk suund=(int)(Math.random()*20); y[nurk]=((suund*10)+1); n++; } public boolean keyDown(Event e, int key) { if ((key == Event.LEFT) &&(!right)) { left = true; up = false; down = false; if(!algus) algus=true; } if ((key == Event.RIGHT) && (!left)) { right = true; up = false; down = false; if(!algus) algus=true; } if ((key == Event.UP) && (!down)) { up = true; right = false; left = false; if(!algus) algus=true; } if ((key == Event.DOWN) && (!up)) { down = true; right = false; left = false; if(!algus) algus=true; } return true; } public boolean action(Event event, Object obj) { tempo = (String) obj; if(tempo.equals("Tase 1")) { lisa=5; tase=150; setTime = new Thread(this); setTime.start(); return true; } if(tempo.equals("Tase 2")) // siin on bug (liigub läbi)!!!!!!!!!!!!!!!!!!!! { lisa=10; tase=100; setTime = new Thread(this); setTime.start(); return true; } if(tempo.equals("Tase 3")) { lisa=15; tase=60; setTime = new Thread(this); setTime.start(); return true; } return false; } }