Java中View游戏开发框架
java中游戏开发引擎View比较适合被动触发的游戏,不能使用于那种对战的游戏
Game01Activity.java 这里是调用的activity
package cn.sun.syspro; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class Game01Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); //隐藏定格 this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow() .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(new Myview(this)); } }
Myview.java
package cn.sun.syspro; import android.R.style; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; public class Myview extends View{ /** * 重写父类构造函数 * @param context */ private int x = 200,y = 200; public Myview(Context context) { super(context); setFocusable(true); // TODO Auto-generated constructor stub } /** * 重写绘图函数 */ @Override protected void onDraw(Canvas canvas){ //创建一个画笔 Paint paint = new Paint(); //设置画笔的颜色 paint.setColor(Color.BLACK); //绘制文本 canvas.drawRGB(250, 224, 240); canvas.drawText("Hello Sun",this.x,this.y, paint); super.onDraw(canvas); } /** * 重写按下事件 */ @Override public boolean onKeyDown(int keyCode,KeyEvent event){ if(keyCode == event.KEYCODE_DPAD_UP){ this.y-=2; }else if(keyCode == event.KEYCODE_DPAD_DOWN){ this.y+=2; }else if(keyCode == event.KEYCODE_DPAD_LEFT){ this.x-=2; }else if(keyCode == event.KEYCODE_DPAD_RIGHT){ this.x+=2; } invalidate(); return super.onKeyDown(keyCode, event); } /** * 重写按键弹起事件 */ @Override public boolean onKeyUp(int keyCode,KeyEvent event){ if(keyCode == event.KEYCODE_DPAD_UP){ this.y-=2; }else if(keyCode == event.KEYCODE_DPAD_DOWN){ this.y+=2; }else if(keyCode == event.KEYCODE_DPAD_LEFT){ this.x-=2; }else if(keyCode == event.KEYCODE_DPAD_RIGHT){ this.x+=2; } invalidate(); return super.onKeyUp(keyCode, event); } /** * 重写触屏事件函数 */ @Override public boolean onTouchEvent(MotionEvent event){ int t_x = (int) event.getX(); int t_y = (int) event.getY(); if(event.getAction() == MotionEvent.ACTION_DOWN){ this.x = t_x; this.y = t_y; }else if(event.getAction() == MotionEvent.ACTION_MOVE){ this.x = t_x; this.y = t_y; }else if(event.getAction() == MotionEvent.ACTION_UP){ this.x = t_x; this.y = t_y; } invalidate(); return super.onTouchEvent(event); } }