关闭

通过处理按键模拟加速度,刹车等功能

标签: timerimportexceptionupthreadclass
1614人阅读 评论(0) 收藏 举报

import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;

public class gKey {
public int key_UP;
public int key_DOWN;
public int key_LEFT;
public int key_RIGHT;
public int key_A;
public int key_B;
public int key_C;
public int key_D;
public int key_STAR;
public int key_POUND;
public int key_TIMER;
public int movespeed ;
public int leftvalue ;
public int upvalue ;
public int rightvalue ;
public int downvalue ;

public gKey ( int keytime , int mspeed )
{
  key_TIMER = keytime ;
  refreshKey() ;  
  movespeed = mspeed ;
}

  public void refreshKey ()
{
  key_UP = 0 ;
  key_DOWN = 0 ;
  key_LEFT = 0 ;
  key_RIGHT = 0 ;
  key_A = 0 ;
  key_B = 0 ;
  key_C = 0 ;
  key_D = 0 ;
  key_STAR = 0 ;
  key_POUND = 0 ;
}

public void flushKey ()
{
  if ( key_UP > 0 )
   if ( key_UP < key_TIMER ) key_UP += upvalue ;
  if ( key_DOWN > 0 )
   if ( key_DOWN < key_TIMER ) key_DOWN += downvalue ;
  if ( key_LEFT > 0 )
   if ( key_LEFT < key_TIMER ) key_LEFT += leftvalue  ;
  if ( key_RIGHT > 0 )
   if ( key_RIGHT < key_TIMER ) key_RIGHT += rightvalue ;
}

public void keyPressed(int keyCode,Canvas canvas)
{
     switch (canvas.getGameAction(keyCode)) {
     case Canvas.UP:
         key_UP = (key_UP < key_TIMER)?key_UP+movespeed:key_TIMER ;
         upvalue = movespeed ;
         break;
     case Canvas.DOWN:
      key_DOWN = (key_DOWN < key_TIMER )?key_DOWN+movespeed:key_TIMER ;
         upvalue = movespeed ;
         break;
     case Canvas.LEFT:
      key_LEFT = (key_LEFT < key_TIMER )?key_LEFT+movespeed:key_TIMER ;
      leftvalue = movespeed ;
         break;
     case Canvas.RIGHT:
      key_RIGHT = (key_RIGHT < key_TIMER )?key_RIGHT+movespeed:key_TIMER ;
      rightvalue = movespeed ;
         break;
     case Canvas.GAME_A:
      key_A = (key_A < key_TIMER )?key_A+1:key_TIMER ;
         break;
     case Canvas.GAME_B:
      key_B = (key_B < key_TIMER )?key_B+1:key_TIMER ;
         break;
     case Canvas.GAME_C:
      key_C = (key_C < key_TIMER )?key_C+1:key_TIMER ;
         break;
     case Canvas.GAME_D:
      key_D = (key_D < key_TIMER )?key_D+1:key_TIMER ;
         break;
     case 0:
     switch (keyCode) {
      case Canvas.KEY_NUM2:
       key_UP = (key_UP < key_TIMER )?key_UP+movespeed:key_TIMER ;
       upvalue = movespeed ;
          break;
      case Canvas.KEY_NUM8:
       key_DOWN = (key_DOWN < key_TIMER )?key_DOWN+movespeed:key_TIMER ;
          upvalue = movespeed ;
          break;
         case Canvas.KEY_NUM4:
       key_LEFT = (key_LEFT < key_TIMER )?key_LEFT+movespeed:key_TIMER ;
       leftvalue = movespeed ;
          break;
         case Canvas.KEY_NUM6:
       key_RIGHT = (key_RIGHT < key_TIMER )?key_RIGHT+movespeed:key_TIMER ;
       rightvalue = movespeed ;
          break;
      case Canvas.KEY_POUND:
       key_POUND = (key_POUND < key_TIMER )?key_POUND+1:key_TIMER ;
          break;
      case Canvas.KEY_STAR:
       key_STAR = (key_STAR < key_TIMER )?key_STAR+1:key_TIMER ;
          break;
         }
         break;
     }
}

    public void keyReleased(int keyCode,Canvas canvas) {
     switch (canvas.getGameAction(keyCode)) {
     case Canvas.UP:
      upvalue = - (key_UP / 4) ;
         break;
     case Canvas.DOWN:
      downvalue = - (key_DOWN / 4) ;
         break;
     case Canvas.LEFT:
      leftvalue = - (key_LEFT / 4) ;
         break;
     case Canvas.RIGHT:
      rightvalue = -(key_RIGHT / 4) ;
         break;
     case Canvas.GAME_A:
      key_A = 0 ;
         break;
     case Canvas.GAME_B:
      key_B = 0 ;
         break;
     case Canvas.GAME_C:
      key_C = 0 ;
         break;
     case Canvas.GAME_D:
      key_D = 0 ;
         break;
     case 0:
     switch (keyCode) {
      case Canvas.KEY_NUM2:
       upvalue = -(key_UP / 4) ;
          break;
      case Canvas.KEY_NUM8:
       downvalue = -(key_DOWN / 4) ;
          break;
         case Canvas.KEY_NUM4:
       leftvalue = -(key_LEFT / 4) ;
          break;
         case Canvas.KEY_NUM6:
       rightvalue = -(key_RIGHT / 4) ;
          break;
      case Canvas.KEY_POUND:
       key_POUND = 0 ;
          break;
      case Canvas.KEY_STAR:
       key_STAR = 0 ;
          break;
        }
        break;
    }
    }

}

import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;

public class gCanvas extends Canvas implements Runnable {

private Graphics bgGraphics;
private Image bgBuf;
private int width ;
private int height ;
private int dfwidth ;
private int dfheight ;
private int snoffx ;
private int snoffy ;
private boolean isOver;
private boolean isFirstRun ;
public gKey keyParam ;
public int thDelay ;
private Thread mThread ;


// test
private int x,y ;

public gCanvas (int w,int h,int keytimer,int thdelay)
{
  try{
   width = getWidth() ;
   height = getHeight() ;
   dfwidth = w ;
   dfheight = h ;
   isFirstRun = true ;
   snoffx = ( width - dfwidth ) / 2 ;
   snoffy = ( height - dfheight ) / 2 ;
   isOver = false ;
   bgBuf = Image.createImage ( dfwidth, dfheight ) ;
   bgGraphics = bgBuf.getGraphics() ;
   mThread = new Thread(this) ;
   x = 0 ;
   y = 0 ;
   keyParam = new gKey ( keytimer) ;
   thDelay = thdelay ;  
   // test
  }
  catch(Exception ex){
   isOver = true ;
  }
  
}


public void OverProcess ()
{
  isOver = true ;
  // add destroy code
}

public void run ()
{
  try{
   while ( !isOver )
   {
    // add execute code
    if ( keyParam.key_A > 0 ){
     keyParam.refreshKey() ;  // 键按下后只响应一次要调用这个函数
     OverProcess() ;
    }
    
    if ( y > 0 ) y -= keyParam.key_UP ;
    if ( y < width - 32 ) y += keyParam.key_DOWN ;//这里读出来的值不是顾定的,你按的越长值越大,上限是keytime,表示加速度,当你松开键时这里的值不会马上为0,但是会很快变成0,表示惯性
    if ( x > 0 ) x -= keyParam.key_LEFT ;
    if ( x < height - 32 ) x += keyParam.key_RIGHT ;
    keyParam.flushKey() ;
    
    
    repaint() ;
    mThread.sleep(thDelay);
   }
  }
  catch(Exception ex)
  {}
}

public void paint (Graphics g)
{
  // clear screen
  if ( isFirstRun == true ){
   g.setColor(0) ;
   g.fillRect ( 0 , 0 , width , height ) ;
  }
  
  
  // add draw function

  g.drawImage (bgBuf,snoffx,snoffy,g.LEFT|g.TOP) ;
}


    public void keyPressed(int keyCode) {
     keyParam.keyPressed(keyCode,this) ;
    }


    public void keyReleased(int keyCode) {
     keyParam.keyReleased(keyCode,this) ;
    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:831160次
    • 积分:9656
    • 等级:
    • 排名:第1935名
    • 原创:120篇
    • 转载:35篇
    • 译文:3篇
    • 评论:1121条
    新浪微博
    最新评论