North education learning Example 1 Free Rolling Ball

Source: Internet
Author: User

Today, I have no intention of seeing the works I made when I used to study the j2_game in beijiao. They are all basic entry-level things and they were all running on the simulator, I have never tested it with a real machine. I think it's a bit hard. After reading the code, I thought some of the ideas I had when implementing them were somewhat clever. So I planned to spend some time sorting out the previous works one by one.

Since I had only been studying for half a year, and most of them were just exercises assigned by the instructors after class, I wrote these simple and rough articles, so don't expect too much. Of course, I will try to reoptimize and improve it, and give detailed comments. I hope that my friends who are interested in and haven't gotten started can get a better result after reading it.

This was written when I first learned the canvas interface. It mainly draws circles, sets colors, and controls the changes of object coordinates.

 

Finally:

 

 

 

Paste the code below

 

Gamemidlet class:

Package rollball; </P> <p> Import javax. microedition. MIDlet. MIDlet; <br/> Import javax. microedition. lcdui. *; <br/>/** <br/> * @ author kf156 (Asian Day) <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. policydestroyed (); <br/> instance = NULL; <br/>}< br/>

 

Gamecanvas class

 Package rollball; </P> <p> Import javax. microedition. lcdui. canvas; <br/> Import javax. microedition. lcdui. graphics; </P> <p>/** <br/> * freely scrolling ball <br/> * @ author kf156 (Asian Day) <br/> */<br/> public class gamecanvas extends canvas implements runnable {<br/> Public thread; </P> <p> Public Boolean running = true; </P> <p> Public int width, height, ballx = 0, Bally = 0, and ballangle = 0; </P> <p> private final int diameter = 40; // ball diameter </P> <p> Public int [] Key = new int [4]; </P> <p> private final int distance = 5; // The distance from each movement </P> <p> private final int angle = 12; // The angle of each rotation </P> <p> Public final static int key_up =-1; // up </P> <p> Public final static int key_down =-2; // down </P> <p> Public final static int key_left =-3; // left </P> <p> Public final static int key_right =-4; // right </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 () {// judge <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> protected void paint (Graphics g) {</P> <p> G. setcolor (0x000000); <br/> G. fillrect (0, 0, width, height); </P> <p> // positive ball <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/>}</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/>}</P> <p> // when the key is pressed <br/> protected void keypressed (INT keycode) {<br/> key [2] = Key [3] = keycode; <br/>}</P> <p> // when the key is released <br/> Public void keyreleased (INT keycode) {<br/> key [3] = 0; <br/>}< br/>

 

 

The project has been uploaded, feel that the web page to see the inconvenient friends can also directly download: http://download.csdn.net/source/1569954

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.