본문 바로가기

카테고리 없음

Android Soft KeyPad and Airplane control

Android Soft KeyPad and Airplane control

안드로이드 화면에 키패드와 비행기를 그려넣고 키패드를 이용하여 비행기의 위치를 변경(이동)하는 예

다른 구성 파일은 여기를 참고하세요(http://micropilot.tistory.com/category/Android%20Game/Game%20Framework%2008)

키패드와 비행기 이미지 http://micropilot.tistory.com/category/Android%20Game/Direction%20key%20pad

GameState.java

package game.framework;


import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.util.Log;

import android.view.KeyEvent;

import android.view.MotionEvent;


public class GameState implements IState {


GameKeyPad keyPad;

Airplane airplane;

public GameState() {

keyPad = new GameKeyPad();

airplane = new Airplane();

}

@Override

public void init() {   }


@Override

public void destroy() { }


@Override

public void update() {

airplane.update();

}


@Override

public void render(Canvas canvas) {

airplane.draw(canvas);

keyPad.draw(canvas);

}


@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

return false;

}


@Override

public boolean onTouchEvent(MotionEvent event) {

return keyPad.onTouchEvent(event);

}

}


GameKeyPad.java

package game.framework;


import android.graphics.*;

import android.util.Log;

import android.view.MotionEvent;

/* 화면에 키패를 그려주고 그 키패드에서 발생하는 터치 이벤트를 처리한다 */

public class GameKeyPad extends GraphicObject {


Paint paint;

Rect leftRect, topRect, rightRect, bottomRect,centerRect;

int x, y;


public GameKeyPad(){

super(AppManager.getInstance().getBitmap(R.drawable.gamepad));

paint = new Paint();

paint.setAlpha(150);

leftRect = new Rect(21, 54, 56, 90);

topRect = new Rect(55, 22, 90, 56);

rightRect = new Rect(90, 56, 123, 90);

bottomRect = new Rect(56, 90, 91, 125);

centerRect = new Rect(59, 59, 88, 88);

}


public GameKeyPad(int kpx, int kpy) {

super(AppManager.getInstance().getBitmap(R.drawable.gamepad));

paint = new Paint();

paint.setAlpha(150);


leftRect = new Rect(kpx+21, kpy+54, kpx+56, kpy+90);

topRect = new Rect(kpx+55, kpy+22, kpx+90, kpy+56);

rightRect = new Rect(kpx+90, kpy+56, kpx+123, kpy+90);

bottomRect = new Rect(kpx+56, kpy+90, kpx+91, kpy+125);

centerRect = new Rect(kpx+59, kpy+59, kpx+88, kpy+88);

}


/** 생성자 호출 후에는 이 메소드를 호출하여 키패드의 화면상의 위치를 지정할 수 있다.

*  그렇지 않으면 화면의 좌하단 구석에 키패드가 위치하게 된다.

*/

public void setPosition(int x, int y) {

this.x = x;

this.y = y;

leftRect.left+=x; leftRect.top+=y; leftRect.right+=x; leftRect.bottom+=y;

topRect.left+=x; topRect.top+=y; topRect.right+=x; topRect.bottom+=y;

rightRect.left+=x; rightRect.top+=y; rightRect.right+=x; rightRect.bottom+=y;

bottomRect.left+=x; bottomRect.top+=y; bottomRect.right+=x; bottomRect.bottom+=y;

centerRect.left+=x; centerRect.top+=y; centerRect.right+=x; centerRect.bottom+=y;

}


public void draw(Canvas canvas) {

if(x==0 && y==0) setPosition (0,canvas.getHeight()-bitmap.getHeight()); // 좌하단 구석

canvas.drawBitmap(bitmap, x, y, paint);

}


public boolean onTouchEvent(MotionEvent event) {

int x = (int)event.getX();

int y = (int)event.getY();

Airplane airplane = AppManager.getInstance().getAirplane();

int action = event.getAction();

if(centerRect.contains(x, y)){

Log.i("키패드 이벤트", "Fire !");

return false;

}

else if(action==event.ACTION_DOWN) {

Log.i("터치이벤트", "다운");

if(leftRect.contains(x,y)) airplane.setSpeed(-2, 0);

else if(topRect.contains(x,y)) airplane.setSpeed(0, -2);

else if(rightRect.contains(x,y)) airplane.setSpeed(2, 0);

else if(bottomRect.contains(x,y)) airplane.setSpeed(0, 2);

}else if(action==event.ACTION_UP) {

airplane.setSpeed(0, 0);

}


return true;

}

}


Airplane.java

package game.framework;


import android.graphics.*;


public class Airplane extends GraphicObject {


int speedX, speedY;

public Airplane() {

super(AppManager.getInstance().getBitmap(R.drawable.mustang_transparent));

AppManager.getInstance().setAirplane(this);

}

public Airplane(Bitmap bitmap) {

super(bitmap);

AppManager.getInstance().setAirplane(this);

}


public void setSpeed(int speedX, int speedY) {

this.speedX = speedX;

this.speedY = speedY;

}

public void update() {

if(this.x==0 && this.y==0) return;

x += speedX;

y += speedY;

}

public void draw(Canvas canvas) {

if(this.x==0 && this.y==0) {

this.x = canvas.getWidth()/2-bitmap.getWidth()/2;

this.y = canvas.getHeight()-bitmap.getHeight();

}

canvas.drawBitmap(bitmap, x, y, null);

}

}


AppManager.java

package game.framework;


import android.content.res.*;

import android.graphics.*;


public class AppManager {


private GameView gameView;

private Resources resources;

private Airplane airplane;

private AppManager() {}

void setGameView(GameView gameView) {

this.gameView = gameView;

}

void setResources(Resources resources ){

this.resources = resources;

}


public GameView getGameView() {

return gameView;

}


public Resources getResources() {

return resources;

}

public Bitmap getBitmap(int resId) {

return BitmapFactory.decodeResource(getResources(), resId);

}

public Airplane getAirplane() {

return airplane;

}


public void setAirplane(Airplane airplane) {

this.airplane = airplane;

}


private static AppManager instance;

public static AppManager getInstance() {

if(instance==null) instance = new AppManager();

return instance;

}

}