北教學習執行個體一 自由滾動的小球

來源:互聯網
上載者:User

      今天無意看到以前在“北教”學習J2ME遊戲時做的作品,都是些很基礎的入門的東西,而且當初全是在模擬器上跑,都沒拿真機測過,想想也蠻汗的。看了下代碼,覺得其中好些當初在實現時的想法卻也有些巧妙的,於是打算花點時間,把以前的這些作品一一整理出來。

      由於當初學習時間只有半年,而且大多都只是課後老師布置的練習,故所寫的這些比較簡單且粗糙,所以大夥別抱太大期望。當然,我會儘可能重新最佳化完善,並給出詳細注釋。希望對J2ME有興趣,卻還未入門的朋友看了後能有收穫。

      這個是當初剛學Canvas介面時寫的,主要是圓的繪製、設定顏色、控制物體座標的改變。

 

      最終:

     

      

 

 

 

      下邊貼出代碼

 

GameMIDlet類:

package rollBall;</p><p>import javax.microedition.midlet.MIDlet;<br />import javax.microedition.lcdui.*;<br />/**<br /> *<br /> * @author kf156(亞日)<br /> *<br /> */<br />public class GameMIDlet extends MIDlet {<br />private static GameMIDlet instance;</p><p>private GameCanvas game;</p><p>public GameMIDlet() {<br />instance = this;<br />}</p><p>protected void destroyApp(boolean arg0) {<br />}</p><p>protected void pauseApp() {<br />}</p><p>protected void startApp() {<br />if (game == null) {<br />game = new GameCanvas();<br />}<br />Display.getDisplay(this).setCurrent(game);<br />}</p><p>public static void quitApp() {<br />instance.destroyApp(true);<br />instance.notifyDestroyed();<br />instance = null;<br />}<br />}<br />

 

GameCanvas類

 package rollBall;</p><p>import javax.microedition.lcdui.Canvas;<br />import javax.microedition.lcdui.Graphics;</p><p>/**<br /> * 自由滾動的小球<br /> *<br /> * @author kf156(亞日)<br /> *<br /> */<br />public class GameCanvas extends Canvas implements Runnable {<br />public Thread thread;</p><p>public boolean running = true;</p><p>public int width, height, ballX = 0, ballY = 0, ballAngle = 0;</p><p>private final int DIAMETER = 40;// 小球直徑</p><p>public int[] key = new int[4];</p><p>private final int DISTANCE = 5;// 每次移動的距離</p><p>private final int ANGLE = 12;// 每次轉動的角度</p><p>public final static int KEY_UP = -1;// 上</p><p>public final static int KEY_DOWN = -2;// 下</p><p>public final static int KEY_LEFT = -3;// 左</p><p>public final static int KEY_RIGHT = -4;// 右</p><p>public GameCanvas() {<br />setFullScreenMode(true);<br />startThread();<br />init();<br />}</p><p>public void free() {<br />}</p><p>public void init() {<br />width = getWidth();<br />height = getHeight();<br />}</p><p>public void input() { // 判斷<br />key[0] = key[2];<br />key[1] = key[3];<br />key[2] = 0;<br />}</p><p>public void update() {</p><p>switch (key[1]) {</p><p>case KEY_UP:<br />ballY -= DISTANCE;<br />ballAngle += ANGLE;<br />if (ballY < -DIAMETER)<br />ballY = height;<br />break;<br />case KEY_DOWN:<br />ballY += DISTANCE;<br />ballAngle -= ANGLE;<br />if (ballY > height)<br />ballY = -DIAMETER;<br />break;<br />case KEY_LEFT:<br />ballX -= DISTANCE;<br />ballAngle += ANGLE;<br />if (ballX < -DIAMETER)<br />ballX = width;<br />break;<br />case KEY_RIGHT:<br />ballX += DISTANCE;<br />ballAngle -= ANGLE;<br />if (ballX > width)<br />ballX = -DIAMETER;<br />break;</p><p>}</p><p>}</p><p>protected void paint(Graphics g) {</p><p>g.setColor(0x000000);<br />g.fillRect(0, 0, width, height);</p><p>// 正轉的小球<br />g.setColor(0x00ff00);<br />g.fillArc(ballX, ballY, DIAMETER, DIAMETER, 0, 360);</p><p>g.setColor(0x0000ff);<br />g.fillArc(ballX, ballY, DIAMETER, DIAMETER, ballAngle + 60, 60);<br />g.fillArc(ballX, ballY, DIAMETER, DIAMETER, ballAngle + 180, 60);<br />g.fillArc(ballX, ballY, DIAMETER, DIAMETER, ballAngle + 300, 60);</p><p>}</p><p>public void startThread() {<br />if (thread == null) {<br />thread = new Thread(this);<br />thread.start();<br />}<br />}</p><p>public void run() {<br />int debug = 0;<br />try {<br />while (running) {<br />long start = System.currentTimeMillis();<br />input();<br />debug = 1;<br />update();<br />debug = 2;<br />repaint();<br />serviceRepaints();<br />Thread.yield();<br />long end = System.currentTimeMillis() - start;<br />if (end < 50)<br />Thread.sleep(50 - end);<br />}<br />GameMIDlet.quitApp();<br />} catch (Exception e) {<br />System.err.println("run error:" + e + "debug:" + debug);<br />}<br />}</p><p>// 當按鍵按下時<br />protected void keyPressed(int keyCode) {<br />key[2] = key[3] = keyCode;<br />}</p><p>// 當按鍵釋放時<br />public void keyReleased(int keyCode) {<br />key[3] = 0;<br />}<br />}<br />

 

 

  

 工程已經上傳,覺得網頁上看不方便的朋友也可直接下載:http://download.csdn.net/source/1569954

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.