📄 tetris.java
字号:
package code;////////////////////////////////////////////////////////////////////////////////////////////////////** * 俄罗斯方块 * 高雷 * 2007年11月30日 *////////////////////////////////////////////////////////////////////////////////////////////////////import javax.microedition.midlet.*; //j2me MIDlet程序必须继承MIDlet类,所以要引入此包import javax.microedition.lcdui.*; //Display这个类所在包import javax.microedition.rms.*;import java.io.*;import java.util.*;import java.util.Calendar;import java.util.Date;///////////////////////////////////////////////////////////////////////////////////////////////////public class Tetris extends MIDlet { static Tetris s_midlet; //MIDlet类的静态对象,方便实用 MIDlet类方法 static Display s_display = null;//用来显示 Canvas static cGame176x220 s_game = null; //Canvas类对象,主要实现游戏的类 public Tetris() { s_midlet = this; } /** * 程序开始 系统会调用这个函数 * 也有些手机 可以把程序初始化部分放到构造函数里,这连个地方应视手机的不同而定! */ public void startApp() { ReadData(); if (s_display == null) { s_display = Display.getDisplay(this);//创建Display对象,参数是MIDlet类对象,也就是我们当前写的这个Minesweeper类 } if (s_game == null) { s_game = new cGame176x220(); //创建 Canvas对象 s_display.setCurrent(s_game); //把Canvas对象设置成当前显示 } else { s_display.setCurrent(s_game); } } /** * 程序暂停 系统会自动调用这个函数,不是所有手机都支持, * 手机在接到中断,如 来电,来短信时候会调用这个函数,这个函数 通常是空的! */ public void pauseApp() { } /** * 程序关闭 系统会调用这个函数,如果希望关闭程序的时候保存数据,可在这个函数里添加保存数据的方法 * 比如游戏进行中,按了关机键,程序就会调用这个函数,也可以在程序中调用这个函数来结束游戏! */ public void destroyApp(boolean unconditional) { WriteData(); notifyDestroyed(); } static Calendar cal = Calendar.getInstance(); static Date date = null; public static String getDate() { date = new Date( System.currentTimeMillis() ); cal.setTime( date ); return "" +( cal.get( Calendar.MONTH ) + 1 )+"月"+cal.get( Calendar.DAY_OF_MONTH )+"日"+cal.get( Calendar.YEAR ); } public static String getTime() { date = new Date( System.currentTimeMillis() ); cal.setTime( date ); return "_" + cal.get( Calendar.HOUR_OF_DAY ) + "_" + cal.get( Calendar.MINUTE ) + "_" + cal.get( Calendar.SECOND ); } public static int getYear() { date = new Date( System.currentTimeMillis() ); cal.setTime( date ); return cal.get( Calendar.YEAR ); } public static int getMonth() { date = new Date( System.currentTimeMillis() ); cal.setTime( date ); return ( cal.get( Calendar.MONTH ) + 1 ); } public static int getDay() { date = new Date( System.currentTimeMillis() ); cal.setTime( date ); return cal.get( Calendar.DAY_OF_MONTH ); } public static final String gameName = "Tetris"; public static final int recoreMax = 8; public static int[] success = new int[recoreMax]; public static String[] dateTime = new String[recoreMax]; public static int[] year = new int[recoreMax]; public static int[] month = new int[recoreMax]; public static int[] day = new int[recoreMax]; /** * 读取存档记录 */ public static void ReadData() { RecordStore store = null; RecordEnumeration result = null; byte data[]; try { store = RecordStore.openRecordStore( gameName, false ); result= store.enumerateRecords( null, null, false ); data = result.nextRecord(); store.closeRecordStore(); ByteArrayInputStream inputstream = new ByteArrayInputStream( data ); DataInputStream datastream = new DataInputStream( inputstream ); int k; k = datastream.readInt(); // 11备用 for(int i=0; i<recoreMax; i++ ) { success [i] = datastream.readInt(); //分数// dateTime[i] = datastream.readUTF(); //日期 year [i] = datastream.readInt(); //年 month [i] = datastream.readInt(); //月 day [i] = datastream.readInt(); //日 }//----------------2.0-------------- k = datastream.readInt(); // 12备用 } catch ( Exception e ) { e.printStackTrace(); } System.out.println("...ReadData...ok"); } /** * 写记录 */ public static void WriteData() { ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); DataOutputStream datastream = new DataOutputStream( bytestream ); try { int k0=0,k1=1;//保留字 datastream.writeInt(k0); for(int i=0; i<recoreMax; i++ ) { datastream.writeInt( success [i] ); // 分数// datastream.writeUTF( new String( dateTime[i].getBytes(),"UTF-8") ); // 日期 datastream.writeInt( year [i] ); //年 datastream.writeInt( month [i] ); //月 datastream.writeInt( day [i] ); //日 } datastream.writeInt(k1); } catch ( Exception e ) { e.printStackTrace(); } RecordStore store = null; try { RecordStore.deleteRecordStore( gameName ); } catch ( RecordStoreNotFoundException e ) { } catch ( Exception e ) { } try { store = RecordStore.openRecordStore( gameName, true ); store.addRecord( bytestream.toByteArray(), 0, bytestream.toByteArray().length); store.closeRecordStore(); } catch ( Exception e ) { } System.out.println("...WriteData...ok"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -