import javax.microedition.media.*; import javax.microedition.media.control.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; /* autor Writer */ public class Game extends Canvas implements Runnable { int w,h,x,y,LifeHero,ArmorHero; boolean up,down,left,right,anim,b19i; Image menu1,life,armor; Player pl,pl1,pl2; Tile t; Tile1 t1; Tile2 t2; Tile3 t3; TiledLayer tile,tile1,tile2,tile3; Hero hero; Mine mine; B19 b19; LayerManager lm; public Game() { setFullScreenMode(true); w = getWidth(); h = getHeight(); this.w = w; this.h = h; LifeHero = 100; ArmorHero = 100; try { hero = new Hero(Image.createImage("/.png/Hero/hero.png")); mine = new Mine(Image.createImage("/.png/Mine/mine.png")); b19 = new B19(Image.createImage("/.png/B19/b19.png")); menu1 = Image.createImage ("/.png/Menu/menu1.png"); life = Image.createImage ("/.png/Menu/life.png"); armor = Image.createImage ("/.png/Menu/armor.png"); pl = Manager.createPlayer (getClass() .getResourceAsStream("/PLR/puly.mid"), "audio/midi"); pl.realize(); pl.prefetch(); pl.setLoopCount(1); pl1 = Manager.createPlayer (getClass() .getResourceAsStream("/PLR/mine.mid"), "audio/midi"); pl1.realize(); pl1.prefetch(); pl1.setLoopCount(1); pl2 = Manager.createPlayer (getClass() .getResourceAsStream("/PLR/b19.mid"), "audio/midi"); pl2.realize(); pl2.prefetch(); pl2.setLoopCount(1); }catch (Exception ex) { ex.printStackTrace(); } t = new Tile(); tile = t.getTiledLayer(); t1 = new Tile1(); tile1 = t1.getTiledLayer(); t2 = new Tile2(); tile2 = t2.getTiledLayer(); t3 = new Tile3(); tile3 = t3.getTiledLayer(); lm = new LayerManager(); lm.append(hero); lm.append(mine); lm.append(b19); lm.append(tile); lm.append(tile1); lm.append(tile2); lm.append(tile3); new Thread(this).start(); } protected void paint(Graphics g) { g.setColor(255,255,255); g.fillRect(0,0,w,h); lm.paint(g, x, y); g.drawImage(menu1, 0, 298, 0); g.drawImage(life, 5, 300, 0); g.drawImage(armor, 70, 300, 0); g.drawString(" " +LifeHero, 20, 295, 20); g.drawString(" " +ArmorHero, 85, 295, 20); } public void keyPressed(int keyCode) { if (keyCode == KEY_NUM2) {up = true;} if (keyCode == -1) {up = true;} if (keyCode == KEY_NUM8) {down = true;} if (keyCode == -2) {down = true;} if (keyCode == KEY_NUM4) {left = true;} if (keyCode == -3) {left = true;} if (keyCode == KEY_NUM6) {right = true;} if (keyCode == -4) {right = true;} if (keyCode == KEY_NUM5) { try { pl.start(); }catch(MediaException ex){}; } if (keyCode == -5) { try { pl.start(); }catch(MediaException ex){}; } } public void keyRepeated(int keyCode) { keyPressed(keyCode); } public void keyReleased(int keyCode) { if (keyCode == KEY_NUM2) {up = false;} if (keyCode == -1) {up = false;} if (keyCode == KEY_NUM8) {down = false;} if (keyCode == -2) {down = false;} if (keyCode == KEY_NUM4) {left = false;} if (keyCode == -3) {left = false;} if (keyCode == KEY_NUM6) {right = false;} if (keyCode == -4) {right = false;} } public void run() { while (true) { if (up) {hero.up();} if (down) {hero.down();} if (left) {hero.left();} if (right) {hero.right();} if ((hero.getX() > w / 2 + x * -1) && right) {x--;} if ((hero.getY() < h / 2 + y * -1) && up) {y++;} if ((hero.getX() < w / 2 + x * -1) && left) {x++;} if ((hero.getY() > h / 2 + y * -1) && down) {y--;} if (x < -tile.getWidth() + w) { x=-tile.getWidth()+w; } if (x > 0) {x = 0;} if (y < -tile.getHeight() + h) { y = -tile.getHeight() + h; } if (y > 0) {y = 0;} /*Мой код.*/ if (hero.collidesWith(mine, false)) { if (LifeHero > 0) { LifeHero--; } try { pl1.start(); }catch(MediaException ex){}; mine.nextFrame(); } if (hero.collidesWith(b19, false)) { if (ArmorHero > 0) { ArmorHero--; } try { pl2.start(); }catch(MediaException ex){}; b19.nextFrame(); } /*Конец кода.*/ if (hero.collidesWith(tile, false)) { if (up) {hero.down();} if (down) {hero.up();} if (left) {hero.right();} if (right) {hero.left();} } repaint(); try { Thread.sleep(5); }catch (Exception ex){} }}}