maingame.java
来自「j2me 连连看源代码」· Java 代码 · 共 2,475 行 · 第 1/5 页
JAVA
2,475 行
import javax.microedition.lcdui.Graphics;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.lang.Thread;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Font;
import javax.microedition.rms.RecordStore;
import java.lang.Runtime;
import java.util.Random;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.rms.*;
//#ifdef NOKIA
//# import com.nokia.mid.ui.*;
//#else
import javax.microedition.lcdui.Canvas;
//#endif
/*
* Created on 2006-1-9
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author aaron
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
//#ifdef NOKIA
//# public class MainGame extends FullCanvas implements Runnable{
//#else
//#ifdef COMMAND
//# public class MainGame extends Canvas implements Runnable,CommandListener{
//#else
public class MainGame extends Canvas implements Runnable{
//#endif
//#endif
//System variables
private static GameMIDlet game_midlet; // the midlet of the game
private static Thread game_thread; // the main thread of the game
private static MainGame main_game;
private static final int TIME_PER_TICKER = 70; // the time interval of the game loop
// it should adjust according to the game.
private static Runtime rt = Runtime.getRuntime() ;
public static final Font font = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
private Random rand = new Random();
private static FontSmall picFont;
private static StringBuffer system_buffer = new StringBuffer();
private static long key_counter;
private static byte tempKey;
private static byte response_key;
private static boolean isKeyReleased;
private static final String RMS_NAME = "LLK6";
private static RecordStore rs;
private static boolean isHaveRecord;
private static final int MAX_RANK = 10;
public static pRank game_rank;
//when there is a callying set this flag to pause the thread
public static boolean thread_flag;
private static pEffect m_pEffect = new pEffect();
//#ifdef COMMAND
//# private static Command m_pNext;
//# private static Command m_pBack;
//#endif
//game variables
private static byte game_stage; // hold the stage information of the game
private static byte old_game_stage;
private static byte last_stage;
public static int screen_width; // the width of the screen
public static int screen_height; // the height of the screen
private static int screen_widthC; // the center width of the screen
private static int screen_heightC; // the center height of the screen
private static int view_x,view_y;
private static int back_gridx,back_gridy; //how many tiles should be draw to fill the back in x or y coordination
private static int back_width,back_height;
private static byte m_iKey; // hold the pressed key's code
private static byte image_loaded = 0; // keep the value of images numbers loaded
private byte menu_option; // indicate the option selected in the menu;
private int game_counter;
private int stunt_counter;
private int item_pointer;
private int help_pointer;
private static boolean hasUpArrow;
private static boolean hasDownArrow;
private static boolean hasArrow;
private static boolean bSound;
private byte tile_width; // the width of the tile
private byte tile_height; // the height of the tile
private byte tile_widthC,tile_heightC;
private byte row_count; // the number of the rows
private byte line_count; // the number of the lines
//varible about loading stage
private static final byte[] wave_y = {0,2,4,2,0};
//variable about help stage and about stage
private static String[] menu_ui;
private static String[] help_ui;
private static String[] about_ui;
private int height;
private int item_size;
private int item_offy;
private int items;
private static final int MARGIN = 2;
private static final int item_height = font.getHeight() + MARGIN;
//variable about the menu window
//and it's width,height,y-coordination
private static Window myWin;
private static short menu_width;
private static int menu_height;
private static short menu_y;
//varible about help
private static int[] stunt_index;
//variable about title
private int animation_counter; // as a counter of the title animation;
//variable about game
private static byte[][] map; // store the tile value
private static final byte EMPTY = 0; // if there is no tile in one grid,
// the value of this tile is EMPTY
private int game_level; // the current level of the game
private short tile_left; // store the tiles left to be solved;
private static final byte MAX_TILE = 33;
private static final byte MAX_LEVEL = 20;
private int hand_x,hand_y; // the coordination of the arrow
private int select_x ,select_y; // the coordination of the selected tile
private boolean isFirstSelect; // if it is selected the first time
private int m_CntX[], m_CntY[], m_CntMapsCount;
private boolean bCanDraw;
private int flashCounter = 0;
private Hashtable ht;
private boolean hasMotion;
private boolean hasStunt; // make the sure the stunt exec only on time
private boolean hasStuntFlashing;
private boolean hasStuntTime;
private boolean hasTremble;
private static boolean hasMusic;
private boolean hitWrongPair;
private static int tremble_y;
private int stunt_time_counter;
private int wrong_counter;
private byte map_value;
private static final byte MAX_PAIR = 3; // when it is a bomb , the max pair to be bombed
private static int level_time; // the time for each level
private static int max_time;
private static int combo_time;
private static boolean hasCombo;
private static int combo_counter;
//#ifdef S60
//# private static final int MINUTE = 12; // 7.5 per 1 second
//#endif
//#ifdef K700
//# private static final int MINUTE = 20;
//#endif
//#if E398 || V600
//# private static final int MINUTE = 12; // 8 per second
//#endif
//#if S40 || K500
//# private static final int MINUTE = 12;
//#endif
//#ifdef A760
private static final int MINUTE = 12;
//#endif
//#if K700 || A760
//#if K700
//# private static final int MAX_COMBO_TIME = 2*MINUTE; // combo time
//#else
private static final int MAX_COMBO_TIME = 2*MINUTE; // combo time
//#endif
//#else
//# private static final int MAX_COMBO_TIME = 3*MINUTE;
//#endif
private static final int MAX_LEVEL_TIME = 20*MINUTE; // max time of a level
private static final int MIN_LEVEL_TIME = 10*MINUTE; // min time of a level
// level_time is between MAX_LEVEL_TIME and MIN_LEVEL_TIME
private static final int MAX_HINT_TIME = 10*MINUTE;
private static final int MAX_STUNT_TIME = 20*MINUTE;
private static final int MAX_TREMBLE_TIME = 5*MINUTE;
private static final short SCORE_UNIT = 50; //get the unit score when a pair of tile has connected
private static int hint_time;
private static boolean hasHint;
private static boolean isGameOver; // whether game is over
private static boolean canOver; // whether all animation is finished
private static boolean canSelect; // whether another pair of tiles can be selected
// here to make sure the first selection execute completely
private static int level_score; // hold the score of the level
private static int game_score;
private static int combo_score; // hold the score of combo
private static int temp_combo_score;
private static String str_score;
private static String str_level;
private static Tile tile_score;
private static Tile tile_combo;
private static int tile_step;
private static final int TOTAL_FRAME = 8; // total fram of the tile score
//#if MEDIUM
//# private static int score_x = 50; // the x coordination of the score
//# private final int level_x = 123;
//# private static final int OFF_X = 8;
//# private static final int OFF_Y = 27;
//#
//#if E398 || V600
//# private static final int TIMEBAR_Y = 0;
//# private final int score_y = 193;
//# private final int level_y = 193;
//#else
//# private static final int TIMEBAR_Y = 2;
//# private final int score_y = 195;
//# private final int level_y = 195;
//#endif
//#
//# private static final int RANK_Y = 50;
//# private static final int NAME_LENGTH = 85;
//# private static final int LEVEL_X = 36;
//# private static final int LEVEL_Y = 27;
//# private static int SHOW_NUM = 7;
//# private static int TITLE_Y = 44;
//# private static final byte LINE_A_PAGE = 5;
//# private static final int MENU_Y = 125;
//#endif
//#if SMALL
//# private static int score_x = 28; // the x coordination of the score
//# private final int score_y = 121;
//# private final int level_x = 94;
//# private final int level_y = 121;
//# private static final int OFF_X = 4;
//# private static final int OFF_Y = 10;
//# private static final int TIMEBAR_Y = 3;
//# private static final int RANK_Y = 35;
//# private static final int NAME_LENGTH = 40;
//# private static final int LEVEL_X = 25;
//# private static final int LEVEL_Y = 2;
//# private static int SHOW_NUM = 5;
//# private static int TITLE_Y = 30;
//# private static final byte LINE_A_PAGE = 4;
//# private static final int MENU_Y = 78;
//#endif
//#if BIG
private static int score_x = 57; // the x coordination of the score
private final int score_y = 226;
private final int level_x = 160;
private final int level_y = 226;
private static final int OFF_X = 20;
private static final int OFF_Y = 27;
private static final int TIMEBAR_Y = 2;
private static final int RANK_Y = 50;
private static final int NAME_LENGTH = 100;
private static final int LEVEL_X = 50;
private static final int LEVEL_Y = 27;
private static int SHOW_NUM = 5;
private static int TITLE_Y = 65;
private static final byte LINE_A_PAGE = 5;
private static final int MENU_Y = 145;
//#endif
//variable for waiting
private static int wait_counter;
private static final byte TILE_NONE = 0;
private static final byte TILE_INIT = 1;
private static final byte TILE_ACTIVE = 2;
private static final byte TILE_SCORE = 3;
private static final byte TILE_COMBO = 4;
// stunt variables
private static byte stunt_type;
private static final byte STUNT_NONE = 0;
private static final byte MAX_STUNT = 4;
private static final byte STUNT_RESHUFFLE = 31;
private static final byte STUNT_HINT = 32;
private static final byte STUNT_BOMB = 30;
private static final byte STUNT_TIME = 33;
private static String[] record_name =
{
"-","-","-","-","-"
,"-","-","-","-","-"
};
private static int[] record_score =
{
0,0,0,0,0,0,0,0,0,0
};
//stage varialbes
private static final byte STAGE_NONE = 0;
private static final byte STAGE_LOADING = 5;
private static final byte STAGE_TITLE = 10;
private static final byte STAGE_SETTING = 15;
private static final byte STAGE_HELP = 20;
private static final byte STAGE_RANKING = 25;
private static final byte STAGE_CREDIT = 30;
private static final byte STAGE_GAME = 35;
private static final byte STAGE_GAMEOVER = 40;
private static final byte STAGE_EXIT = 45;
private static final byte STAGE_INIT = 50;
private static final byte STAGE_ABOUT = 55;
private static final byte STAGE_LEVEL = 60;
private static final byte STAGE_SYSTEM = 65;
private static final byte STAGE_WAIT = 70;
private static final byte STAGE_CMCC = 100;
private static final byte STAGE_LOGO = 105;
private static final byte STAGE_LOGO1 = 106;
private static final byte STAGE_PAY = 110;
//if there is other stages , add here
// key constants
private static final byte KEY_NONE = 0;
private static final byte KEY_LEFT = 1;
private static final byte KEY_RIGHT = 4;
private static final byte KEY_UP = 7;
private static final byte KEY_DOWN = 10;
private static final byte KEY_FIRE = 13;
private static final byte KEY_L = 14;
private static final byte KEY_R = 15;
private static final byte KEY_N2 = 17; // key number 2
private static final byte KEY_N4 = 20;
private static final byte KEY_N6 = 23;
private static final byte KEY_N8 = 26;
private static final byte KEY_N5 = 29;
//image variables
private static final byte MAX_IMAGE = 65;
private static final byte MIN_IMAGE = 6;
private static final byte TITLE = 33;
private static final byte MENU_ITEM = 34;
private static final byte XIN_XIN = 35;
private static final byte GAME = 38;
private static final byte TIME_BAR = 52;
private static final byte ARROW = 44;
private static final byte HERAT = 47;
private static final byte NUMBER = 53;
private static final byte SCORE = 46;
private static final byte COMBO = 45;
private static final byte GAME0VER = 55;
private static final byte CONGRATULATION = 54;
private static final byte BK = 39;
private static final byte UPARROW = 58;
private static final byte DOWNARROW = 59;
private static final byte LEFTARROW = 60;
private static final byte RIGHTARROW = 61;
private static final byte BOMB = 29;
private static final byte COMBOCHAR = 62;
private static final byte COPYRIGHT = 65;
private static final byte OK = 57;
private static final byte BACK = 56;
//image information ,mainly width and height
private static int timebar_height;
private static int timebar_width;
private static int arrow_height;
private static int timebar_x;
private static int congratulation_height;
private static int combochar_width,combochar_height,combo_width;
private static Image[] m_pImage = new Image[MAX_IMAGE]; // store the image reference loaded
static{
for(int i = 0; i < 6; i++)
{
m_pImage[i] = loadImage(i);
}
}
int dx,menuSelector;
boolean reduce = true;
final int maxMenuItem = 5;
String payMsg[];
int errorTime;
byte tmpState;
byte payType;
byte payTime;
byte payMoney;
byte nowTime;
byte money;
//constructor of the MainGame class
MainGame(GameMIDlet pMIDlet)
{
game_midlet = pMIDlet;
main_game = this;
screen_width = Common.SCREEN_WIDTH;// get the width of the screen
screen_height = Common.SCREEN_HEIGHT; // get the height of the screen
screen_widthC = screen_width >>1;
screen_heightC = screen_height >>1;
view_x = (screen_width - Common.VIEW_WIDTH )>>1;
view_y = (screen_height - Common.VIEW_HEIGHT)>>1;
bSound = true;
thread_flag = false;
game_stage = STAGE_LOADING;
// init the game variables
image_loaded = MIN_IMAGE;
tile_width = Common.TILE_WIDTH;
tile_height = Common.TILE_HEIGHT ;
row_count = Common.ROW_COUNT;
line_count = Common.LINE_COUNT ;
tile_widthC = (byte)(tile_width>>1);
tile_heightC = (byte)(tile_height>>1);
game_level = 0;
//create the map
map = new byte[row_count][line_count];
m_CntX = new int[tile_height * 3];
m_CntY = new int[tile_height * 3];
stunt_index = new int[MAX_STUNT];
ht = new Hashtable();
picFont = new FontSmall(this);
// initialize the score tile
tile_score = new Tile();
tile_score.tile_frame = SCORE;
tile_score.tile_type = TILE_NONE;
tile_step = (tile_height<<1)/TOTAL_FRAME;
//initialize the combo tile
tile_combo = new Tile();
tile_combo.tile_frame = COMBO;
tile_combo.tile_type = TILE_NONE;
tile_step = (tile_height<<1)/TOTAL_FRAME;
//kitty_tile = new Tile[(row_count - 2)*(row_count - 2)];
// init rms
initRMS();
//#ifdef FULLCANVAS
setFullScreenMode(true);
//#endif
}
/**
* TODO new the thread and start the thread ,be carefull with the
* synchronization
*/
public void start(){
game_thread = new Thread(this);
game_thread.start();
}
/**
* TODO stop the game , make the thread null
*/
public void stop()
{
game_thread = null;
}
public void showNotify()
{
thread_flag = false;
}
public void hideNotify()
{
thread_flag = true;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?