一个休闲类游戏的demo与记录存储

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 rms;
 Thread current;
 int key;
 private int px, py;
 private int width, height;
 private int r = 30;
 /**
  * 伴随小球数量
  */
 private int count = 0;
 // -------------balls------------------
 /**
  * 0:状态(0静止,1伴随,2离散) <br>
  * 1:x <br>
  * 2:y <br>
  * 3:初始角度 <br>
  * 4:是否受伤 <br>
  * 5:疗伤时间<br>
  * 6:vx<br>
  * 7:vy<br>
  */
 private int[][] balls = 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 < balls.length; i++) {
   balls[i][1] = 45 + i * 15;
   balls[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 < balls.length; i++) {
    for (int j = 0; j < balls[i].length; j++) {
     dos.writeInt(balls[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 < balls.length; i++) {
    for (int j = 0; j < balls[i].length; j++) {
     balls[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);
  drawBalls(g);
 }

 private void drawBalls(Graphics g) {
  for (int i = 0; i < balls.length; i++) {
   g.setColor(0xffffff);
   g.fillArc(balls[i][1] - 5, balls[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 < balls.length; i++) {
   if (balls[i][0] == 1) {
    continue;
   }
   if (this.collise(px - 10, py - 10, 20, 20, balls[i][1] - 5,
     balls[i][2] - 5, 10, 10)) {
    System.out.println("peng");
    balls[i][0] = 1;
    count++;
    int angle = 360 / count;
    int m = 0;
    for (int j = 0; j < balls.length; j++) {
     if (balls[j][0] == 1) {
      balls[j][3] = angle * m;
      m++;
      balls[j][1] = (int) (px + r
        * Math.cos(Math.toRadians(balls[j][3])));
      balls[j][2] = (int) (py - r
        * Math.sin(Math.toRadians(balls[j][3])));
     }
    }
   }
  }

  changeBall();

 }

 private void changeBall() {
  for (int i = 0; i < balls.length; i++) {
   switch (balls[i][0]) {
   case 0:
    break;
   case 1:
    balls[i][3] += 5;
    balls[i][1] = (int) (px + r
      * Math.cos(Math.toRadians(balls[i][3])));
    balls[i][2] = (int) (py - r
      * Math.sin(Math.toRadians(balls[i][3])));
    break;
   case 2:
    balls[i][1] += balls[i][6];
    balls[i][2] += balls[i][7];
    if (balls[i][1] > width - 10 || balls[i][1] < 0) {
     balls[i][6] = -balls[i][6];
    }
    if (balls[i][2] > height - 10 || balls[i][2] < 0) {
     balls[i][7] = -balls[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 < balls.length; j++) {
    if (balls[j][0] == 1) {
     balls[j][0] = 2;
     balls[j][6] = (int) (5 * Math.cos(Math
       .toRadians(balls[j][3])));
     balls[j][7] = (int) (-5 * Math.sin(Math
       .toRadians(balls[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("当前RMS的大小:" + rs.getSize());
   System.out.println("当前RMS的扩充容量:" + rs.getSizeAvailable());
   System.out.println("最后修改时间:" + 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();
   }
  }
 }
}

posted @ 2011-08-26 18:48  tokeep  阅读(208)  评论(0编辑  收藏  举报