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

📄 russiablocksgame.java

📁 利用java写的俄罗斯方块
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    Color newBgColor =
            JColorChooser.showDialog(RussiaBlocksGame.this,"Set color for block", 
                               canvas.getBgColor());
    if (newBgColor != null)
     canvas.setBgColor(newBgColor);
   }
  });
  
  turnHardItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    int curLevel = getLevel();
    if (curLevel < MAX_LEVEL) setLevel(curLevel + 1);
   }
  });
  
  turnEasyItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    int curLevel = getLevel();
    if (curLevel > 1) setLevel(curLevel - 1);
   }
  });
  
  exitItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    System.exit(0);
   }
  });
  playItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    playGame();
   }
  });
  
  pauseItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    pauseGame();
   }
  });
  
  resumeItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    resumeGame();
   }
  });
  
  stopItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    stopGame();
   }
  });
  
  windowsRadioItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    setWindowStyle(plaf);
    canvas.fanning();
    controlPanel.fanning();
   }
  });
  
  motifRadioItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
    setWindowStyle(plaf);
    canvas.fanning();
    controlPanel.fanning();;
   }
  });
  rankItem.addActionListener(this);  
   metalRadioItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
    String plaf = "javax.swing.plaf.metal.MetalLookAndFeel";
    setWindowStyle(plaf);
    canvas.fanning();
    controlPanel.fanning();
   }
  });   
  
   }
   /*
    * 设定窗口风格
    */
   private void setWindowStyle(String plaf) 
   {
  try {
   UIManager.setLookAndFeel(plaf);
   SwingUtilities.updateComponentTreeUI(this);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
   
 private class Game implements Runnable
 {
  /*
   *  (non-Javadoc)
   * @see java.lang.Runnable#run()
   * 游戏线程的run函数
   */
  public void run()
  {
   int col=(int)(Math.random()*(canvas.getCols()-3));
   style=RussiaBlock.STYLES[(int)(Math.random()*RussiaBlock.BLOCK_KIND_NUMBER)][(int)(Math.random()*RussiaBlock.BLOCK_STATUS_NUMBER)];
    
         while (playing) {
    if (block != null) {    //第一次循环时,block为空
     if (block.isAlive()) {
      try {
       Thread.sleep(100);
      } catch (InterruptedException ie) {
       ie.printStackTrace();
      }
      continue;
     }
    }
    
    checkFullLine();
    
    if (isGameOver()) {     //检查游戏是否应该结束了
     playItem.setEnabled(true);
     pauseItem.setEnabled(true);
     resumeItem.setEnabled(false);
     controlPanel.setPlayButtonEnabled(true);
     controlPanel.setPauseButtonLabel(true);
     try{
     RandomAccessFile rankfile=new RandomAccessFile("rank.txt","r");
     byte[] b=new byte[1024];
     byte[] b1;
     String rankstr="";
     String userName="无名氏";
    int i= rankfile.read(b);
    String[] usersName=new String[10];
    int[] scores=new int[10];
    if(i>0)
    {
    	b1=new byte[i];
    	for(int m=0;m<i;m++)
    		b1[m]=b[m];
    	System.out.println(i);
      rankstr=new String(b1);
      System.out.println(rankstr);
      StringTokenizer token=new StringTokenizer(rankstr,"#");
      int j=0;
      boolean flag=false;
      String tempName=null;
	  int tempscore=0;
      while(token.hasMoreTokens()&&j<10)
      {  flag=false;  	  
    	  StringTokenizer token1=new StringTokenizer(token.nextToken(),":");
    	  usersName[j]=token1.nextToken();
    	  scores[j]=Integer.parseInt(token1.nextToken());    	  
    	  if(score>scores[j]&&tempName==null)
    	  {   
    		  tempName=usersName[j];
    		  tempscore=scores[j];
    		  userName=JOptionPane.showInputDialog("恭喜您,您已经闯入了十强,请输入你的大名。");
    		    while(userName.contains(":")||userName.contains("#"))
    		    {
    		        JOptionPane.showMessageDialog(null, "你的大名内含有非法字符如(:,#),请重新输入!");
    		    	userName=JOptionPane.showInputDialog("恭喜您,您已经闯入了十强,请输入你的大名。");
    		    }   
    		    usersName[j]=userName;
    		    scores[j]=score;
    		    System.out.println("ok!!!!"+j);
    		    flag=true;
    		    }
    	  if(tempName!=null&&!flag)
    	  {
    		   String temp1=tempName;
    		   int temp2=tempscore;
    		   tempName=usersName[j];
     		   tempscore=scores[j];    	
     		   usersName[j]=temp1;
     		   scores[j]=temp2;
    	  }
    	  j++;
    	  }
      if(j<9&&tempName!=null)
      {
    	  usersName[j]=tempName;
	      scores[j]=tempscore;
	      j++;
      }
      if(tempName==null&&j<9)
      {
    	  userName=JOptionPane.showInputDialog("恭喜您,您已经闯入了十强,请输入你的大名。");
		    while(userName.contains(":")||userName.contains("#"))
		    {
		        JOptionPane.showMessageDialog(null, "你的大名内含有非法字符如(:,#),请重新输入!");
		    	userName=JOptionPane.showInputDialog("恭喜您,您已经闯入了十强,请输入你的大名。");
		    }    		  
    	  usersName[j]=userName;
    	  scores[j]=score;
          j++;        
      }
      rankstr="";
      for(int n=0;n<j;n++)
    	  rankstr+=usersName[n]+":"+ scores[n]+"#";
      rankstr=rankstr.substring(0,rankstr.lastIndexOf("#"));
      System.out.println("rankstr:"+rankstr);
      }      
   
    else
    {
    userName=JOptionPane.showInputDialog("恭喜您,您已经闯入了十强,请输入你的大名。");
    while(userName.contains(":")||userName.contains("#"))
    {
        JOptionPane.showMessageDialog(null, "你的大名内含有非法字符如(:,#),请重新输入!");
    	userName=JOptionPane.showInputDialog("恭喜您,您已经闯入了十强,请输入你的大名。");
    }    	   
    System.out.println(userName);
    	rankstr+=userName+":"+score;
    } 
    rankfile.close();
    File file=new File("rank.txt");
    file.delete();
    rankfile=new RandomAccessFile("rank.txt","rw");
     b=rankstr.getBytes();
     rankfile.write(b);     
     b=null;
     }
     catch(Exception e)
     {
    	System.out.println(e); 
     }    
     reportGameOver();
     return;
  }
  block = new RussiaBlock(-1, col, getLevel(),style);
  block.start();
  
  col=(int)(Math.random()*(canvas.getCols()-3));
   style=RussiaBlock.STYLES[(int)(Math.random()*RussiaBlock.BLOCK_KIND_NUMBER)][(int)(Math.random()*RussiaBlock.BLOCK_STATUS_NUMBER)];
    controlPanel.setBlockStyle(style);
 }
 }
 /*
  * 判断是否能消去整行
  */
  public void checkFullLine() 
  {
   for (int i = 0; i < canvas.getRows(); i++) {
    int row = -1;
    boolean fullLineColorBox = true;
    for (int j = 0; j < canvas.getCols(); j++) {
     if (!canvas.getBox(i, j).isColorBox()) {
      fullLineColorBox = false;
      break;
     }
    }
    if (fullLineColorBox) {
     curLevelScore += PER_LINE_SCORE;
     score += PER_LINE_SCORE;
     System.out.println("score:"+score);
     row = i--;
     canvas.removeLine(row);
    }
   }
  }
  /*
   * 判断游戏是否结束
   */
  private boolean isGameOver() {
   for (int i = 0; i < canvas.getCols(); i++) {
    RussiaBox box = canvas.getBox(0, i);
    if (box.isColorBox()) return true;
   }
   return false;
  }
 }
 public static void main(String[] args) 
 {
  new RussiaBlocksGame("俄罗斯方块");
 }
public void actionPerformed(ActionEvent arg0) 
{
	// TODO Auto-generated method stub
	if(arg0.getSource().equals(rankItem))
		new rankDialog(this);
	if(arg0.getSource().equals(helpItem))
		JOptionPane.showMessageDialog(null,"这都不会啊,自己上网找找游戏规则吧。");
	if(arg0.getSource().equals(authorItem))
		JOptionPane.showMessageDialog(null,"对不起,关于本人,无可奉告!");
}
}

⌨️ 快捷键说明

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