package bmp; import javax.microedition.media.*; import javax.microedition.media.control.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; import java.io.*; /* Author: Writer */ /* Наш канвас. */ public class Game extends Canvas implements Runnable { int w, h, LifeHero, ArmorHero; int time = 5; boolean up, down, left, right; boolean bot_up, bot_down, bot_left, bot_right; boolean bot_iq; Image menu1, life, armor; Player pl; Tile t; Tile1 t1; TiledLayer tile, tile1; Hero hero; Bot bot; Mine mine; LayerManager lm; public Game() { super(); 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")); bot = new Bot(Image.createImage("/.png/Bot/bot.png")); mine = new Mine(Image.createImage("/.png/Mine/mine.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("/sounds/mine.mid"), "audio/midi"); pl.realize(); pl.prefetch(); pl.setLoopCount(1); }catch(Exception ex) {ex.printStackTrace();} t = new Tile(); tile = t.getTiledLayer(); t1 = new Tile1(); tile1 = t1.getTiledLayer(); lm = new LayerManager(); lm.append(hero); lm.append(bot); lm.append(mine); lm.append(tile); lm.append(tile1); new Thread(this).start(); } protected void paint(Graphics g) { g.setColor(255, 255, 255); g.fillRect(0, 0, w, h); lm.paint(g, Hero.x, Hero.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); g.drawString("x:" +Hero.x, 5, 80, 20); g.drawString("y:" +Hero.y, 5, 120, 20); g.drawString("bot_x:" +Bot.bot_x, 5, 160, 20); g.drawString("bot_y:" +Bot.bot_y, 5, 200, 20); g.drawString("bot_iq:" +bot_iq, 5, 240, 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;} } 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 + Hero.x * -1) && right) {Hero.x--;} if ((hero.getY() < h / 2 + Hero.y * -1) && up) {Hero.y++;} if ((hero.getX() < w / 2 + Hero.x * -1) && left) {Hero.x++;} if ((hero.getY() > h / 2 + Hero.y * -1) && down) {Hero.y--;} if (Hero.x < -tile.getWidth() + w) { Hero.x=-tile.getWidth()+w; } if (Hero.x > 0) {Hero.x = 0;} if (Hero.y < -tile.getHeight() + h) { Hero.y=-tile.getHeight()+h; } if (Hero.y > 0) {Hero.y = 0;} if (bot_up) {bot.bot_up();} if (bot_down) {bot.bot_down();} if (bot_left) {bot.bot_left();} if (right) {hero.right();} if ((bot.getX() > w / 2 + Bot.bot_x * -1) && right) {Bot.bot_x--;} if ((bot.getY() < h / 2 + Bot.bot_y * -1) && up) {Bot.bot_y++;} if ((bot.getX() < w / 2 + Bot.bot_x * -1) && left) {Bot.bot_x--;} if ((bot.getY() > h / 2 + Bot.bot_y * -1) && down) {Bot.bot_y++;} if (Bot.bot_x<-tile.getWidth()+w) { Bot.bot_x=-tile.getWidth()+w; } if (Bot.bot_x > 0) {Bot.bot_x = 0;} if (Bot.bot_y<-tile.getHeight()+h) { Bot.bot_y=-tile.getHeight()+h; } if (Bot.bot_y > 0) {Bot.bot_y = 0;} if(Hero.x > Bot.bot_x-50 && Hero.x < Bot.bot_x+50 && Hero.y > Bot.bot_y-50 && Hero.y < Bot.bot_y+50) { bot_iq = true; } else { bot_iq = false; } if (bot_iq) { if (Bot.bot_x <= Hero.x) {bot_right = true;} if (Bot.bot_x >= Hero.x) {bot_left = true;} if (Bot.bot_y <= Hero.y) {bot_down = true;} if (Bot.bot_y >= Hero.y) {bot_up = true;} } if (hero.collidesWith(mine, false)) { if (ArmorHero > 0) {ArmorHero -= 1;} else if (LifeHero > 0) {LifeHero -= 4;} try { pl.start(); }catch(Exception ex) {ex.printStackTrace();}; mine.nextFrame(); } if (hero.collidesWith(tile, false)) { if (up) {hero.down();} if (down) {hero.up();} if (left) {hero.right();} if (right) {hero.left();} } if (bot.collidesWith(tile, false)) { if (bot_up) { bot.bot_down(); bot_up = false; } if (bot_down) { bot.bot_up(); bot_down = false; } if (bot_left) { bot.bot_right(); bot_left = false; } if (bot_right) { bot.bot_left(); bot_right = false; } } repaint(); try { Thread.sleep(time); }catch (Exception ex){} }}}