import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; public class MainGameCanvas extends GameCanvas implements Runnable { // midlet private GameMidlet midlet = null; // graphic context private Graphics graphics = null; // thread private volatile Thread animationThread = null; // manager Game private LayerManager gameManager = null; // background game private Background background = null; // ship private Ship ship = null; // boll private Boll boll = null; // fps private int fps = 50; // screen width int screenWidth = 0; // screen height int screenHeight = 0; public MainGameCanvas(GameMidlet midlet) throws Exception { // constructor super class super(true); // midlet this.midlet = midlet; // full screen game setFullScreenMode(true); // graphic context graphics = getGraphics(); // screen width screenWidth = this.getWidth(); // screen height screenHeight = this.getHeight(); // creation Layer Manager gameManager = new LayerManager(); // creating game createGame(); } public void start() { // new thread for game animationThread = new Thread(this); // start thread animationThread.start(); } public void run() { Thread currentThread = Thread.currentThread(); try { while (currentThread == animationThread) { long startTime = System.currentTimeMillis(); if (isShown()) { updateGame(); draw(); flushGraphics(); } long endTime = System.currentTimeMillis() - startTime; if (endTime < fps) { synchronized (this) { wait(fps - endTime); } } else { currentThread.yield(); } } } catch (InterruptedException ie) { } } public void stop() { // stop animations animationThread = null; } public void keyPressed(int keyCode) { if(keyCode == -6 || keyCode == -7) { this.stop(); midlet.exitGame(); } } private void draw() { graphics.setColor(0, 0, 0); graphics.fillRect(0, 0, screenWidth, screenHeight); gameManager.paint(graphics, 0, 0); // draw number AI boll.drawAIBoll(graphics); // draw position ship and boll graphics.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL)); graphics.drawString("Boll X = " + boll.getX(), 2 , Font.getDefaultFont().getHeight(), 0); graphics.drawString("Boll Y = " + boll.getY(), 2 , Font.getDefaultFont().getHeight() * 2, 0); graphics.drawString("Ship X = " + ship.getX(), 2 , Font.getDefaultFont().getHeight() * 3, 0); graphics.drawString("Ship Y = " + ship.getY(), 2 , Font.getDefaultFont().getHeight() * 4, 0); // draw rect boll graphics.drawRect(boll.getX() - 40, boll.getY() - 40, boll.WIDTH + 80, boll.HEIGHT + 80); } public void createGame() throws Exception { //********************************************** // creating background //********************************************** try{ Image imageBackground = Image.createImage("/Background.png"); background = new Background(Background.COLUMNS_WIDTH, Background.ROWS_HEIGHT, imageBackground, Background.WIDTH, Background.HEIGHT); background.setVisible(false); imageBackground = null; }catch (Exception ex) { System.err.println("Background it is not loaded"); } //********************************************** // creating ship //********************************************** try{ Image imageShip = Image.createImage("/Ship.png"); ship = new Ship(this, imageShip, Ship.WIDTH, Ship.HEIGHT); ship.setVisible(false); imageShip = null; }catch (Exception ex) { System.err.println("Ship.png it is not loaded"); } //********************************************** // creating boll //********************************************** try{ Image imageBoll = Image.createImage("/Boll.png"); boll = new Boll(this, imageBoll, 16, 24); boll.setVisible(false); imageBoll = null; }catch (Exception ex) { System.err.println("Boll.png it is not loaded"); } } public void setGame(){ //********************************************** // background //********************************************** // create background background.createBackground(); // set position background background.setPosition(0, -screenHeight); // background visible background.setVisible(true); //********************************************** // ship //********************************************** // set position ship ship.setPosition(screenWidth/2 - 30, screenHeight - 68); // ship visible ship.setVisible(true); //********************************************** // boll //********************************************** // set position boll boll.setPosition(screenWidth/2 - boll.WIDTH/2, screenHeight/2 - boll.HEIGHT/2); // boll visible boll.setVisible(true); //********************************************** // add component to Game //********************************************** // add ship gameManager.append(ship); // add boll gameManager.append(boll); // add background gameManager.append(background); } public void updateGame() { //********************************************** // user input //********************************************** int keyStates = getKeyStates(); // move down if ((keyStates & DOWN_PRESSED) != 0) { ship.moveShip(1); } else if ((keyStates & UP_PRESSED) != 0) { ship.moveShip(2); } else if ( (keyStates & LEFT_PRESSED) != 0) { ship.moveShip(3); } else if ((keyStates & RIGHT_PRESSED) !=0 ) { ship.moveShip(4); // stop } else { ship.moveShip(0); } //********************************************** // move //********************************************** // move boll boll.moveBoll(ship); // move background background.moveBackground(screenHeight); } }