在中兴软件技术从事手机游戏开发的实习报告(2)

时间:2021-08-31

  3.游戏的具体实现

  test.java – 主类,运行在手机平台上必须要继承middle父类,及调用相应的控制游戏运行方法。

  mycanvas.java – 游戏显示及调用类,其中定义了各种游戏中需要的变量和创建其他类的实例。

  player.java – 玩家类,其中定义了玩家的位置及运动轨迹。

  enemy.java – 敌机类,定义了敌机的类型,敌机的位置,敌机的运行轨迹。

  enemy类的实现和注释:

  import javax.microedition.lcdui.*;

  public class enemy {

  private int x, y;

  private int type;// 设置敌人的图片格式

  private boolean isblod; // 掉血的敌机,如果被击落将变为玩家的子弹升级值

  public boolean isblod() {

  return isblod;

  }

  /**

  * 设置敌人被击中后是否掉落升级玩家子弹的物品

  *

  * @param isblod

  */

  public void setblod(boolean isblod) {

  this.isblod = isblod;

  }

  image img;

  public enemy(int xx, int yy, int type) {

  this.x = xx;

  this.y = yy;

  this.type = type;

  stringbuffer s = new stringbuffer();

  int i = mycanvas.time % 3;

  string str;

  try {

  // 根据不同的类型导入不同的敌人图片

  switch (type) {

  case 0:

  s.append("/plan0");

  s.append(i);

  s.append(".png");

  str = s.tostring();

  img = image.createimage(str);// plan 0

  break;

  case 1:

  s.append("/plan0");

  s.append(i);

  s.append(".png");

  str = s.tostring();

  img = image.createimage(str);// plan 1

  break;

  case 2:

  img = image.createimage("/boss0.png");// boss 0

  break;

  case 3:

  s.append("/plan1");

  s.append(i);

  s.append(".png");

  str = s.tostring();

  img = image.createimage(str);// plan 1

  break;

  case 4:

  s.append("/plan1");

  s.append(i);

  s.append(".png");

  str = s.tostring();

  img = image.createimage(str);// plan 2

  case 5:

  img = image.createimage("/boss1.png");// boss 1

  case 6:

  img = image.createimage("/blod.png");// blod

  break;

  case 7:

  img = image.createimage("/explored.png");// explored

  break;

  }

  } catch (exception e) {

  system.out.println("设置敌人的图片不能为空");

  }

  }

  public void draw(graphics g) {

  g.drawimage(img, x, y, 0);

  }

  public void cycle() {

  switch (gettype()) {

  case 0:

  y += gety() % 1 + 1;

  break;

  case 1:

  y += gety() % 2 + 1;

  break;

  case 2:

  y += gety() % 2 + 2;

  break;

  case 3:

  y += gety() % 2 + 3;

  break;

  case 4:

  y += gety() % 2 + 5;

  break;

  case 5:

  y += gety() % 2 + 8;

  break;

  case 6:

  y += gety() % 1 + 1;

  break;

  }

  }

  /**

  * 设置敌人的x坐标

  *

  * @param xx

  */

  public void setx(int xx) {

  x = xx;

  }

  /**

  * 得到敌人当前的x坐标

  *

  * @return

  */

  public int getx() {

  return x;

  }

  /**

  * 设置敌人的y坐标

  *

  * @param yy

  */

  public void sety(int yy) {

  y = yy;

  }

  /**

  * 得到敌人当前的y坐标

  *

  * @return

  */

  public int gety() {

  return y;

  }

  /**

  * 得到敌人类型

  *

  * @return

  */

  public int gettype() {

  return type;

  }

  /**

  * 设置敌人类型

  *

  * @param type

  */

  public void settype(int type) {

  this.type = type;

  }

  }

  ball.java – 子弹类,其中定义了子弹的位置及子弹运行轨迹