通過處理按鍵模擬加速度,剎車等功能
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) ;
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=745631
相關文章
- 12.2 實現鍵盤模擬按鍵
- SSK:超級鍵盤模擬器,呼叫底層,可模擬所有按鍵
- 通過session模擬登陸Session
- 小車側方位停車過程的動態模擬matlab模擬Matlab
- 鍵盤亂鍵怎麼處理 電腦鍵盤按鍵錯亂
- Windows10 VS2017 C++模擬點選按鍵WindowsC++
- 錯誤處理:如何通過 error、deferred、panic 等處理錯誤?Error
- Maxwell 磁場模擬場計算後處理
- MATLAB數字訊號處理(2)LFM脈衝雷達回波處理模擬Matlab
- 前端坑多:使用js模擬按鍵輸入的踩坑記錄前端JS
- 山地車倒置後剎車手感不飽滿
- 模擬一則ORA-600 [4194][][]故障並處理
- 父子元件通訊——模擬12306購票新增乘車人元件
- 史上最全的汽車按鍵功能圖解說明 車內按鍵全英文看不懂?圖解
- 解決虛擬按鍵遮擋popupWindow
- 通過模擬vuex的核心原始碼快速掌握其原理Vue原始碼
- 傑裡之不執行對箱按鍵同步訊息處理方法【篇】
- ovftool匯出虛擬機器報錯處理過程!虛擬機
- 【CONNECT】ORA-00020錯誤模擬及處理方法實驗
- 通過外來鍵找主鍵
- 影像處理演算法的模擬平臺之VGA時序演算法
- HP MicroServer Gen8通過iLO按F10鍵能進入 Intelligent ProvisioningROSServerIntel
- Android處理按鈕重複點選Android
- Tickeys for Mac鍵盤音效模擬工具Mac
- Go 通過 Map/Filter/ForEach 等流式 API 高效處理資料GoFilterAPI
- 生活篇:基於TRIZ原理設計自行車剎車裝置
- 通過經緯度計算距離實現附近、附近的人等功能
- 移動端h5模擬長按事件H5事件
- video同層播放層級過高遮擋模擬暫停按鈕的問題IDE
- 復古車模擬,休閒打造
- 通過抓包實現Python模擬登陸各網站原理分析Python網站
- 嵌入式linux:通過qemu模擬mini2440開發環境Linux開發環境
- Kettle通過Http post請求webservice介面以及結果解析處理HTTPWeb
- python通過TimedRotatingFileHandler按時間切割日誌Python
- Mac鍵盤音效模擬工具——Tickeys for MacMac
- Python-模擬滑鼠鍵盤動作Python
- 高通平臺 按鍵 進入 fastboot/recovery/edl 模式ASTboot模式
- DroidPlugin手札——home鍵強殺處理Plugin
- 按鍵原理