MIDP: Game API

2,804 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,804
On SlideShare
0
From Embeds
0
Number of Embeds
130
Actions
Shares
0
Downloads
68
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MIDP: Game API

  1. 1. MIDP: Game API Jussi Pohjolainen TAMK University of Applied Sciences
  2. 2. Game API <ul><li>It is easy to handle animation and graphics with the Game API </li></ul><ul><li>All the classes can be found from javax.microedition.lcdui.game.*; </li></ul>
  3. 3. GameCanvas <ul><li>Using the traditional Canvas -class </li></ul><ul><ul><li>You inherit the Canvas and override paint-method. </li></ul></ul><ul><ul><li>repaint() </li></ul></ul><ul><ul><li>Event handling is done by using methods like keypressed, keyreleased </li></ul></ul><ul><li>Using the GameCanvas -class </li></ul><ul><ul><li>You inherit the GameCanvas-class </li></ul></ul><ul><ul><li>There is no need for paint-method, you can draw anywhere! </li></ul></ul><ul><ul><li>flushGraphics() </li></ul></ul><ul><ul><li>Two ways of doing event handling </li></ul></ul>
  4. 4. Example of GameCanvas Usage <ul><li>class MyCanvas extends GameCanvas { </li></ul><ul><ul><li>public void anymethod(){ </li></ul></ul><ul><ul><ul><li>Graphics g = getGraphics(); </li></ul></ul></ul><ul><ul><ul><li>// some drawing </li></ul></ul></ul><ul><ul><ul><li>flushGraphics() </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  5. 5. Handling Events <ul><li>Constructor of GameCanvas </li></ul><ul><ul><li>protected GameCanvas(boolean suppressKeyEvents) </li></ul></ul><ul><li>You have to call this constructor in you own GameCanvas class.. </li></ul><ul><ul><li>=> You have to give a boolean value.. </li></ul></ul><ul><ul><li>true : use only GameCanvases own event handling </li></ul></ul><ul><ul><li>false : in addition to GameCanvases own event handling use Canvases event handling </li></ul></ul>
  6. 6. GameCanvas Usage <ul><li>class MyCanvas extends GameCanvas { </li></ul><ul><ul><li>public MyCanvas() { </li></ul></ul><ul><ul><li>// Let's use Game Canvas event handling! </li></ul></ul><ul><ul><ul><li>super(true); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public void anymethod() { </li></ul></ul><ul><ul><ul><li>// drawing.. </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  7. 7. Event Handling <ul><li>You can ask which button is pressed (GameCanvas Event Handling) </li></ul><ul><ul><li>public int getKeyStates() </li></ul></ul><ul><li>Bit-finals of GameCanvas </li></ul><ul><ul><li>UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED </li></ul></ul>
  8. 8. GameCanvas Example 3 <ul><li>class MyCanvas extends GameCanvas implements Runnable { </li></ul><ul><ul><li>public MyCanvas() { </li></ul></ul><ul><ul><ul><li>super(true); </li></ul></ul></ul><ul><ul><ul><li>(new Thread(this).start()); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public void run() { </li></ul></ul><ul><ul><ul><li>while(true) { </li></ul></ul></ul><ul><ul><ul><ul><li>int ks = getKeyStates(); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>if ((ks & UP_PRESSED) != 0) </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>moveUp(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>else if((ks & DOWN_PRESSED) != 0) </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>moveDown(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>// Drawing... </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  9. 9. Layers <ul><li>You can use layers with the Game canvas. </li></ul><ul><li>For example: </li></ul><ul><ul><li>background-layer (bottom) </li></ul></ul><ul><ul><li>car-layer (front of the background) </li></ul></ul><ul><ul><li>And when the user clicks right-command, the car layer is moved one pixel to the right (animation!) </li></ul></ul><ul><li>javax.microedition.lcdui.game.Layer </li></ul>
  10. 10. Layer-class <ul><li>Layer class is abstract and it has two concrete subclasses: 1) TiledLayer , 2) Sprite </li></ul><ul><li>Layer's methods </li></ul><ul><ul><li>int getX() </li></ul></ul><ul><ul><li>int getY() </li></ul></ul><ul><ul><li>int getWidth() </li></ul></ul><ul><ul><li>int getHeight() </li></ul></ul><ul><ul><li>void setPosition(..) </li></ul></ul><ul><ul><li>move(..) </li></ul></ul>
  11. 11. Class Diagram {abstract} Layer int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..) Sprite TiledLayer
  12. 12. Mastering the layers <ul><li>Every layer (Sprite or TiledLayer) is put into a LayerManager . The LayerManager is eventually drawn to the screen. </li></ul><ul><li>LayerManager's methods </li></ul><ul><ul><li>append(Layer l) </li></ul></ul><ul><ul><li>insert(Layer l, int i) </li></ul></ul><ul><ul><li>Layer getLayer(int i) </li></ul></ul><ul><ul><li>paint(..) </li></ul></ul>
  13. 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. 14. LayerManager: setViewWindow <ul><li>public void setViewWindow(int x, int y, int width, int height) </li></ul><ul><li>What part of a big picture is shown on the screen: </li></ul>
  15. 15. Sprite - class <ul><li>Sprite classes constructors: </li></ul><ul><ul><li>public Sprite(Image i) </li></ul></ul><ul><ul><li>public Sprite(Image i, int framewidth, int frameheight) </li></ul></ul>
  16. 16. Example of Using Sprite and LayoutManager <ul><li>LayerManager l = new LayerManager(); </li></ul><ul><li>Sprite s = new Sprite(myimage); </li></ul><ul><li>s.setPosition(50,50); </li></ul><ul><li>l.append(s); </li></ul><ul><li>Graphics g = getGraphics(); </li></ul><ul><li>l.paint(g,0,0); </li></ul><ul><li>flushGraphics(); </li></ul>
  17. 17. Sprite animation <ul><li>Make one image-file, which contains all the frames </li></ul><ul><li>In the Sprite's constructor you define one frame's height and width </li></ul><ul><li>After that you can use Sprite's nextFrame() method </li></ul>
  18. 18. Example <ul><li>Sprite x = new Sprite(image, 540/18, 30); </li></ul><ul><li>layermanager.append(x); </li></ul><ul><li>. </li></ul><ul><li>. </li></ul><ul><li>x.nextFrame(); </li></ul>
  19. 19. With Threads.. <ul><li>public void run() { </li></ul><ul><ul><li>while(true){ </li></ul></ul><ul><ul><ul><li>int ks = getKeyStates(); </li></ul></ul></ul><ul><ul><ul><li>if((ks & RIGHT_PRESSED) != 0){ </li></ul></ul></ul><ul><ul><ul><ul><li>mysprite.move(3,0); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>mysprite.nextFrame(); </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  20. 20. Influencing frames <ul><li>Changing sequence </li></ul><ul><ul><li>int sequence [] = {0, 15, 17}; </li></ul></ul><ul><ul><li>mysprite.setFrameSequence(sequence) </li></ul></ul><ul><li>Jumping to another frame </li></ul><ul><ul><li>mysprite.setFrame(10); </li></ul></ul>
  21. 21. Transformation <ul><li>It is possible to transform the sprite </li></ul><ul><ul><li>public void setTransform(int transform) </li></ul></ul><ul><li>Finals </li></ul><ul><ul><li>TRANS_NONE </li></ul></ul><ul><ul><li>TRANS_ROT90 </li></ul></ul><ul><ul><li>TRANS_MIRROR </li></ul></ul><ul><ul><li>.. see the api </li></ul></ul><ul><li>In practice </li></ul><ul><ul><li>mysprite.setTransform(Sprite.TRANS_MIRROR) </li></ul></ul>
  22. 22. Reference Pixel Reference pixel
  23. 23. Reference Pixel Reference pixel mysprite.defineReferencePixel(15,15);
  24. 24. Collisions <ul><li>Sprite </li></ul><ul><ul><li>collidesWith(Sprite s, boolean pixelLevel) </li></ul></ul><ul><ul><li>collidesWith(TiledLayer s, boolean pixelLevel) </li></ul></ul><ul><ul><li>collidesWith(Image s, int x, int y, boolean pixelLevel) </li></ul></ul><ul><li>PixelLevel </li></ul><ul><ul><li>The rect of the sprite or the &quot;real pixels&quot; </li></ul></ul>
  25. 25. Example <ul><li>Sprite x = new Sprite(...); </li></ul><ul><li>Sprite y = new Sprite(...); </li></ul><ul><li>if(x.collidesWith(y, true)){ </li></ul><ul><ul><li>// CRASH! </li></ul></ul><ul><li>} </li></ul>
  26. 26. TiledLayer <ul><li>A TiledLayer is a visual element composed of a grid of cells that can be filled with a set of tile images. </li></ul><ul><li>Rows and Columns </li></ul><ul><ul><li>TiledLayer(int columns, int rows, Image i, int tileWidth, int tileHeight); </li></ul></ul>
  27. 27. Example <ul><li>TiledLayer a = new TiledLayer(4,2, picture, 32, 32); </li></ul><ul><li>a.setCell(0,1,1); a.setCell(1,1,1), a.setCell(2,1,1); </li></ul><ul><li>a.setCell(3,1,1); </li></ul><ul><li>a.setCell(1,0,2); </li></ul><ul><li>a.setCell(2,0,3); </li></ul>0 1 2 3 0 1 1 2 3

×