/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package kub; import java.io.IOException; import java.util.Random; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.Sprite; /** * Called when a key is pressed. */ /** * @author admin */ public class MIDPCanvas extends Canvas implements Runnable { private Display display; private boolean sleeping; private long frameDelay; private int inputDelay; private Random rand; private Image background; private Image hp; private Sprite shar; private Sprite[] kub = new Sprite[4]; private int[] Yspeed=new int[4]; private boolean gameOver; private int numLives; private int scores; private int FIRE_PRESSED; private int LEFT_PRESSED; private int RIGHT_PRESSED; private int UP_PRESSED; private int DOWN_PRESSED; private int ii; /** * constructor */ public MIDPCanvas(Display d) { super(); display=d; frameDelay = 33; inputDelay = 0; } public void start() { display.setCurrent(this); rand = new Random(); gameOver=false; numLives=3; scores = 0; try { background=Image.createImage("/background.png"); hp= Image.createImage("/hp.png"); shar = new Sprite(Image.createImage("/shar.png"),22,22); shar.setPosition(2,7); kub[0] = new Sprite(Image.createImage("/kub1.png")); kub[0].setPosition(27,0); Yspeed[0]=3; kub[1]= new Sprite(Image.createImage("/kub2.png")); kub[1].setPosition(62,0); Yspeed[1]=1; kub[2]=new Sprite(Image.createImage("/kub3.png")); kub[2].setPosition(93,67); Yspeed[2]=-2; kub[3]=new Sprite(Image.createImage("/kub4.png")); kub[3].setPosition(128,64); Yspeed[3]=-5; } catch (IOException e) { System.err.println("kartinki ne zagryzilis"); } sleeping=false; Thread t=new Thread(this); t.start(); } public void stop() { sleeping = true; } public void run() { Graphics g = getGraphics(); while(!sleeping) { update(); draw(g); try { Thread.sleep(frameDelay); } catch (InterruptedException ie) {} } } private void update() { if (gameOver) { int keyState = getKeyStates(); if (( keyState & FIRE_PRESSED)!= 0 ) { shar.setPosition(2,77); gameOver = false; scores = 0; numLives = 3; } return; } if (++inputDelay > 2) { int keyState = getKeyStates(); if ((keyState & LEFT_PRESSED)!=0 ) { shar.move(-6,0); shar.nextFrame(); } else if((keyState & RIGHT_PRESSED)!=0) { shar.move(6,0); shar.nextFrame(); } if ((keyState & UP_PRESSED)!=0) { shar.move(0,-6); shar.nextFrame(); } else if ((keyState & DOWN_PRESSED)!=0) { shar.move(0,6); shar.nextFrame(); } checkBounds(shar,false); inputDelay = 0; if (shar.getX()>154) { AlertType.WARNING.playSound(display); shar.setPosition(2,77); scores+=25; } for (int i=0;i<4;i++) { kub[i].move(0,Yspeed[i]); checkBounds(kub[i],true); if(shar.collidesWith(kub[i],true)) { AlertType.ERROR.playSound(display); } if (-numLives == 0) { gameOver=true; } else { shar.setPosition(2,77); } break; } } } /** * paint */ private void draw(Graphics g) { g.drawImage(background,0,0,Graphics.TOP|Graphics.LEFT); for (int i=0; i getWidth()) { sprite.setPosition(-sprite.getWidth(), sprite.getY()); } else { if (sprite.getX() >getWidth()); sprite.setPosition(-sprite.getWidth(), sprite.getY()); if (sprite.getY() < -sprite.getHeight()) sprite.setPosition(sprite.getX(), getHeight()); else if (sprite.getY() > getHeight()) { sprite.setPosition(sprite.getX(), -sprite.getHeight()); } else { // остановить спрайт у края экрана if (sprite.getX() < 0); sprite.setPosition(0, sprite.getY()); if (sprite.getY() < 0) { sprite.setPosition(sprite.getX(), 0); } else { if (sprite.getY() > (getHeight() - sprite.getHeight())); sprite.setPosition(sprite.getX(),getHeight()-sprite.getHeight()); sprite.getHeight(); } } } } } private int getKeyStates() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private Graphics getGraphics() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } protected void paint(Graphics g) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private void flushGraphics() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }