import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; import javax.microedition.midlet.*; import java.io.*; public class ExampleGameCanvas extends GameCanvas implements Runnable{ // Sports Car Directions static final int DIR_UP = 0; static final int DIR_RIGHT = 1; static final int DIR_DOWN = 2; static final int DIR_LEFT = 3; // to be used to transform for each car direction static final int[] sportcarTrans = {Sprite.TRANS_NONE, Sprite.TRANS_ROT90, Sprite.TRANS_ROT180, Sprite.TRANS_ROT270}; //MIDlet parent; Sprite sportcar; TiledLayer track; TiledLayer blocks; int[] normalSequence = {0}; int[] explosionSequence = {1,1,2,2,3,3,4,4,4,5,5,5,5,5,5,6,6,6,6,7,7,7,7, 7,7,7}; // contruct the ExampleGameCanvas public ExampleGameCanvas () { super(true); } public void start(){ /* Create the sportcar object, define the reference pixel and setup the frames */ try { Image sportcarFrame = Image.createImage("/sportFrames.png"); sportcar = new Sprite(sportcarFrame, 16, 22); sportcar.defineReferencePixel(8, 5); sportcar.setFrameSequence(normalSequence); // set to 0 sportcar.setPosition(30, 240); // cteate track and set the cell values Image tiles = Image.createImage("/tiles.png"); track = new TiledLayer(30, 30, tiles, 16, 16); track.fillCells(0, 0, 30, 1, 1); track.fillCells(0, 29, 30, 1, 1); track.fillCells(29, 1, 1, 28, 1); track.fillCells(4, 15, 22, 1, 1); blocks = new TiledLayer(30, 30, tiles, 16, 16); blocks.createAnimatedTile(2); blocks.setCell(3, 3, -1); blocks.setCell(6, 20, -1); blocks.setCell(10, 24, -1); blocks.setCell(16, 18, -1); blocks.setCell(23, 3, -1); blocks.setCell(3, 16, -1); blocks.setCell(16, 25, -1); } catch (IOException ioe) { System.err.println(ioe); } } public void showNotify(){ // start a thread to run the game new Thread(this).start(); } public void run(){ // retrieve graphics to render to the screen buffer Graphics g = getGraphics(); /* create a LayerManager to render the layers add the layers in the proper order */ LayerManager mgr = new LayerManager(); mgr.append(sportcar); mgr.append(track); mgr.append(blocks); // define the window size and its location on the screen int viewOffset = g.getFont().getHeight(); int viewWidth = getWidth(); int viewHeight = getHeight()- viewOffset; // Game state information boolean exploding = false; int lastKeys = 0; int dir = 0; int vx =0; int vy = 0; int blockLeft = 7; // counter used to animate the blocks int blockTile = 0; int score =1; while (true){ // update car direction based on key input int keys = getKeyStates(); if(keys != lastKeys){ if ((keys & LEFT_PRESSED)!= 0){ dir --; } else if ((keys & RIGHT_PRESSED)!= 0){ dir++; } dir = (dir+4) & 0x03; sportcar.setTransform(sportcarTrans[dir]); lastKeys = keys; } // check if the car is exploding if(exploding){ // check if we have reached the end of explosion sequence if (sportcar.getFrame() == explosionSequence.length -1) { return; } // show next frame in the explosion sequence sportcar.nextFrame(); } else { //update te car velocities based on current direction switch (dir){ case DIR_UP: if (vy > -32) vy--; if (vx > 0) vx--; if(vx < 0) vx++; break; case DIR_RIGHT: if (vy < 32) vx++; if (vy > 0) vy--; if(vy < 0) vy++; break; case DIR_DOWN: if(vy < 32) vy++; if(vx > 0) vx--; if(vx < 0) vx++; break; case DIR_LEFT: if(vx > -32) vx--; if(vy > 0) vy--; if(vy < 0) vy++; break; } // move the car based on its velocity sportcar.move(vx >>4, vy >>4); // check if the car hits the track walls if (sportcar.collidesWith(track, true)){ // start explosion sequence sportcar.setFrameSequence(explosionSequence); exploding = true; } // check if the car hits a block if (sportcar.collidesWith(blocks, true)){ // find out which cells intersect and get the overall bounds of the car int xmin = sportcar.getX(); int xmax = xmin +sportcar.getWidth(); int ymin = sportcar.getY(); int ymax = ymin + sportcar.getHeight(); // convert car bounds to grid coordinates and then divide by 16 xmin >>= 4; xmax >>= 4; ymin >>= 4; ymax >>= 4; // check each cell that intersects the car bounds for (int i = xmin; i <= xmax; i++){ for(int j = ymin; j <= ymax; j++){ // check if the cell is not empty if (blocks.getCell(i,j) != 0){ // remove the cells block blocks.setCell(i,j,0); blockLeft--; // increment score score++; } } } if (blockLeft == 0){ // the game is over } } } // Animate the blocks by updating the static tile which is // linked to the animated tile blocks.setAnimatedTile(-1, 2 + ((blockTile++ >> 2) & 0x03)); // adjust the view window so that the car is always at the centre of the screen mgr.setViewWindow(sportcar.getRefPixelX()- (viewWidth/2), sportcar.getRefPixelY() - (viewHeight/2), viewWidth, viewHeight); // Fill the background with colour g.setGrayScale(45); g.fillRect(0, viewOffset, viewWidth, viewHeight); // render the layers mgr.paint(g, 0, viewOffset); // show the score g.setColor (0xFFFFFF); g.fillRect(0, 0, viewWidth, viewOffset); g.setColor(0x000000); // flush the screen flushGraphics(); } } }