⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 highscorecanvas.java

📁 J2ME手机游戏:是男人就下100层,根据FLASH游戏改写
💻 JAVA
字号:
package src;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.rms.RecordStore;

public class HighScoreCanvas extends Canvas implements CommandListener{
	public int[]          hiScores = new int[10];
	//private int            score;
	 private Command pauseCommand = null;
	 private Command exitCommand = null;
	 private GameMIDlet midlet = null;
	
	public HighScoreCanvas(GameMIDlet midlet)
	{
		 this.midlet = midlet;
		 this.readHiScores();
		 pauseCommand = new Command("重开",Command.STOP,0);
		 exitCommand = new Command("退出",Command.EXIT,0);
		 this.addCommand(pauseCommand);
		 this.addCommand(exitCommand);
		 this.setCommandListener(this);
	}
	
	 public void updateHiScores(int score) {
		    // See whether the current score made the hi score list
		    int i;
		    for (i = 0; i < 10; i++)
		      if (score > hiScores[i])
		        break;

		    // Insert the current score into the hi score list
		    if (i < 10) {
		      for (int j = 4; j > i; j--) {
		        hiScores[j] = hiScores[j - 1];
		      }
		      hiScores[i] = score;
		    }
		  }

		  private void readHiScores()
		  {
		    // Open the hi scores record store
		    RecordStore rs = null;
		    try {
		      rs = RecordStore.openRecordStore("HiScores", true);
		    }
		    catch (Exception e) {
		    }

		    if (rs != null) {
		      // Read the hi score records
		      try {
		        int    len;
		        byte[] recordData = new byte[8];

		        for (int i = 1; i <= rs.getNumRecords(); i++) {
		          // Re-allocate record holder if necessary
		          if (rs.getRecordSize(i) > recordData.length)
		            recordData = new byte[rs.getRecordSize(i)];

		          // Read the score and store it in the hi score array
		          len = rs.getRecord(i, recordData, 0);
		          hiScores[i - 1] = (Integer.parseInt(new String(recordData, 0, len)));
		        }
		      }
		      catch (Exception e) {
		        System.err.println("Failed reading hi scores!");
		      }

		      // Close the record store
		      try {
		        rs.closeRecordStore();
		      }
		      catch (Exception e) {
		        System.err.println("Failed closing hi score record store!");
		      }
		    }
		    else {
		      // The record store doesn't exist, so initialize the scores to 0
		      for (int i = 0; i < 10; i++)
		        hiScores[i] = 0;
		    }
		    
		  }

		  public void writeHiScores()
		  {
		    // Delete the previous hi scores record store
		    try {
		      RecordStore.deleteRecordStore("HiScores");
		    }
		    catch (Exception e) {
		    }

		    // Create the new hi scores record store
		    RecordStore rs = null;
		    try {
		      rs = RecordStore.openRecordStore("HiScores", true);
		    }
		    catch (Exception e) {
		      System.err.println("Failed creating hi score record store!");
		    }

		    // Write the scores
		    for (int i = 0; i < 10; i++) {
		      // Format each score for writing
		      byte[] recordData = Integer.toString(hiScores[i]).getBytes();

		      try {
		        // Write the score as a record
		        rs.addRecord(recordData, 0, recordData.length);
		      }
		      catch (Exception e) {
		        System.err.println("Failed writing hi scores!");
		      }
		    }

		    // Close the record store
		    try {
		      rs.closeRecordStore();
		    }
		    catch (Exception e) {
		      System.err.println("Failed closing hi score record store!");
		    }
		  }

		protected void paint(Graphics g) {
			// TODO 自动生成方法存根
			g.setColor(0x000000);     /// 默认清理为黑色
			g.fillRect(0, 0, getWidth(), getHeight());
		    
	        g.setColor(0x00ff00);                    //设置字体,颜色
		    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                    Font.SIZE_LARGE));
		    g.drawString("英雄榜", getWidth()/2-20, this.getHeight()/10,  Graphics.LEFT | Graphics.BOTTOM); 
		    g.drawLine(10,  this.getHeight()/8,this.getWidth() - 10,this.getHeight()/8);
		    
		    g.setColor(0xffffff);	
		    g.drawString("名次     地下层数",getWidth()/7,this.getHeight()/4, Graphics.LEFT | Graphics.BOTTOM);
		    for(int i = 0;i < hiScores.length;i++)
		    	
		    	g.drawString(i+1+"          "+hiScores[i],getWidth()/6+10 ,getHeight()/4+20*i , Graphics.LEFT | Graphics.TOP);
		    	
		    	
			
		    
		}

		public void commandAction(Command c, Displayable d) {
			// TODO 自动生成方法存根
			if(c.getCommandType()==Command.EXIT){
				   midlet.exitGame();
				}
				else if(c.getCommandType()==Command.STOP)
				{
					midlet.restartGame();
				}
		}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -