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

📄 jtetrixl.java

📁 俄罗斯方块的源代码。对于刚学编程的人有很大帮助
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  import   java.awt.*;   
  import   java.awt.event.*;   
  import   javax.swing.*;   
    
  public   class   JTetrix   extends   JFrame   implements   Runnable   {   
  private   ImageIcon   iconLogo;   
    
  private   JPanel   nextPanel,   scorePanel,   opPanel,   gamePanel,   hidedOpPanel;   
    
  private   JLabel   labLevel,   labLine,   labScore;   
    
  private   final   JTextArea   keyfocus;   //   键盘的操作焦点,隐藏于游戏画面之后   
    
  private   JButton   btnNew,   btnPause,   btnQuit;   
    
  private   showNextPiece   nextPieceArea;   //   预览下一方块   
    
  private   final   gameOpBoard   gameOpArea;   //   游戏画面   
    
  private   Container   c;   
    
  private   int   Level,   Score,   LineRemoved;   //   等级、得分、移除总行数   
    
  private   double   interval;   //   暂停时间,用于控制速度   
    
  private   boolean   pause;   //   是否暂停   
    
  private   Thread   loopThread;   //   游戏回圈执行绪   
    
  static   final   long   serialVersionUID   =   1;   
    
  public   JTetrix()   {   
  c   =   getContentPane();   
    
  //iconLogo   =   new   ImageIcon(JTetrix.class.getResource("logo.jpg"));   
  Level   =   1;   
  Score   =   0;   
  LineRemoved   =   0;   
    
  //   预览下一个方块的面版配置   
  nextPanel   =   new   JPanel();   
  nextPanel.setLayout(null);   
  nextPanel.setBorder(BorderFactory.createTitledBorder("下一个"));   
  nextPanel.setLocation(10,   10);   
  nextPanel.setSize(150,   120);   
    
  nextPieceArea   =   new   showNextPiece(5,   6,   15,   15);   
  nextPieceArea.generateNextPiece();   //   先产生第一片待取   
  nextPieceArea.setLocation(40,   20);   
  nextPanel.add(nextPieceArea);   
    
  //   等级、得分面版配置   
  scorePanel   =   new   JPanel();   
  scorePanel.setBorder(BorderFactory.createTitledBorder("等级   /   得分"));   
  scorePanel.setLocation(10,   140);   
  scorePanel.setSize(150,   160);   
  scorePanel.add(new   JLabel("                     速度等级                     "));   
  scorePanel.add(labLevel   =   new   JLabel("1",   SwingConstants.CENTER));   
  scorePanel.add(new   JLabel("                     移除行数                     "));   
  scorePanel.add(labLine   =   new   JLabel("0",   SwingConstants.CENTER));   
  scorePanel.add(new   JLabel("                     目前得分                       "));   
  scorePanel.add(labScore   =   new   JLabel("0",   SwingConstants.CENTER));   
    
  //   功能面版配置   
  opPanel   =   new   JPanel();   
  opPanel.setBorder(BorderFactory.createTitledBorder(""));   
  opPanel.setLocation(10,   320);   
  opPanel.setSize(150,   130);   
  opPanel.add(btnNew   =   new   JButton("开新游戏"));   
  opPanel.add(btnPause   =   new   JButton("暂停游戏"));   
  opPanel.add(btnQuit   =   new   JButton("关闭游戏"));   
    
  //   游戏操作画面面版配置   
  gameOpArea   =   new   gameOpBoard(10,   22,   18,   18);   
  gameOpArea.setLocation(20,   20);   
  gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix());   //   先取出一个方块   
  nextPieceArea.generateNextPiece();   //   预览窗格产生下一个方块预览   
    
  gamePanel   =   new   JPanel();   
  gamePanel.setLayout(null);   
  gamePanel.setLocation(180,   20);   
  gamePanel.setSize(12   *   18,   24   *   18);   
    
  gamePanel.setBorder(BorderFactory.createTitledBorder(""));   
  gamePanel.add(gameOpArea);   
    
  //   隐藏的操作面版配置   
  hidedOpPanel   =   new   JPanel();   
  hidedOpPanel.setLayout(new   BorderLayout());   
  hidedOpPanel.setLocation(200,   40);   
  hidedOpPanel.setSize(10   *   18,   22   *   18);   
  keyfocus   =   new   JTextArea();   
    
  keyfocus.setEditable(false);   
  hidedOpPanel.add(keyfocus);   
    
  //   加入面版至视窗   
  c.setLayout(null);   
  c.add(nextPanel);   
  c.add(scorePanel);   
  c.add(opPanel);   
  c.add(gamePanel);   
  c.add(hidedOpPanel);   
    
  //   键盘事件处理   
  keyfocus.addKeyListener(new   KeyAdapter()   {   
  public   void   keyPressed(KeyEvent   e)   {   
  int   key   =   e.getKeyCode();   
    
  if   (key   ==   KeyEvent.VK_RIGHT)   
  gameOpArea.moveRight();   
  else   if   (key   ==   KeyEvent.VK_LEFT)   
  gameOpArea.moveLeft();   
  else   if   (key   ==   KeyEvent.VK_UP)   
  gameOpArea.RotateRL(1);   
  else   if   (key   ==   KeyEvent.VK_DOWN)   
  gameOpArea.RotateRL(0);   
  else   if   (key   ==   KeyEvent.VK_SPACE)   {   
  gameOpArea.soonMoveDown();   
  }   
  }   
  });   
    
  //   开新游戏   
  btnNew.addActionListener(new   ActionListener()   {   
  public   void   actionPerformed(ActionEvent   e)   {   
  newGame();   
  keyfocus.requestFocus();   
  }   
  });   
    
  //   暂停游戏   
  btnPause.addActionListener(new   ActionListener()   {   
  public   void   actionPerformed(ActionEvent   e)   {   
  pause   =   true;   
    
  JOptionPane.showOptionDialog(null,   "程式名称:\n         JTetrix   v0.1\n"   
  +   "程式设计:\n         younganne\n"   
  +   "简介:\n         一个用Java写的俄罗斯方块游戏\n",   "关于JTetrix",   
  JOptionPane.DEFAULT_OPTION,   
  JOptionPane.INFORMATION_MESSAGE,   null,   null,   null);   
    
  pause   =   false;   
  interval   =   2000   /   Level;   
  loopThread.interrupt();   
  keyfocus.requestFocus();   
  }   
  });   
    
  //   关闭游戏   
  btnQuit.addActionListener(new   ActionListener()   {   
  public   void   actionPerformed(ActionEvent   e)   {   
  System.exit(0);   
  }   
  });   
    
  //   启动游戏回圈执行绪   
  loopThread   =   new   Thread(this);   
  loopThread.start();   
    
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  setTitle("JTetrix   v0.1");   
  setSize(420,   500);   
    
  setVisible(true);   
  }   
    
  //   游戏回圈的执行绪执行对象   
  public   void   run()   {   
  while   (true)   {   //   游戏回圈   
  try   {   
  //   是否暂停   
  if   (pause)   
  interval   =   999999999;   
  else   
  interval   =   2000   /   Level;   
    
  if   (gameOpArea.isUpdated())   {//   由游戏画面的阵列是否更新来判断是否取出下一个   
  gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix());   
  nextPieceArea.generateNextPiece();   
    
  //   更新等级、得分等资讯   
  Score   =   gameOpArea.getScore();   
  labScore.setText(""   +   Score);   
  LineRemoved   =   gameOpArea.getLineRemoved();   
  labLine.setText(""   +   LineRemoved);   
  Level   =   (int)   ((Score   +   100)   /   100);   //   每一百分升级一次   
  labLevel.setText(""   +   Level);   
  }   
    
  Thread.sleep((int)   interval);   
  gameOpArea.MoveDown();   //   不断下移   
  }   catch   (InterruptedException   e)   {   
  }   
  }   
  }   
    
  //   开新游戏   
  public   void   newGame()   {   
  Level   =   1;   
  Score   =   0;   
  LineRemoved   =   0;   
  labScore.setText(""   +   Score);   
  labLine.setText(""   +   LineRemoved);   
  labLevel.setText(""   +   Level);   
    
  gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix());   //   先取出一个方块   
  gameOpArea.newState();   
  nextPieceArea.generateNextPiece();   //   预览窗格产生下一个方块预览   
  }   
    
  public   static   void   main(String[]   args)   {   
  JTetrix   frm   =   new   JTetrix();   
  }   
  }   
    
  //   方块的资料结构与操作方法   
    
  class   TetrixPiece   {   
  private   int   pieceType;   //   方块样式   
    
  private   int[][]   coordinates   =   new   int[4][2];   //   四个方块,记录X与Y   
    
  public   TetrixPiece()   {   
  initialize((int)   (Math.random()   *   7   +   1));   
  }   
    
  public   TetrixPiece(int   type)   {   
  initialize(type);   
  }   
    
  //   向左转动   
  public   void   rotateLeft()   {   
  if   (pieceType   ==   5)   //   不转动正方形方块   
  return;   
    
  int   tmp;   
  for   (int   i   =   0;   i   <   4;   i++)   {   
  tmp   =   getXCoord(i);   
  setXCoord(i,   getYCoord(i));   
  setYCoord(i,   -tmp);   
  }   
  }   
    
  //   向右转动   
  public   void   rotateRight()   {   
  if   (pieceType   ==   5)   //   不转动正方形方块   
  return;   
    
  int   tmp;   
  for   (int   i   =   0;   i   <   4;   i++)   {   
  tmp   =   getXCoord(i);   
  setXCoord(i,   -getYCoord(i));   
  setYCoord(i,   tmp);   
  }   
  }
  //   取得方块样式   
  public   int   getType()   {   
  return   pieceType;   
  }   
    
  //   取得X座标   
  public   int   getXCoord(int   index)   {   
  return   coordinates[index][0];   
  }   
    
  //   取得Y座标   
  public   int   getYCoord(int   index)   {   
  return   coordinates[index][1];   
  }   
    
  //   取得方块最左边一格的座标   
  public   int   getMinX()   {   
  int   tmp   =   coordinates[0][0];   
    
  for   (int   i   =   1;   i   <   4;   i++)   
  if   (tmp   >   coordinates[i][0])   
  tmp   =   coordinates[i][0];   
    
  return   tmp;   
  }   
    
  //   取得方块最右边一格的座标   
  public   int   getMaxX()   {   
  int   tmp   =   coordinates[0][0];   
    
  for   (int   i   =   1;   i   <   4;   i++)   
  if   (tmp   <   coordinates[i][0])   
  tmp   =   coordinates[i][0];   
    
  return   tmp;   
  }   
    
  //   取得方块最上面一格的座标   
  public   int   getMinY()   {   
  int   tmp   =   coordinates[0][1];   
    
  for   (int   i   =   1;   i   <   4;   i++)   
  if   (tmp   >   coordinates[i][1])   
  tmp   =   coordinates[i][1];   
    
  return   tmp;   
  }   
    
  //   取得方块最下面一格的座标   
  public   int   getMaxY()   {   
  int   tmp   =   coordinates[0][1];   
    
  for   (int   i   =   1;   i   <   4;   i++)   
  if   (tmp   <   coordinates[i][1])   
  tmp   =   coordinates[i][1];   
    
  return   tmp;   
  }   
    
  //   设定X座标   
  public   void   setXCoord(int   index,   int   value)   {   
  coordinates[index][0]   =   value;   
  }   
    
  //   设定Y座标   
  public   void   setYCoord(int   index,   int   value)   {   
  coordinates[index][1]   =   value;   
  }   
    
  //   设定XY座标   
  public   void   setCoord(int   index,   int   x,   int   y)   {   
  coordinates[index][0]   =   x;   
  coordinates[index][1]   =   y;   
  }   
    
  //   初始方块类型   
  private   void   initialize(int   type)   {   
  //   七种方块的座标   
  final   int   pieceTypes[][][]   =   {   
  {   {   0,   -1   },   {   0,   0   },   {   -1,   0   },   {   -1,   1   }   },   
    
  {   {   0,   -1   },   {   0,   0   },   {   1,   0   },   {   1,   1   }   },   
    
  {   {   0,   -1   },   {   0,   0   },   {   0,   1   },   {   0,   2   }   },   
    
  {   {   -1,   0   },   {   0,   0   },   {   1,   0   },   {   0,   1   }   },   
    
  {   {   0,   0   },   {   1,   0   },   {   0,   1   },   {   1,   1   }   },   
    
  {   {   -1,   -1   },   {   0,   -1   },   {   0,   0   },   {   0,   1   }   },   
    
  {   {   1,   -1   },   {   0,   -1   },   {   0,   0   },   {   0,   1   }   }   };   
    
  if   (type   <   1   ||   type   >   7)   
  type   =   1;   
    
  //   依指定的类型设定方块座标   
  pieceType   =   type;   
  for   (int   i   =   0;   i   <   4;   i++)   {   
  coordinates[i][0]   =   pieceTypes[type   -   1][i][0];   
  coordinates[i][1]   =   pieceTypes[type   -   1][i][1];   
  }   
  }   
  }   
    
  //   显示下一个方块的类别   
    
  class   showNextPiece   extends   JComponent   {   
  private   int   boardWidth,   boardHeight;   //   显示的格数   
    
  private   int   gridWidth,   gridHeight;   //   每格的大小   
    
  private   TetrixPiece   nextPiece;   //   下一个方块   
    
  private   Color   pieceColor;   //   方块色彩   

⌨️ 快捷键说明

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