Bài giảng Lập trình j2me cho thiết bị di động phần 5 - Trịnh Thị Vân Anh

ppt 12 trang huongle 3690
Bạn đang xem tài liệu "Bài giảng Lập trình j2me cho thiết bị di động phần 5 - Trịnh Thị Vân Anh", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptbai_giang_lap_trinh_j2me_cho_thiet_bi_di_dong_phan_5_trinh_t.ppt

Nội dung text: Bài giảng Lập trình j2me cho thiết bị di động phần 5 - Trịnh Thị Vân Anh

  1. LẬPLẬP TRÌNHTRÌNH J2MEJ2ME CHOCHO THIẾTTHIẾT BỊBỊ DIDI ĐỘNGĐỘNG PHẦNPHẦN 55 Giáo viên : Trịnh Thị Vân Anh Hà nội, 8-2007
  2. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorBasicMenu (1)(1) n Basic Main Menu import javax.microedition.lcdui.*; addCommand(exitCommand); public class MainMenuScreen extends List addCommand(selectCommand); implements CommandListener { setCommandListener(this); private Eliminator midlet; } private Command selectCommand = new public void commandAction(Command c, Command("Select", Command.ITEM,1); Displayable d) { private Command exitCommand = new if (c == exitCommand) { Command("Exit", Command.EXIT,1); midlet.mainMenuScreenQuit(); private Alert alert; return; public MainMenuScreen(Eliminator midlet) { } else if (c == selectCommand) { super("Eliminator",Choice.IMPLICIT); processMenu(); return; this.midlet = midlet; } else { append("New Game",null); processMenu(); return; append("Settings",null); } append("High Scores", null); } append("Help",null); append("About",null); 2
  3. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorBasicMenu (2)(2) private void processMenu() { alert = new try { Alert("Settings","Settings ",null,null); List down = (List)midlet.display.getCurrent(); alert.setTimeout(Alert.FOREVER); switch (down.getSelectedIndex()) { case 0: scnNewGame(); break; alert.setType(AlertType.INFO); case 1: scnSettings(); break; midlet.mainMenuScreenShow(alert); case 2: scnHighScores(); break; } case 3: scnHelp(); break; case 4: scnAbout(); break;}; private void scnHighScores() { } catch (Exception ex) { alert = new Alert("High Scores" // Proper Error Handling should be done here ,"High Scores ",null,null); System.out.println("processMenu::"+ex);} } alert.setTimeout(Alert.FOREVER); private void scnNewGame() { alert.setType(AlertType.INFO); midlet.mainMenuScreenShow(null); } midlet.mainMenuScreenShow(alert); private void scnSettings() { } 3
  4. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorBasicMenu (3)(3) private void scnHelp() { alert = new Alert("Help","Help ",null,null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); midlet.mainMenuScreenShow(alert); } private void scnAbout() { alert = new Alert("About","Eliminator\nVersion 1.0.0\nby Jason Lam",null,null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); midlet.mainMenuScreenShow(alert); } } 4
  5. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorBasicMenu (4)(4) n Main Midlet Source Code: import javax.microedition.midlet.*; isSplash = false; import javax.microedition.lcdui.*; try { public class Eliminator extends MIDlet { splashLogo =Image.createImage("/splash.png"); protected Display display; new SplashScreen(display, mainMenuScreen, splashLogo,3000); private Image splashLogo; } catch(Exception ex) { private boolean isSplash = true; mainMenuScreenShow(null); MainMenuScreen mainMenuScreen; } public Eliminator() {} } else { public void startApp() { mainMenuScreenShow(null); display = Display.getDisplay(this); } mainMenuScreen = new MainMenuScreen(this); } if(isSplash) { 5
  6. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorBasicMenu (5)(5) public Display getDisplay() { public void mainMenuScreenShow(Alert alert) return display;} { public void pauseApp() {} if (alert==null) public void destroyApp(boolean unconditional) display.setCurrent(mainMenuScreen); { else System.gc(); display.setCurrent(alert,mainMenuScreen); notifyDestroyed(); } } public void mainMenuScreenQuit() { private Image createImage(String filename) { destroyApp(true); Image image = null; } try { } image = Image.createImage(filename); } catch (Exception e) { }return image; } 6
  7. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorSubMenu (1)(1) private void scnNewGame() { midlet.mainMenuScreenShow(); } private void scnSettings() { midlet.settingsScreenShow(); } private void scnHighScore() { midlet.highScoreScreenShow(); } private void scnHelp() { midlet.helpScreenShow(); } private void scnAbout() { midlet.aboutScreenShow(); } } 7
  8. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorSubMenu (2)(2) n High Score Screen Source Code: import javax.microedition.lcdui.*; public class HighScoreScreen extends Form implements CommandListener { private Eliminator midlet; private Command backCommand = new Command("Back", Command.BACK,1); private Command resetCommand = new Command("Rest", Command.SCREEN,1); public HighScoreScreen (Eliminator midlet) { super("High Score"); this.midlet = midlet; StringItem stringItem = new StringItem(null,"JL 100\nJL 50\nJL 10"); append(stringItem); addCommand(backCommand); addCommand(resetCommand); setCommandListener(this); } public void commandAction(Command c, Displayable d) { if (c == backCommand) { midlet.mainMenuScreenShow(); return; }if (c == resetCommand) { // not implemented yet System.out.println("Reset High Scores Not Implemented Yet"); }}} 8
  9. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorSubMenu (3)(3) n Help Screen Source Code: import javax.microedition.lcdui.*; public class HelpScreen extends Form implements CommandListener { private Eliminator midlet; private Command backCommand = new Command("Back", Command.BACK, 1); public HelpScreen (Eliminator midlet) { super("Help"); this.midlet = midlet; StringItem stringItem = new StringItem(null,"It is the year 3023, many things have changed over the years " + ); append(stringItem); addCommand(backCommand); setCommandListener(this); } public void commandAction(Command c, Displayable d) { if (c == backCommand) { midlet.mainMenuScreenShow(); return; }}} 9
  10. Eliminator:Eliminator: GameGame Menu,Menu, EliminatorSubMenu (4)(4) n About Screen Source Code: import javax.microedition.lcdui.*; public class AboutScreen extends Form implements CommandListener { private Eliminator midlet; private Command backCommand = new Command("Back", Command.BACK, 1); public AboutScreen (Eliminator midlet) { super("About"); this.midlet = midlet; StringItem stringItem = new StringItem(null,"Eliminator\nVersion 1.0.0\nBy Jason Lam"); append(stringItem); addCommand(backCommand); setCommandListener(this); } public void commandAction(Command c, Displayable d) { if (c == backCommand) { midlet.mainMenuScreenShow(); return; }}} 10
  11. Eliminator:Eliminator: TerrainTerrain (Scrolling(Scrolling Background)Background) private TiledLayer loadTerrain() throws Exception { {6,0,0,0,1,2}, {6,0,0,0,4,5}, {6,0,0,0,7,8}, Image tileImages = {6,0,0,0,0,0}, {9,0,1,2,3,0}, {0,0,4,5,6,0}, Image.createImage("/terrain.png"); {0,0,7,8,9,0}, {0,0,0,0,0,0},{0,0,0,0,0,0}, TiledLayer tiledLayer = new {0,0,0,0,0,0},{3,0,0,0,0,0}, {6,0,0,0,0,0}, TiledLayer(TILE_NUM_COL,TILE_NUM {6,0,0,0,1,2},{6,0,0,0,4,5}, {6,0,0,0,7,8}, _ROW,tileImages,TILE_WIDTH,TILE_H {6,0,0,0,0,0},{9,0,0,0,0,0}, {0,0,0,0,0,0}, EIGHT); {0,0,0,0,0,0},{0,0,0,0,0,0}, {3,0,0,0,0,1} // Define Terrain Map }; int[][] map = { // Map Terrain Map with actual graphic from {0,0,0,0,0,0}, {3,0,0,0,0,0}, {6,0,0,0,0,0}, terrain.png {6,0,0,0,1,2}, {6,0,0,0,4,5}, {6,0,0,0,7,8}, for (int row=0; row<TILE_NUM_ROW; row++) {6,0,0,0,0,0},{9,0,1,2,3,0}, {0,0,4,5,6,0}, { {0,0,7,8,9,0},{0,0,0,0,0,0}, {0,0,0,0,0,0}, for (int col=0; col<TILE_NUM_COL; col++) { {0,0,0,0,0,0},{3,0,0,0,0,0}, {6,0,0,0,0,0}, tiledLayer.setCell(col,row,map[row][col]); } }return tiledLayer; } nVí dụ: EliminatorScrolling 11
  12. Eliminator:Eliminator: PlayerPlayer ,, vví dụ : EliminatorPlayer n Player Sprite public class PlayerSprite extends Sprite { public void startPosition() { private static final int MOVE = 3; setPosition(scnWidth/2,scnHeight/2);} private int x,y; public void moveLeft() { private int scnWidth,scnHeight; getXY(); private int frameWidth, frameHeight; if (x - MOVE > 0) private int frame; move(MOVE * -1,0);} private int lives; public void moveRight() { public PlayerSprite(Image image, int frameWidth, getXY(); int frameHeight, int scnWidth, int scnHeight) if (x + MOVE + frameWidth 0) this.scnWidth = scnWidth; move(0,MOVE * -1);} this.scnHeight = scnHeight; public void moveDown() { this.frameWidth = frameWidth; getXY(); this.frameHeight = frameHeight; if (y + MOVE + frameHeight < scnHeight) this.frame = 1; this.lives = 3;} move(0,MOVE);} 12