圣剑英雄2的手机实现
圣剑英雄2的手机实现。。Canvas代码(第一部分)
圣剑英雄2的手机实现。。。。
交流!!交流!!!
由于发布字数受限制,所以分开两部分
/** * */
/** * @author jencsy * */ import java.util.Random; import java.io.IOException; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*;
// import java.io.*;
public class HeroLegend2Canvas extends GameCanvas implements Runnable {
// 游戏状态 private static final short TITLE = 0;
private static final short GAME_START = 1;
private static final short GAME_PLAYING = 2;
private static final short GAME_FIGHT = 3;
private static final short GAME_END = 4;
private short gamestate;// 存储状态标志
private boolean conti = false;// run循环标志
private TiledLayer playingmap1;// 人物移动场景1
private TiledLayer playingmap2;// 人物移动场景2
private TiledLayer fightmap;// 战斗场景
private LayerManager lm;
private int viewx = 0;// 视窗x值
private int viewy = 0;// 视窗y值
// enemy[0]:怪物 // enemy[1]:人物 private Sprite[] enemy = new Sprite[2];
private Sprite[] atmenu = new Sprite[5];// 战斗菜单资源
private Sprite[] rolestates = new Sprite[2];// 英雄战斗状态
private Sprite sword = null;
private int idx = 0;
// 怪物序列 private int enemyfreqence[][] = new int[][] { { 0, 1, 2, 3, 4, 5, 6 }, { 8, 9, 10, 11, 12 }, { 19, 18, 17, 16 }, { 21, 20 } };
// 英雄序列 private int herofrequence[][] = new int[][] { { 31, 30, 29, 28, 27, 26 }, { 15, 14, 13, 37, 37 }, { 42, 7 }, { 44, 45, 46, 47, 5 } };
// enemyfrequence[0]={0,1,2,3,4,5,6,7,8,9}; // private int h = 0;
private int menuindx = 0;// 战斗菜单标志
private int menuselect = 1;// 存储战斗菜单选项标志
private int bu = 20;// 怪物战斗移动序列步长
private int bu2 = -16;// 英雄战斗移动序列步长
private int indx2 = 0; // 怪物frame标志
private int count = 0;// 战斗顺序标志
private boolean at = false;// 战斗标志
private int enemyatack = 0;// 怪物战斗序列控制标志
private int heroatack = 0;// 英雄战斗序列控制标志
private boolean damagehero = false;// 英雄受到攻击标志
private int randomdh;// 人物攻击伤害随机数
private int randomde;// 怪物攻击伤害随机数
private int playmap = 1;// 移动地图标志
// int damageHhp = randomde + 30;
// int damageEhp = randomdh + 20;
private int damageHhp = 0;
private int damageEhp = 0;
private boolean keyselect = true;// 战斗菜单活动标志
private boolean who = true;// 画图层次标志
private int titleindx = 0;// 菜单标志
private int titleselect;// 存储菜单选项标志
private String substr = null;
private String substr0 = "新年快乐!!!努力喔,生命在于运动!!!";// 语句
private String substr1 = "It's a joke!!! '>_<'";
private String substr2 = "This is a demo !!!";
private int j = 1;// 语句循环标志
private int haha = 0;
private boolean hahaha = false;
private boolean damageenemy = false;// 怪物受到攻击标志
private boolean findedsword = false;// findsword控制标志
private int wait = 0;// findsword控制标志
private boolean startfight = false;// 进入战斗标志
// exit命令 private Command exitcmd = new Command("退出", Command.EXIT, 1);
// start命令 public Command startcmd = new Command("开始", Command.SCREEN, 2);
// private HeroLegend2MIDlet smm; // HeroLegend2Canvas herolegend2Canvas; private HeroLegend2_role herolegend2_role;
// int hp = herolegend2_role.HP/10 ; public HeroLegend2Canvas() throws IOException { super(true); // x = getWidth() / 2; // y = getHeight() / 2;
try { playingmap1 = createPlayingmap1(); playingmap2 = createPlayingmap2(); fightmap = createfightmap();
lm = new LayerManager(); lm.append(playingmap1); lm.append(playingmap2); lm.append(fightmap); // lm.append(herolegend2_role); sword = createCharacter("/pic/sword01.png", 14, 14); // int sx=(new Random()).nextInt()%50+100; // int sy=(new Random()).nextInt()%50+100; sword.setPosition(this.getWidth() - 20, this.getHeight() - 20); lm.append(sword);
enemy[0] = createCharacter("/pic/shot15.png", 40, 40); enemy[1] = createCharacter("/pic/shot11.png", 40, 40); enemy[0].setPosition(0, 80); enemy[1].setPosition(140, 80);
herolegend2_role = createhero("/pic/shot08.png"); herolegend2_role.setFrame(0); herolegend2_role.init(100, 100, 650, 650); herolegend2_role.setHeroLegend2Canvas(this); herolegend2_role.setLayManager(lm); lm.append(herolegend2_role); rolestates[0] = createCharacter("/pic/peoplestates01.png", 114, 40); rolestates[0].setPosition(10, 135);
atmenu[0] = createCharacter("/pic/b_fight.png", 12, 12); atmenu[1] = createCharacter("/pic/b_goods.png", 12, 12); atmenu[2] = createCharacter("/pic/b_leave.png", 12, 12); atmenu[3] = createCharacter("/pic/b_magic.png", 12, 12); atmenu[4] = createCharacter("/pic/b_replay.png", 12, 12);
atmenu[4].setPosition(150, 148); atmenu[0].setPosition(atmenu[4].getX(), atmenu[4].getY() - 14); atmenu[1].setPosition(atmenu[4].getX() - 14, atmenu[4].getY()); atmenu[3].setPosition(atmenu[4].getX(), atmenu[4].getY() + 14); atmenu[2].setPosition(atmenu[4].getX() + 14, atmenu[4].getY());
// damageHhp = 0; // damageEhp = 0;
} catch (Exception e) { System.out.println("wtong!!"); } System.gc();
splash2();
System.out.println(getWidth() + "sdf" + getHeight());
addCommand(exitcmd);
}
// 移动人物资源 public HeroLegend2_role createhero(String pic) { Image img = null; try { img = Image.createImage(pic); } catch (Exception e) {
} return new HeroLegend2_role(img, 20, 25); }
// sword资源 public Sprite createCharacter(String pic, int frameWidth, int frameHeight) { Image img = null; try { img = Image.createImage(pic); } catch (Exception e) { } return new Sprite(img, frameWidth, frameHeight); }
// 人物移动地图1 public TiledLayer createPlayingmap1() { Image img = null; try { img = Image.createImage("/pic/playingmap06.png");
} catch (Exception exp) { System.out.println("layer create image"); }
TiledLayer tiledLayer = new TiledLayer(20, 20, img, 18, 18); int[] map1 = { 3, 4, 1, 2, 10, 10, 10, 10, 10, 31, 33, 10, 3, 4, 10, 17, 17, 10, 10, 10, 17, 17, 6, 7, 17, 17, 10, 10, 8, 32, 34, 10, 10, 10, 10, 10, 10, 17, 17, 10, 10, 17, 7, 7, 7, 7, 10, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 10, 17, 7, 7, 7, 7, 10, 10, 10, 10, 8, 10, 10, 10, 10, 10, 10, 30, 30, 10, 10, 17, 7, 7, 7, 24, 10, 7, 10, 10, 10, 10, 16, 10, 16, 10, 10, 30, 30, 10, 10, 17, 17, 27, 7, 29, 7, 7, 7, 10, 10, 10, 16, 10, 10, 16, 10, 30, 30, 10, 10, 7, 7, 8, 8, 8, 8, 8, 10, 7, 10, 16, 16, 16, 16, 30, 30, 30, 8, 10, 7, 10, 10, 8, 10, 31, 32, 33, 34, 35, 16, 16, 30, 10, 30, 30, 30, 30, 8, 10, 10, 7, 8, 8, 8, 32, 32, 8, 16, 16, 16, 16, 16, 30, 17, 17, 26, 26, 8, 10, 7, 8, 8, 30, 32, 32, 32, 8, 32, 30, 30, 30, 30, 30, 30, 30, 3, 4, 8, 10, 10, 10, 7, 30, 32, 8, 8, 8, 8, 8, 30, 30, 30, 8, 8, 8, 8, 26, 8, 10, 10, 7, 30, 10, 10, 30, 30, 30, 30, 8, 8, 8, 30, 30, 30, 30, 30, 26, 8, 30, 8, 8, 7, 8, 8, 8, 8, 30, 30, 30, 30, 30, 30, 30, 8, 30, 30, 26, 8, 30, 7, 7, 7, 30, 30, 30, 10, 30, 30, 30, 30, 30, 30, 30, 8, 30, 26, 26, 8, 30, 10, 7, 7, 8, 8, 8, 8, 8, 8, 3, 4, 8, 8, 30, 30, 30, 26, 30, 8, 10, 10, 7, 8, 7, 7, 30, 30, 30, 30, 30, 30, 30, 8, 8, 8, 26, 26, 30, 30, 10, 7, 7, 10, 8, 8, 7, 30, 30, 30, 8, 8, 8, 8, 8, 8, 8, 30, 7, 7, 7, 10, 8, 7, 8, 30, 7, 7, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 7, 1, 2, 10, 10, 7, 30, 8, 7, 8, 30, 30, 30, 30, 8, 8, 8, 30, 3, 4, 8, 31, 34, 10, 10, 30, 30, 8, 30, 30, 30, 8, 8, 8, 8, 30, 8, 30, 30, 30, 30, 32, 33 }; for (int i = 0; i < map1.length; i++) { int column = i % 20; int row = (i - column) / 20; tiledLayer.setCell(column, row, map1[ i]); }
System.gc(); return tiledLayer; }