import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.Sprite; public class MIDPCanvas extends Canvas implements Runnable { private Tank tank; public Sprite auto; //автомобиль public Sprite soldier; //солдат public Sprite plane; //самолет public Sprite explosion; //спрайт взрыва private Image bg,panel; public int score; //очки public Thread t; public MIDPCanvas() { this.setFullScreenMode(true); tank=new Tank(this); try{ auto=new Sprite(Image.createImage("/auto.png"),60,32); auto.setFrameSequence(new int[]{0,0,0,1,1,1}); soldier=new Sprite(Image.createImage("/soldier.png"),25,38); soldier.setFrameSequence(new int[]{0,0,1,1}); plane=new Sprite(Image.createImage("/plane.png"),100,34); explosion=new Sprite(Image.createImage("/explosion.png"),30,30); explosion.setFrameSequence(new int[]{0,0,1,1,2,2,3,3,4,4,4}); explosion.setVisible(false); bg=Image.createImage("/bg.png"); panel=Image.createImage("/panel.png"); }catch(Exception e){} } public void startGame(Display d){ tank.setPosition(5,getHeight()-45); auto.setPosition(getWidth()+100,getHeight()-45); soldier.setPosition(getWidth()+50,getHeight()-45); plane.setPosition(1000+100, -1000); score=0; d.setCurrent(this); t=new Thread(this); t.start(); } public void run(){ while(true){ try{ tank.updateShell(); this.autoUpdate(); this.updateSoldier(); this.updatePlane(); this.updateExplosion(); t.sleep(50-(score)/20); }catch(Exception e){} if (tank.getLive()<=0) { try{ t.sleep(5000); }catch(Exception e){} Main.link.destroyApp(true); } } } public void paint(Graphics g) { g.setColor(255, 255, 255); g.fillRect(0, 0, getWidth(),getHeight()); g.drawImage(bg, 0, this.getHeight()-320, 20); g.drawImage(panel, 0, 0, 20); g.setColor(0, 0, 0); g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL)); g.drawString("Мощность снаряда="+tank.getPowerShell()+" (4-изм.)",this.getWidth()/2,13,Graphics.HCENTER|Graphics.BASELINE); g.drawString("Броня="+tank.getLive()+",очки="+score,this.getWidth()/2,30,Graphics.HCENTER|Graphics.BASELINE ); tank.paint(g); auto.paint(g); soldier.paint(g); plane.paint(g); explosion.paint(g); if (tank.getLive()<=0){ g.drawImage(panel, 0, getHeight()/2,20); g.setColor(0,0,0); g.drawString("Конец игры.",this.getWidth()/2, getHeight()/2+10,Graphics.HCENTER|Graphics.BASELINE); } repaint(); } private void autoUpdate(){ auto.move(-2, 0); auto.nextFrame(); if (tank.collidesWith(auto,true)) { tank.attack(20); explosion.setFrame(0); explosion.setPosition(auto.getX(),auto.getY()+10); explosion.setVisible(true); auto.setPosition(this.getWidth()+100-(score/10), auto.getY()); } } private void updateSoldier(){ soldier.move(-2, 0); soldier.nextFrame(); if (tank.collidesWith(soldier, true)){ tank.attack(5); explosion.setFrame(0); explosion.setPosition(soldier.getX(),soldier.getY()+10); explosion.setVisible(true); soldier.setPosition(this.getWidth()+50-(score/10), soldier.getY()); } } private void updatePlane(){ plane.move(-2,2); plane.nextFrame(); if (tank.collidesWith(plane, true)){ tank.attack(50); explosion.setFrame(0); explosion.setPosition(plane.getX()+50,plane.getY()+30); explosion.setVisible(true); plane.setPosition(1000+100,-1000); } } private void updateExplosion(){ if (explosion.getFrame()