这个第四个实例,我只是单纯的把j2me的代码换成android的,所以屏幕的大小的适应性和好不好看,我都没有考虑。
下面给出第四个实例的MainView的代码
效果图:
package com.wjh.demon_4; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.KeyEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.SurfaceHolder.Callback; public class MainView extends SurfaceView implements Callback,Runnable{ int keyCode = 0; String keyAction = ""; Thread gameThread = null; boolean isGame = true; SurfaceHolder holder = null; public char m_achWords[][] = //存储原始字符矩阵 { {'H', 'R', 'T', 'W', 'T'}, {'E', 'I', 'H', 'O', 'H'}, {'L', 'G', 'I', 'R', 'R'}, {'L', 'H', 'N', 'L', 'E'}, {'O', 'T', 'G', 'D', 'E'} }; public int m_nCurLine = 0; //记录当前行 public int m_anDis[] = { 0,1,2,3,4 }; public boolean m_bGameOK = false; //组合成功的标志 public MainView(Context context) { super(context); // TODO Auto-generated constructor stub setFocusable(true); getHolder().addCallback(this); holder = this.getHolder(); } @Override public void run() { // TODO Auto-generated method stub while(isGame) { input(); logic(); doDraw(); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //开始游戏主线程 public void start() { if(gameThread == null) { gameThread = new Thread(this); gameThread.start(); } } //停止游戏主线程 public void stop() { isGame = false; if(gameThread != null) { try { gameThread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //输入判断 public void input() { } //逻辑判断 public void logic() { if( m_bGameOK ) return; m_bGameOK = true; for( int n = 0; n < 5; n ++ ){ if( m_anDis[n] != 0 ){ m_bGameOK = false; break; } } } public void doDraw() { Canvas c = null; try { c = holder.lockCanvas(); synchronized (holder) { paint(c); } }finally{ if(c != null) { holder.unlockCanvasAndPost(c); } } } //画图 public void paint(Canvas canvas) { Paint backGround = new Paint(); backGround.setColor(Color.BLACK); //清屏 canvas.drawRect(0, 0, getWidth(), getHeight(), backGround); //设置画笔颜色 Paint cPaint = new Paint(); //显示字符 //realCol为所显示的字符在原始矩阵中的列数 int realCol = 0; //row为行数,col为列数 for( int row = 0; row < 5; row ++ ){ if( row == m_nCurLine ) //当前行的字符用黄色显示 cPaint.setColor(Color.YELLOW); else cPaint.setColor(Color.WHITE); //非当前行的字符用白色显示 for( int col = 0; col < 5; col ++ ){ //利用偏位数得到与当前位置对应的原始矩阵中的位置 realCol = m_anDis[row] + col; if( realCol > 4 ) realCol = realCol - 5; canvas.drawText(""+m_achWords[row][realCol], 30 + col * 15, 30+row * 15, cPaint); } } cPaint.setColor(Color.WHITE); if( m_bGameOK ) canvas.drawText("真棒,完全正确", 50, 120, cPaint); else canvas.drawText("左右移动字符,使纵向拼写正确", 40, 120, cPaint); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub stop(); } public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if( m_bGameOK ) return true; //得到当前按键状态 //如果按下方向键的上键,则调整当前行的位置 if( keyCode == KeyEvent.KEYCODE_DPAD_UP ) m_nCurLine --; //如果按下方向键的下键,则调整当前行的位置 if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) m_nCurLine ++; //调整当前行的值,使它在0到4之间 if( m_nCurLine < 0 ) m_nCurLine += 5; if( m_nCurLine > 4) m_nCurLine -= 5; //如果按下方向键的左键,则当前行的字符左移 if( keyCode == KeyEvent.KEYCODE_DPAD_LEFT) m_anDis[m_nCurLine] ++; //如果按下方向键的右键,则当前行的字符右移 if( keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) m_anDis[m_nCurLine] --; if( m_anDis[m_nCurLine] < 0 ) m_anDis[m_nCurLine] += 5; if( m_anDis[m_nCurLine] > 4 ) m_anDis[m_nCurLine] -= 5; return super.onKeyDown(keyCode, event); } }
apk文件(将后缀改为apk): Demon_4.zip
源代码文件: Demon_4.rar