Demo and record storage of a casual game

Source: Internet
Author: User

MIDlet:

IMPORT javax. microedition. lcdui. display;
IMPORT javax. microedition. MIDlet. MIDlet;
IMPORT javax. microedition. MIDlet. midletstatechangeexception;

Public class main extends MIDlet {
Display display;
Ballcanvas ball;

Public main (){
Display = display. getdisplay (this );
Ball = new ballcanvas ();
}

Protected void destroyapp (Boolean arg0) throws midletstatechangeexception {

}

Protected void pauseapp (){
Ball. Stop ();
}

Protected void Startapp () throws midletstatechangeexception {
Display. setcurrent (ball );
Ball. Start ();
}
}

 

Canvas:

IMPORT java. Io. bytearrayinputstream;
IMPORT java. Io. bytearrayoutputstream;
IMPORT java. Io. datainput;
IMPORT java. Io. datainputstream;
IMPORT java. Io. dataoutputstream;
IMPORT java. Io. ioexception;

IMPORT javax. microedition. lcdui. Canvas;
IMPORT javax. microedition. lcdui. graphics;

Public class ballcanvas extends canvas implements runnable {
RMS;
Thread current;
Int key;
Private int PX, Py;
Private int width, height;
Private int r = 30;
/**
* Number of accompanied balls
*/
Private int COUNT = 0;
// ------------- Bballs ------------------
/**
* 0: Status (0 static, 1 accompanied, 2 discrete) <br>
* 1: x <br>
* 2: y <br>
* 3: initial angle <br>
* 4: injured <br>
* 5: healing time <br>
* 6: VX <br>
* 7: Vy <br>
*/
Private int [] [] bils = new int [10] [8];

//-------------------------------
//-------------------------------
Public ballcanvas (){
Setfullscreenmode (true );
RMS = new RMS ();
Width = getwidth ();
Height = getheight ();
Px = width> 1;
Py = height> 1;

For (INT I = 0; I <bils. length; I ++ ){
Bils [I] [1] = 45 + I * 15;
Bils [I] [2] = 260;
}

RMS. Print ();
}

Public byte [] encode (){
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Dataoutputstream dos = new dataoutputstream (baos );
Try {
Dos. writeint (PX );
Dos. writeint (Py );
Dos. writeint (R );
Dos. writeint (count );
For (INT I = 0; I <bils. length; I ++ ){
For (Int J = 0; j <bils [I]. length; j ++ ){
Dos. writeint (bils [I] [J]);
}
}
} Catch (exception e ){
}
Return baos. tobytearray ();
}

Public void decode (byte [] daTa ){
If (DATa = NULL ){
Return;
}
Bytearrayinputstream BAIS = new bytearrayinputstream (DATa );
Datainputstream Dis = new datainputstream (BAIS );
Try {
Px = dis. readint ();
Py = dis. readint ();
R = dis. readint ();
Count = dis. readint ();
For (INT I = 0; I <bils. length; I ++ ){
For (Int J = 0; j <bils [I]. length; j ++ ){
Bils [I] [J] = dis. readint ();
}
}
} Catch (ioexception e ){
E. printstacktrace ();
}
}

Public void start (){
Current = new thread (this );
Current. Start ();
}

Public void stop (){
Current = NULL;
}

Protected void paint (Graphics g ){
Clear (g );
G. setcolor (0xffff00 );
G. fillroundrect (PX-10, Py-10, 20, 20, 10, 10 );
Drawbils (g );
}

Private void drawbils (Graphics g ){
For (INT I = 0; I <bils. length; I ++ ){
G. setcolor (0 xffffff );
G. fillarc (bils [I] [1]-5, bils [I] [2]-5, 10, 10, 0,360 );
}
}

Private void clear (Graphics g ){
Int old = G. getcolor ();
G. setcolor (0x00ff00 );
G. fillrect (0, 0, width, height );
G. setcolor (old );
}

Public void run (){
While (current! = NULL ){
Input ();
Logic ();
Repaint ();
Try {
Thread. Sleep (50 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}

Private void input (){
Switch (key ){
Case-1:
Py-= 5;
Break;
Case-2:
PY + = 5;
Break;
Case-3:
PX-= 5;
Break;
Case-4:
PX + = 5;
Break;
}
}

Private void logic (){
For (INT I = 0; I <bils. length; I ++ ){
If (bils [I] [0] = 1 ){
Continue;
}
If (this. collise (PX-10, Py-10, 20, 20, bils [I] [1]-5,
Bils [I] [2]-5, 10, 10 )){
System. Out. println ("peng ");
Bils [I] [0] = 1;
Count ++;
Int angle = 360/count;
Int m = 0;
For (Int J = 0; j <bils. length; j ++ ){
If (bils [J] [0] = 1 ){
Bils [J] [3] = angle * m;
M ++;
Bils [J] [1] = (INT) (PX + R
* Math. Cos (math. toradians (bils [J] [3]);
Bils [J] [2] = (INT) (Py-R
* Math. Sin (math. toradians (bils [J] [3]);
}
}
}
}

Changeball ();

}

Private void changeball (){
For (INT I = 0; I <bils. length; I ++ ){
Switch (bils [I] [0]) {
Case 0:
Break;
Case 1:
Bils [I] [3] + = 5;
Bils [I] [1] = (INT) (PX + R
* Math. Cos (math. toradians (bils [I] [3]);
Bils [I] [2] = (INT) (Py-R
* Math. Sin (math. toradians (bils [I] [3]);
Break;
Case 2:
Bils [I] [1] + = bils [I] [6];
Bils [I] [2] + = bils [I] [7];
If (bils [I] [1]> width-10 | bils [I] [1] <0 ){
Bils [I] [6] =-bils [I] [6];
}
If (bils [I] [2]> height-10 | bils [I] [2] <0 ){
Bils [I] [7] =-bils [I] [7];
}
Break;
}
}
}

Public Boolean collise (INT X1, int Y1, int W1, int H1, int X2, int Y2,
Int W2, int H2 ){
If (x1> X2 + W2 | X2> X1 + W1 | Y1> Y2 + H2 | Y2> Y1 + H1 ){
Return false;
}
Return true;
}

Public void keyreleased (INT key ){
If (Key =-5 ){
For (Int J = 0; j <bils. length; j ++ ){
If (bils [J] [0] = 1 ){
Bils [J] [0] = 2;
Bils [J] [6] = (INT) (5 * Math. Cos (math
. Toradians (bils [J] [3]);
Bils [J] [7] = (INT) (-5 * Math. Sin (math
. Toradians (bils [J] [3]);
}
}
Count = 0;
}

If (Key = key_num1 ){
System. Out. println ("save ");
RMS. Save (this. encode ());
}
If (Key = key_num3 ){
System. Out. println ("LOAD ");
Byte [] d = RMS. Load ();
This. Decode (d );
}
This. Key = 0;

}

Public void keypressed (INT key ){
This. Key = key;
}
}

RMS:

IMPORT javax. microedition. RMS. recordstore;
IMPORT javax. microedition. RMS. recordstoreexception;
IMPORT javax. microedition. RMS. recordstorenotopenexception;

Public class RMS {
Private recordstore RS;
Private string name = "g3001 ";

Public void open (){
Try {
Rs = recordstore. openrecordstore (name, true );
} Catch (recordstoreexception e ){
E. printstacktrace ();
}
}

Public void print (){
Open ();
Try {
System. Out. println ("Current RMS size:" + Rs. getsize ());
System. Out. println ("Current RMS capacity expansion:" + Rs. getsizeavailable ());
System. Out. println ("last modification time:" + Rs. getlastmodified ());
System. Out. println ("version:" + Rs. getversion ());
} Catch (recordstorenotopenexception e ){
E. printstacktrace ();
}
Close ();
}

Public void save (byte [] daTa ){
Open ();
Try {
If (Rs. getnumrecords () = 0 ){
Rs. addrecord (DATa, 0, daTa. Length );
} Else {
Rs. setrecord (1, daTa, 0, daTa. Length );
}
} Catch (recordstoreexception e ){
E. printstacktrace ();
}
Close ();
}

Public byte [] load (){
Byte [] daTa = NULL;
Open ();
Try {
DaTa = Rs. getrecord (1 );
} Catch (recordstoreexception e ){
E. printstacktrace ();
}
Close ();
Return daTa;
}

Public void close (){
If (RS! = NULL ){
Try {
Rs. closerecordstore ();
} Catch (recordstoreexception e ){
E. printstacktrace ();
}
}
}
}

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.