MIDP: Game API

download MIDP: Game API

of 27

  • date post

    10-May-2015
  • Category

    Education

  • view

    2.500
  • download

    1

Embed Size (px)

Transcript of MIDP: Game API

  • 1.MIDP: Game API Jussi PohjolainenTAMK University of Applied Sciences

2. Game API

  • It is easy to handle animation and graphics with the Game API
  • All the classes can be found fromjavax.microedition.lcdui.game.*;

3. GameCanvas

  • Using the traditionalCanvas -class
    • You inherit the Canvas and override paint-method.
    • repaint()
    • Event handling is done by using methods like keypressed, keyreleased
  • Using theGameCanvas -class
    • You inherit the GameCanvas-class
    • There is no need for paint-method, you can draw anywhere!
    • flushGraphics()
    • Two ways of doing event handling

4. Example of GameCanvas Usage

  • class MyCanvas extends GameCanvas {
    • public void anymethod(){
      • Graphics g = getGraphics();
      • // some drawing
      • flushGraphics()
    • }
  • }

5. Handling Events

  • Constructor of GameCanvas
    • protected GameCanvas(boolean suppressKeyEvents)
  • You have to call this constructor in you own GameCanvas class..
    • => You have to give a boolean value..
    • true :use only GameCanvases own event handling
    • false :in addition to GameCanvases own event handling use Canvases event handling

6. GameCanvas Usage

  • class MyCanvas extends GameCanvas {
    • public MyCanvas() {
    • // Let's use Game Canvas event handling!
      • super(true);
    • }
    • public void anymethod() {
      • // drawing..
    • }
  • }

7. Event Handling

  • You can ask which button is pressed (GameCanvas Event Handling)
    • public int getKeyStates()
  • Bit-finals of GameCanvas
    • UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED

8. GameCanvas Example 3

  • class MyCanvas extends GameCanvas implements Runnable {
    • public MyCanvas() {
      • super(true);
      • (new Thread(this).start());
    • }
    • public void run() {
      • while(true) {
        • int ks = getKeyStates();
        • if ((ks & UP_PRESSED) != 0)
          • moveUp();
        • else if((ks & DOWN_PRESSED) != 0)
          • moveDown();
        • // Drawing...
      • }
    • }
  • }

9. Layers

  • You can uselayerswith the Game canvas.
  • For example:
    • background-layer (bottom)
    • car-layer (front of the background)
    • And when the user clicks right-command, the car layer is moved one pixel to the right (animation!)
  • javax.microedition.lcdui.game.Layer

10. Layer-class

  • Layer class is abstract and it has two concrete subclasses: 1)TiledLayer , 2)Sprite
  • Layer's methods
    • int getX()
    • int getY()
    • int getWidth()
    • int getHeight()
    • void setPosition(..)
    • move(..)

11. Class Diagram {abstract}Layer int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..) Sprite TiledLayer 12. Mastering the layers

  • Every layer (Sprite or TiledLayer) is put into aLayerManager . TheLayerManageris eventually drawn to the screen.
  • LayerManager's methods
    • append(Layer l)
    • insert(Layer l, int i)
    • Layer getLayer(int i)
    • paint(..)

13. Class Diagram * {abstract}Layer int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..) Sprite TiledLayer LayerManager append(Layer l) insert(Layer l, int i) Layer getLayer(int i) paint(..) 14. LayerManager:setViewWindow

  • public void setViewWindow(int x, int y, int width, int height)
  • What part of a big picture is shown on the screen:

15. Sprite - class

  • Sprite classes constructors:
    • public Sprite(Image i)
    • public Sprite(Image i, int framewidth, int frameheight)

16. Example of Using Sprite and LayoutManager

  • LayerManager l = new LayerManager();
  • Sprite s = new Sprite(myimage);
  • s.setPosition(50,50);
  • l.append(s);
  • Graphics g = getGraphics();
  • l.paint(g,0,0);
  • flushGraphics();

17. Sprite animation

  • Make one image-file, which contains all the frames
  • In the Sprite's constructor you define one frame's height and width
  • After that you can use Sprite'snextFrame()method

18. Example

  • Sprite x = new Sprite(image, 540/18, 30);
  • layermanager.append(x);
  • .
  • .
  • x.nextFrame();

19. With Threads..

  • public void run() {
    • while(true){
      • int ks = getKeyStates();
      • if((ks & RIGHT_PRESSED) != 0){
        • mysprite.move(3,0);
        • mysprite.nextFrame();
      • }
    • }
  • }

20. Influencing frames

  • Changing sequence
    • int sequence [] = {0, 15, 17};
    • mysprite.setFrameSequence(sequence)
  • Jumping to another frame
    • mysprite.setFrame(10);

21. Transformation

  • It is possible to transform the sprite
    • public void setTransform(int transform)
  • Finals
    • TRANS_NONE
    • TRANS_ROT90
    • TRANS_MIRROR
    • .. see the api
  • In practice
    • mysprite.setTransform(Sprite.TRANS_MIRROR)

22. Reference Pixel Reference pixel 23. Reference Pixel Reference pixel mysprite.defineReferencePixel(15,15); 24. Collisions

  • Sprite
    • collidesWith(Sprite s, boolean pixelLevel)
    • collidesWith(TiledLayer s, boolean pixelLevel)
    • collidesWith(Image s, int x, int y, boolean pixelLevel)
  • PixelLevel
    • The rect of the sprite or the "real pixels"

25. Example

  • Sprite x = new Sprite(...);
  • Sprite y = new Sprite(...);
  • if(x.collidesWith(y, true)){
    • // CRASH!
  • }

26. TiledLayer

  • A TiledLayer is a visual element composed of a grid of cells that can be filled with a set of tile images.
  • Rows and Columns
    • TiledLayer(int columns, int rows, Image i, int tileWidth, int tileHeight);

27. Example

  • TiledLayer a = new TiledLayer(4,2, picture, 32, 32);
  • a.setCell(0,1,1); a.setCell(1,1,1), a.setCell(2,1,1);
  • a.setCell(3,1,1);
  • a.setCell(1,0,2);
  • a.setCell(2,0,3);

0 1 2 3 0 1 1 2 3