public ? class ? Core ? extends ? Canvas ?
? implements ? Runnable ? , ? RecordFilter ? { ?
? ?
? ?
? ////////////////////////////////////////////////////////////////// ?
? //Game ? core ?
? ////////////////////////////////////////////////////////////////// ?
? ?
? ?
? //#ifndef ? Core.GameCanvas ?
? ?
? //define ? constant ? to ? handle ? game ? key ? states ?
? public ? static ? final ? int ? UP_PRESSED ? = ? 1 ? << ? UP; ?
? public ? static ? final ? int ? DOWN_PRESSED ? = ? 1 ? << ? DOWN; ?
? public ? static ? final ? int ? LEFT_PRESSED ? = ? 1 ? << ? LEFT; ?
? public ? static ? final ? int ? RIGHT_PRESSED ? = ? 1 ? << ? RIGHT; ?
? public ? static ? final ? int ? FIRE_PRESSED ? = ? 1 ? << ? FIRE; ?
? public ? static ? final ? int ? GAME_A_PRESSED ? = ? 1 ? << ? GAME_A; ?
? public ? static ? final ? int ? GAME_B_PRESSED ? = ? 1 ? << ? GAME_B; ?
? public ? static ? final ? int ? GAME_C_PRESSED ? = ? 1 ? << ? GAME_C; ?
? public ? static ? final ? int ? GAME_D_PRESSED ? = ? 1 ? << ? GAME_D; ?
? //double ? buffer ?
? private ? Image ? __bufferedImage; ?
? //clip ?
? private ? boolean ? __setClip; ?
? private ? int ? __clipX, ? __clipY, ? __clipWidth, ? __clipHeight; ?
? ?
? public ? void ? paint(Graphics ? g) ?
? { ?
? if ? (this.__setClip) ? { ?
? g.clipRect( ? this.__clipX, ? this.__clipY, ? this.__clipWidth, ? this.__clipHeight); ?
? this.__setClip ? = ? false; ?
? } ?
? g.drawImage(this.__bufferedImage, ? 0, ? 0, ? Graphics.TOP ? | ? Graphics.LEFT ? ); ?
? } ?
? ?
? public ? void ? flushGraphics() ?
? { ?
? repaint(); ?
? serviceRepaints(); ?
? } ?
? ?
? public ? void ? flushGraphics(int ? x, ? int ? y, ? int ? width, ? int ? height) ?
? { ?
? this.__setClip ? = ? true; ?
? this.__clipX ? = ? x; ?
? this.__clipY ? = ? y; ?
? this.__clipWidth ? = ? width; ?
? this.__clipHeight ? = ? height; ?
? repaint(); ?
? serviceRepaints(); ?
? } ?
? ?
? //#endif ?
? ?
? ?
? //define ? constant ? to ? handle ? soft ? key ? states ?
? public ? static ? final ? int ? SOFT_FIRST_PRESSED ? = ? GAME_A_PRESSED; ?
? public ? static ? final ? int ? SOFT_LAST_PRESSED ? = ? GAME_B_PRESSED; ?
? //MIDlet ? object ?
? ? ? ? ? private ? MIDlet ? __midlet; ?
? //key ? states ? and ? soft ? key ? states ?
? private ? int ? __keyStates, ? __currentKeyStates, ? __lastKeyStates, ? __softKeyStates, ? __currentSoftKeyStates, ? __lastSoftKeyStates; ?
? ? ? ? ? //used ? for ? sleep ?
? ? ? ? ? private ? long ? __lastSystemTime, ? __elapsedTime; ?
? ?
? //canvas ? Graphics ? object ?
? private ? Graphics ? g; ?
? //view ? window ?
? private ? int ? viewX, ? viewY, ? viewWidth, ? viewHeight; ?
? ? ? ? ? //running ? and ? pause ? flags ?
? ? ? ? ? public ? boolean ? running ? = ? true; ?
? ? ? ? ? public ? boolean ? pause; ?
? ? ? ? ? //clock ?
? private ? boolean ? clockRunning ? = ? true; ?
? ? ? ? ? public ? long ? virtualTime; ?
? ? ? ? ? //frame ? delay ?
? ? ? ? ? private ? long ? frameDelay ? = ? 120; ?
? //auto ? flushGraphics ?
? ?
? public ? Core(MIDlet ? m) ? { ?
? //#ifndef ? Core.GameCanvas ?
? super(); ?
? //#else ?
? //# ? super(false); ?
? //# ? setFullScreenMode(true); ?
? //#endif ?
? ?
? ?
? //#ifdef ? Core.GameCanvas ?
? this.viewWidth ? = ? getWidth(); ?
? this.viewHeight ? = ? getHeight(); ?
? //#else ?
? //#if ? polish.FullCanvasSize:defined ?
? //#= ? this.viewWidth ? = ? ${polish.FullCanvasWidth}; ?
? //#= ? this.viewHeight ? = ? ${polish.FullCanvasHeight}; ?
? //#else ?
? //# ? ? this.viewWidth ? = ? getWidth(); ?
? //# ? ? this.viewHeight ? = ? getHeight(); ?
? //#endif ?
? //#endif ?
? ?
? ?
? //#ifndef ? Core.GameCanvas ?
? this.__bufferedImage ? = ? Image.createImage(this.viewWidth, ? this.viewHeight ? ); ?
? this.g ? = ? this.__bufferedImage.getGraphics(); ?
? //#else ?
? //# ? this.g ? = ? getGraphics(); ?
? //#endif ?
? ?
? ?
? //midlet ?
? ? ? ? ? this.__midlet ? = ? m; ?
? } ?
? ?
? protected ? void ? keyPressed(int ? keyCode) ? { ?
? ?
? //#ifndef ? Core.GameCanvas ?
? this.__keyStates ? |= ? 1 ? << ? getGameAction(keyCode); ?
? //#endif ?
? ?
? ?
? ?
? switch ? (keyCode) ? { ?
? //#ifdef ? polish.key.LeftSoftKey ?
? //# ? case ? ${polish.key.LeftSoftKey}: ?
? //#else ?
? case ? -6: ?
? //#endif ?
? this.__softKeyStates ? |= ? SOFT_FIRST_PRESSED; ?
? break; ?
? //#ifdef ? polish.key.RightSoftKey ?
? //# ? case ? ${polish.key.RightSoftKey}: ?
? //#else ?
? case ? -7: ?
? //#endif ?
? this.__softKeyStates ? |= ? SOFT_LAST_PRESSED; ?
? break; ?
? } ?
? ?
? } ?
? ?
? protected ? void ? keyReleased(int ? keyCode) ? { ?
? ?
? //#ifndef ? Core.GameCanvas ?
? this.__keyStates ? &= ? ~(1 ? << ? getGameAction(keyCode)); ?
? //#endif ?
? ?
? ?
? ?
? switch ? (keyCode) ? { ?
? //#ifdef ? polish.key.LeftSoftKey ?
? //# ? case ? ${polish.key.LeftSoftKey}: ?
? //#else ?
? case ? -6: ?
? //#endif ?
? this.__softKeyStates ? &= ? ~SOFT_FIRST_PRESSED; ?
? break; ?
? //#ifdef ? polish.key.RightSoftKey ?
? //# ? case ? ${polish.key.RightSoftKey}: ?
? //#else ?
? case ? -7: ?
? //#endif ?
? this.__softKeyStates ? &= ? ~SOFT_LAST_PRESSED; ?
? break; ?
? } ?
? ?
? } ?
? ?
? /** ?
? ? * ? detect ? user ? input ?
? ? * ? ?
? ? * ? @param ? keyPressedConstant ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? XXX_PRESSED ? constant ? like ? FIRE_PRESSED ?
? ? * ? @param ? detectsStroke ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? set ? true ? to ? detect ? stroke ? action,false ? to ? detect ? key ? pressed ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? state ?
? ? * ? @param ? soft ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? set ? true ? to ? detect ? soft ? key,false ? to ? detect ? game ? key. ?
? ? * ? @param ? update ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? set ? true ? to ? update ? key(soft ? key) ? state ?
? ? * ? @return ?
? ? */ ?
? private ? final ? boolean ? detectInput(int ? keyPressedConstant, ? boolean ? detectsStroke,boolean ? soft,boolean ? update){ ?
? ? ? ? ? if(soft){ ? ? ? ?
? ? ? ? ? if(update){ ?
? ? ? ? ? this.__lastSoftKeyStates ? = ? this.__currentSoftKeyStates; ?
? ? ? ? ? this.__currentSoftKeyStates ? = ? this.__softKeyStates; ?
? ? ? ? ? } ?
? ? ? ? ? ?
? ? ? ? ? if(detectsStroke){ ?
? ? ? ? ? ? ? ? ? ? ? ? ? return ? ((this.__lastSoftKeyStates ? & ? keyPressedConstant) ? != ? 0) ? && ? ((this.__softKeyStates ? & ? keyPressedConstant) ? == ? 0); ? ?
? ? ? ? ? ? ? ? ? }else{ ?
? ? ? ? ? ? ? ? ? return ? ((this.__softKeyStates ? & ? keyPressedConstant) ? != ? 0); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? }else{ ? ? ? ? ?
? ? ? ? ? if(update){ ?
? ? ? ? ? //#ifdef ? Core.GameCanvas ?
? //# ? this.__keyStates ? = ? getKeyStates(); ?
? ? ? ? ? //#endif ?
? ? ? ? ? this.__lastKeyStates ? = ? this.__currentKeyStates; ?
? ? ? ? ? this.__currentKeyStates ? = ? this.__keyStates; ?
? ? ? ? ? } ?
? ? ? ? ? ?
? ? ? ? ? if(detectsStroke){ ?
? ? ? ? ? ? ? ? ? ? ? ? ? return ? ((this.__lastKeyStates ? & ? keyPressedConstant) ? != ? 0) ? && ? ((this.__keyStates ? & ? keyPressedConstant) ? == ? 0); ? ?
? ? ? ? ? ? ? ? ? }else{ ?
? ? ? ? ? ? ? ? ? return ? ((this.__keyStates ? & ? keyPressedConstant) ? != ? 0); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? } ?
? } ?
? ?
? //implements ? interface ? Runnable ?
? public ? void ? run(){ ?
? try{ ?
? initialize(); ?
? ?
? while ? (running) ? { ?
? this.__lastSystemTime ? = ? System.currentTimeMillis(); ?
? ?
? if(!pause){ ?
? //#ifndef ? Core.GameCanvas ?
? this.g ? = ? this.__bufferedImage.getGraphics(); ?
? //#else ?
? //# ? this.g ? = ? getGraphics(); ?
? //#endif ?
? executeFrameTask(); ?
? if(clockRunning){ ?
? this.virtualTime ? += ? this.frameDelay; ?
? } ?
? } ?
? ?
? this.__elapsedTime ? = ? System.currentTimeMillis() ? - ? this.__lastSystemTime; ?
? if ? (this.__elapsedTime>0 ? && ? this.__elapsedTime ? < ? this.frameDelay) ? { ?
? try{ ?
? Thread.sleep(this.frameDelay ? - ? this.__elapsedTime); ?
? }catch(InterruptedException ? ie){} ?
? } ?
? ?
? //#mdebug ? benchmark ?
? //# ? System.out.println("elapsed ? time: ? " ? + ? String.valueOf(this.__elapsedTime) ? + ? ?
? //# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ? ? (" ? + ? String.valueOf(this.__elapsedTime*100/this.frameDelay) ? + ? "%)"); ?
? //#enddebug ?
? } ?
? ?
? this.__midlet.notifyDestroyed(); ?
? ? ? ? ? ? ? ? ? //#mdebug ? info ?
? ? ? ? ? //# ? System.out.println("game ? stop ? normally."); ?
? //#enddebug ?
? ? ? ? ? }catch(Exception ? e){ ?
? ? ? ? ? ? ? ? ? //#mdebug ? fatal ?
? ? ? ? ? //# ? System.out.println("a ? fatal ? error ? occured."); ?
? ? ? ? ? ? ? ? ? //# ? e.printStackTrace(); ?
? //#enddebug ?
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? showSystemHint(Locale.get("Game.SystemError")); ?
? ? ? ? ? ?
? ? ? ? ? this.__midlet.notifyDestroyed(); ?
? ? ? ? ? } ?
? } ?
? ? ? ? ? ? ? ? ? //略…… ?
? }???
??????? 上面的代碼在MIDP1.0接口的基礎上模擬了MIDP2.0 ? GameCanvas的功能,并對用戶輸入作了增強。 ?
? ? ? ? paint(Graphics ? g)實現了父類的抽象方法。 ?
? ? ? ? flushGraphics()和flushGraphics(int ? x, ? int ? y, ? int ? width, ? int ? height)是對GameCanvas相應方法的模擬,方便調用。 ?
? ? ? ? keyPressed(int ? keyCode)和keyReleased(int ? keyCode)用來捕獲用戶輸入,并記錄按鍵的狀態。 ?
? ? ? ? detectInput(int ? keyPressedConstant, ? boolean ? detectsStroke,boolean ? soft,boolean ? update)用于根據案件狀態來判斷,用戶(玩家)是否將某個鍵(包括左右系統鍵)按下一次,或按下沒有松開。 ?
? ? ? ? run()實現了Runnable接口的方法,其中,游戲初始化在initialize()中進行,游戲循環在executeFrameTask()中實現。每一幀如果不到預定的幀延遲時間就讓當前線程睡眠一會。 ?
? ?
? ? ? ? 這只是一小部分,為了閱讀方便有些改動。另外,在我的游戲內核里還有 ?
? ? ? ? Temporary ? Memory部分 ? ? ? ? ? ? 用于方便的把數據按照數據流DataInputStream和DataOutputStream來操作。 ?
? ? ? ? General部分 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 定義了8個方向,高效產生隨機數,對矩形進行碰撞判斷,byte[]和int的相互轉換。 ?
? ? ? ? Device ? Feature部分 ? ? ? ? ? ? ? ? 包括設備相關特性的操作:聲音、振動和燈光 ?
? ? ? ? UI部分 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 包括在屏幕上書寫特效文字,在一個區域內分頁顯示大段文字(比如人物對話),系統信息提示和進度條。 ?
? ? ? ? Map部分 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 對游戲中地圖的解析和顯示 ?
? ? ? ? Game ? Menu部分 ? ? ? ? ? ? ? ? ? ? ? ? ? 游戲菜單,繪制菜單和獲取用戶對菜單項的選擇。 ?
? ? ? ? Resource ? and ? storage部分 ? ? 用于獲取打包資源(為了使壓縮比更大)和按文件名方式管理RMS內容,還有從網上下載資源。 ?
? ? ? ? Path部分 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 產生完美的圓弧和直線路徑,用于子彈敵人飛行等 ?
? ? ? ? 上面各部分為了使字節碼盡可能少,我沒有做類封裝,而是寫到了一起。另外,如果有哪些部分沒有被調用到,那么在作繞碼(混淆)時,會被自動去掉,所以不必擔心。 ?
? ?
? ? ? ? ? 我還寫了對MIDP2.0 ? Sprite類的模擬類,以便使用。 ?
? ?
? ? ? ? ? 內核部分之外,就是具體游戲邏輯的實現了。其實,有了上面工具的幫助,游戲邏輯的實現是就容易多了。 ?
? ?
? ? ? ? ? 除了j2me的程序,在開發中還需要一些工具,如:資源打包工具,地圖編輯器和圖像優化工具。 ?
? ? ? ? ? 資源打包工具——把多個資源文件(如png圖片等)打包為一個文件,這樣在jar包中會有更大的壓縮比。 ?
? ? ? ? ? 地圖編輯器——編輯游戲地圖,共類似TiledLayer使用。 ?
? ? ? ? ? 圖像優化工具——將png圖片進一步壓縮,節約空間。 ?
? ?
? ? ? ? ? 以上內容我正在使用。 ?
? ?
? ?
? 二、商業運作 ?
? ? ? ? ? 這方面問題如果沒有深入進去,是比較難了解的。在國內,j2me應用是通過sp(Service ? Provider,開發者是cp ? content ? provider之一)來進行發布,通過移動審批后供用戶下載使用。通常的商業模式為:預付款+按比例分成。具體數字和比例看CP的名氣、東西的品質和談判的技巧等。 ?
? ? ? ? ? 如果東西好,還可以銷售到國外。除了通過運營商銷售以外,還可以放在獨立的銷售網站上銷售。好東西,價格都很高的。像gameloft的單機游戲幾十港元一個。通過運營商的,通常可以買到4-5歐元一個下載。 ?
? ? ? ? ? 所以,新興的手機游戲市場,是個大餡餅,有很多機會。尤其是對于我們這種沒有資金的個人或小型團隊來說,是淘金千載難逢的機遇。 ?
? ? ? ? ? 更詳細的以后再談。 ?
? 三、demo ?
? ? ? ? ? 這個demo是我目前尚未發布的作品,MIDP2 ? is ? required.具體我不多談了,CSDN不能上傳,有興趣可以給我發郵件,我用附件發送。 ?
? ?
? 四、關于我和我尋求的幫助 ?
? ? ? ? ? 先說我個人。我2000年考入國防科技大學,2年后不能忍受壓抑的環境要求退學未受批準,于是不告而別被開除。后來研究技術,沒有找到合適的市場機會。幾個月前和像素點工作室的朋友在四川合作開發手機游戲,現在擁有1個完整產品和完善內核、周邊工具。 ?
? ?
? ? ? ? ? 也許最理智的程序員也會在愛情面前陷入死循環,直到資源耗盡系統崩潰! ?
? ? ? ? ? 我尋求的幫助。去年我和北京一個女孩有個為期5個月的浪漫約定,于是我就走入移動開發領域。可是,時間到了,我的東西還沒出來,我失約了。約定又超過了2個月,我不想絮叨情感上的糾纏不清,可的確發生了很多事情。今天我和她聊天,我不能再等了。我不能等到我的游戲談判完賣出去以后再去找她,因為她要毀了自己,絕無玩笑。 ?
? ?
? ?
? ? ? ? ? 對具體的技術和商業細節感興趣,可以和我面談。如果你愿意善意的幫助一個同行,交一個真誠的朋友或者共同開發手機游戲市場,請盡快聯系我。??
http://topic.csdn.net/t/20050308/05/3832358.html
posted on 2007-02-03 19:15
SIMONE 閱讀(366)
評論(0) 編輯 收藏 所屬分類:
JAVA