MIDP: Game API

27
MIDP: Game API Jussi Pohjolainen TAMK University of Applied Sciences

Transcript of MIDP: Game API

Page 1: MIDP: Game API

MIDP: Game API

Jussi Pohjolainen

TAMK University of Applied Sciences

Page 2: MIDP: Game API

Game API

• It is easy to handle animation and graphics with the Game API

• All the classes can be found from javax.microedition.lcdui.game.*;

Page 3: MIDP: Game API

GameCanvas

• Using the traditional Canvas-class– You inherit the Canvas and override paint-method.– repaint()

– Event handling is done by using methods like keypressed, keyreleased

• Using the GameCanvas-class– You inherit the GameCanvas-class– There is no need for paint-method, you can draw anywhere! – flushGraphics()

– Two ways of doing event handling

Page 4: MIDP: Game API

Example of GameCanvas Usage

class MyCanvas extends GameCanvas {

public void anymethod(){

Graphics g = getGraphics();

// some drawing

flushGraphics()

}

}

Page 5: MIDP: Game API

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

Page 6: MIDP: Game API

GameCanvas Usage

class MyCanvas extends GameCanvas {

public MyCanvas() {

// Let's use Game Canvas event handling!

super(true);

}

public void anymethod() {

// drawing..

}

}

Page 7: MIDP: Game API

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

Page 8: MIDP: Game API

GameCanvas Example 3class 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...

}

}

}

Page 9: MIDP: Game API

Layers

• You can use layers with 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

Page 10: MIDP: Game API

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(..)

Page 11: MIDP: Game API

Class Diagram

{abstract} Layer{abstract} Layer

int getX()int getY()int getWidth()int getHeight()void setPosition(..)move(..)

int getX()int getY()int getWidth()int getHeight()void setPosition(..)move(..)

SpriteSprite TiledLayerTiledLayer

Page 12: MIDP: Game API

Mastering the layers

• Every layer (Sprite or TiledLayer) is put into a LayerManager. The LayerManager is eventually drawn to the screen.

• LayerManager's methods– append(Layer l)– insert(Layer l, int i)– Layer getLayer(int i)– paint(..)

Page 13: MIDP: Game API

Class Diagram

{abstract} Layer{abstract} Layer

int getX()int getY()int getWidth()int getHeight()void setPosition(..)move(..)

int getX()int getY()int getWidth()int getHeight()void setPosition(..)move(..)

SpriteSprite TiledLayerTiledLayer

LayerManagerLayerManager

append(Layer l)insert(Layer l, int i)Layer getLayer(int i)paint(..)

append(Layer l)insert(Layer l, int i)Layer getLayer(int i)paint(..)

*

Page 14: MIDP: Game API

LayerManager: setViewWindow

• public void setViewWindow(int x, int y, int width, int height)

• What part of a big picture is shown on the screen:

Page 15: MIDP: Game API

Sprite - class

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

Page 16: MIDP: Game API

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();

Page 17: MIDP: Game API

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's nextFrame() method

Page 18: MIDP: Game API

Example

Sprite x = new Sprite(image, 540/18, 30);

layermanager.append(x);

.

.

x.nextFrame();

Page 19: MIDP: Game API

With Threads..

public void run() {

while(true){

int ks = getKeyStates();

if((ks & RIGHT_PRESSED) != 0){

mysprite.move(3,0);

mysprite.nextFrame();

}

}

}

Page 20: MIDP: Game API

Influencing frames

• Changing sequence– int sequence [] = {0, 15, 17};– mysprite.setFrameSequence(sequence)

• Jumping to another frame– mysprite.setFrame(10);

Page 21: MIDP: Game API

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)

Page 22: MIDP: Game API

Reference Pixel

Reference pixel

Page 23: MIDP: Game API

Reference Pixel

Reference pixel

mysprite.defineReferencePixel(15,15);

Page 24: MIDP: Game API

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"

Page 25: MIDP: Game API

Example

Sprite x = new Sprite(...);

Sprite y = new Sprite(...);

if(x.collidesWith(y, true)){// CRASH!

}

Page 26: MIDP: Game API

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);

Page 27: MIDP: Game API

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