免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5339 | 回复: 5
打印 上一主题 下一主题

小弟写的一个类似俄罗斯的游戏, 多多指点 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-06-16 21:02 |只看该作者 |倒序浏览
  1. // 试用鼠标拖动方块, 在同一行放开 (只对已经落下的)
  2. // javac TettyBlocks.java
  3. // java TettyBlocks
  4. // 由于时间不够, 写的很粗糙, 大家多多指点

  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.awt.geom.*;
  8. import java.util.*;
  9. import javax.swing.*;

  10. public class TettyBlocks {
  11.     public static void main(String[] args) {
  12.          JFrame frame = new BoardFrame();
  13.          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.          frame.show();
  15.     }
  16. }

  17. class BoardFrame extends JFrame {
  18.         private Board board;
  19.         private RepaintThread thread;
  20.         private JTextField scoreField = new JTextField(6);
  21.         private JButton pauseResume = new JButton("Pause");
  22.         private JButton play = new JButton("Play");
  23.         private JButton newGame = new JButton("New Game");
  24.         private JButton help = new JButton("Help");
  25.         private JLabel score = new JLabel("Score");
  26.         private JButton speedUp = new JButton("Speed Up");
  27.         private JTextField levelField = new JTextField(2);

  28.         public BoardFrame() {
  29.                 setTitle("Tetty Blocks Game-----by M_ZH");
  30.                 setSize(500, 600);
  31.                 setResizable(false);
  32.                 setVisible(true);
  33.                 board = new Board(20, 6, scoreField);
  34.                 thread = new RepaintThread(board);
  35.                 Container contentpane = getContentPane();
  36.                 contentpane.add(board, BorderLayout.CENTER);   
  37.                 JPanel buttonPanel = new JPanel();
  38.                 contentpane.add(buttonPanel, BorderLayout.SOUTH);
  39.                 scoreField.setText("0");
  40.                 scoreField.setEditable(false);
  41.                
  42.                 JPanel messagePanel = new JPanel();
  43.                 messagePanel.setSize(100, 300);
  44.                 messagePanel.setLayout(new FlowLayout());
  45.                 messagePanel.add(score);
  46.                 messagePanel.add(scoreField);
  47.                 board.levelField(levelField);
  48.                 levelField.setText("0");
  49.                 levelField.setEditable(false);
  50.                 messagePanel.add(levelField);
  51.                 messagePanel.add(new JLabel("level"), BorderLayout.SOUTH);
  52.                
  53.                 contentpane.add(messagePanel, BorderLayout.EAST);
  54.                 newGame.setEnabled(false);
  55.                 pauseResume.setEnabled(false);
  56.                
  57.                 addButton(buttonPanel, play,
  58.                 new ActionListener() {  
  59.                    public void actionPerformed(ActionEvent evt) {
  60.                         thread.start();       
  61.                         play.setEnabled(false);
  62.                         newGame.setEnabled(true);
  63.                         pauseResume.setEnabled(true);
  64.                    }               
  65.                 });       
  66.                
  67.                
  68.                 addButton(buttonPanel, newGame,
  69.                 new ActionListener() {  
  70.                    public void actionPerformed(ActionEvent evt) {
  71.                         if(pauseResume.getText()=="Resume"||board.gameOver()) {
  72.                             board.restartBoard();
  73.                             synchronized(thread) {
  74.                                 thread.notify();
  75.                             }
  76.                             pauseResume.setText("Pause");
  77.                         }
  78.                         board.newGame();
  79.                    }               
  80.                 });       
  81.                  
  82.                 addButton(buttonPanel, pauseResume,
  83.                 new ActionListener() {  
  84.                    public void actionPerformed(ActionEvent evt) {
  85.                        if(evt.getActionCommand()=="Pause") {
  86.                            board.pauseBoard();
  87.                            pauseResume.setText("Resume");
  88.                        }
  89.                        else if(evt.getActionCommand()=="Resume") {
  90.                            board.restartBoard();
  91.                            synchronized(thread) {
  92.                                thread.notify();
  93.                            }
  94.                            pauseResume.setText("Pause");
  95.                        }                        
  96.                    }               
  97.                 });
  98.                
  99.                 addButton(buttonPanel, help,
  100.                 new ActionListener() {  
  101.                    public void actionPerformed(ActionEvent evt) {
  102.                        String s = "Try: Using mouse drag blocks\n "+
  103.                                                        "then release in the same row.";
  104.                        JOptionPane.showMessageDialog(null, s);
  105.                    }               
  106.                 });
  107.                
  108.                 addButton(buttonPanel, speedUp,
  109.                 new ActionListener() {  
  110.                    public void actionPerformed(ActionEvent evt) {
  111.                         board.upSpeed();
  112.                    }               
  113.                 });       
  114.         }

  115.    public void addButton(Container c, JButton b,
  116.        ActionListener listener) {
  117.        c.add(b);
  118.        b.addActionListener(listener);
  119.    }
  120. }


  121. class RepaintThread extends Thread {
  122.         private Board board;  
  123.         private boolean paused = false;
  124.          
  125.         public RepaintThread(Board b) { board = b; }
  126.         public synchronized void run() {
  127.           try {
  128.                 while(true) {               
  129.                         if(board.getCount() >; board.getAcount()) {
  130.                             board.generateBlocks();
  131.                             board.setCount(0);
  132.                         }   
  133.                         board.increaseCount();
  134.                         board.repaint();
  135.                         sleep(board.getSpeed());
  136.                        
  137.                         if (board.isPause()) {
  138.                             wait();
  139.                         }
  140.                        
  141.                 }
  142.           } catch(InterruptedException e) {}
  143.         }
  144. }

  145. class Board extends JPanel implements MouseListener {
  146.         private int[][] board;
  147.         private int row, col;
  148.         private JTextField scoreField;
  149.         private int width=300, height=500;
  150.         private int blockWidth, blockHeight;
  151.         private ArrayList blocks = new ArrayList();
  152.         private Color[] colors = { Color.black, Color.red, Color.yellow,
  153.                         Color.blue, Color.cyan, Color.magenta, Color.green };
  154.         private int score = 0;               
  155.         private boolean paused = false;
  156.         private boolean gameover = false;
  157.         private int level = 0;
  158.         private JTextField levelField;
  159.         private int aCount = 200;
  160.         private int count = 200;
  161.         private int speed = 25;
  162.        
  163.         public void levelField(JTextField l) { levelField = l; }

  164.        
  165.         public void setCount(int n) { count = n; }
  166.         public void increaseCount() { count++; }
  167.         public int getCount() { return count; }
  168.         public int getSpeed() { return speed; }
  169.         public void upSpeed() {
  170.             if(speed >;= 1)
  171.                 speed -= 1;
  172.             if(aCount >; 50)
  173.                 aCount -= 5;
  174.         }
  175.        
  176.         public boolean gameOver() { return gameover; }       
  177.         public int getAcount() { return aCount; }
  178.        
  179.         public synchronized void pauseBoard() {
  180.             paused = true;
  181.         }
  182.        
  183.         public synchronized void restartBoard() {
  184.             paused = false;
  185.         }
  186.        
  187.         public synchronized boolean isPause() {
  188.             return paused;
  189.         }
  190.        
  191.         public Board(int r, int c, JTextField sc) {
  192.                 row = r;
  193.                 col = c;
  194.                 blockWidth = (int) (width/c);
  195.                 blockHeight = (int) (height/r);
  196.                 board = new int[r][c];
  197.                 addMouseListener(this);
  198.                 scoreField = sc;
  199.         }

  200.         public void add(Block b) { blocks.add(b); }

  201.         public void generateBlocks() {
  202.            int ran = (int) (Math.random()*4) + 1;
  203.            add(new Block(ran-1, 0, random()));
  204.            add(new Block(ran, 0, random()));
  205.            add(new Block(ran+1, 0, random()));
  206.         }
  207.            // a random number between 1 and 6
  208.         public int random() {
  209.            return (int)(Math.random()*6)+1;
  210.         }
  211.        
  212.         public void setRegard() {
  213.                 for(int i=0; i<blocks.size(); i++) {
  214.                         Block b = (Block) blocks.get(i);
  215.                         int x = b.getX();
  216.                         int y = b.getY();
  217.                         int j = y/blockHeight + 1;
  218.                         if(j==1&&board[j][x]!=0) {
  219.                             gameover = true;                   
  220.                             pauseBoard();
  221.                         }
  222.                         for(; j<row&&board[j][x]==0; j++);
  223.                             b.setRegard((int)(j*blockHeight));
  224.                 }
  225.         }
  226. public boolean canMove(Block b) {
  227.                 return b.getY()+blockHeight < b.getRegard();               
  228.         }
  229.         
  230.         public void paint(Graphics g) {
  231.                 g.setColor(Color.black);
  232.                 g.fillRect(0, 0, width, height);
  233.                 setRegard();
  234.                 for(int i=0; i<blocks.size(); i++) {
  235.                        
  236.                         Block b = (Block) blocks.get(i);
  237.                         g.setColor(colors[b.getColorNum()]);
  238.                         int xx = b.getY();
  239.                         int yy = blockHeight;
  240.                         int zz = b.getRegard();
  241.                         if(b.getY()+blockHeight >; b.getRegard())
  242.                                 b.setY(b.getRegard() - blockHeight);
  243.                         g.fillRect(b.getX()*blockWidth+1, b.getY()+1, blockWidth-1, blockHeight-1);
  244.                        
  245.                         if(canMove(b)) {
  246.                             board[b.getY()/blockHeight][b.getX()] = 0;
  247.                             b.move();
  248.                         }
  249.                         else {
  250.                                 board[(int)(b.getY()/blockHeight)][b.getX()] = b.getColorNum();
  251.                                 deleteSuccess();
  252.                         }
  253.                 }
  254.         }  //end paint

  255.             
  256.                 public int findBlock(int r, int c) {
  257.                               int yval = r*blockHeight;
  258.                         for(int i=0; i<blocks.size(); i++) {
  259.                                 Block b = (Block) blocks.get(i);
  260.                                 if(b.getX()==c && Math.abs(yval - b.getY())<3)
  261.                                     return i;
  262.                         }
  263.                         return -1;
  264.                 }


  265.                 private int tempR, tempC;
  266.                 public void mousePressed(MouseEvent e) {
  267.                 tempC = e.getX()/blockWidth;
  268.                 tempR = e.getY()/blockHeight;
  269.         }

  270.        public void mouseReleased(MouseEvent e) {     
  271.                 int i = findBlock(tempR, tempC);
  272.                 if(i == -1)
  273.                     return;
  274.                 Block b = (Block) blocks.get(i);
  275.                                
  276.                 if(e.getX() >;= width || e.getX() <=0 ) return;
  277.                 if(e.getY() >;= height || e.getY() <=0 ) return;
  278.                
  279.                 int c = e.getX()/blockWidth;
  280.                 int r = e.getY()/blockHeight;

  281.         //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~          
  282.                   if(isLegalSwap(tempR, tempC, r, c)) {
  283.                                 int j = findBlock(r, c);
  284.                                 if(j==-1) {
  285.                                         b.setX(c);
  286.                                         b.setY(r*blockHeight);
  287.                                         board[tempR][tempC] = 0;
  288.                                         return;
  289.                                 }

  290.                          Block b2 = (Block) blocks.get(j);                       
  291.                                 int colorN = b.getColorNum();
  292.                                 b.setColorNum(b2.getColorNum());
  293.                                 b2.setColorNum(colorN);
  294.                
  295.                     int temp = board[r][c];
  296.                     board[r][c] = board[tempR][tempC];
  297.                     board[tempR][tempC] = temp;
  298.    
  299.            }
  300.         }    //end mouse released

  301.          public boolean isLegalSwap(int r1, int c1, int r2, int c2) {
  302.              if(board[r1][c1]==0) return false;
  303.              if(r1!=r2) return false;
  304.              if(board[r2][c2]!=0) return true;
  305.    
  306.              if(c2 >; c1) {
  307.                  int i = c1 + 1;
  308.                  for(; i < c2 && r1!=19; i++)
  309.                      if(board[r1+1][i]==0)
  310.                          return false;
  311.              }
  312.              else if(c2 < c1) {
  313.                  int i = c1 - 1;
  314.                  for(; i >; c2 && r1!=19; i--)
  315.                      if(board[r1+1][i]==0)
  316.                          return false;
  317.              }
  318.              
  319.              return true;
  320.          }

  321.         public void mouseClicked(MouseEvent e) {}
  322.         public void mouseEntered(MouseEvent e) {}
  323.         public void mouseExited(MouseEvent e) {}
  324.   
  325.         public void deleteSuccess() {
  326.                  int count = 0, val;
  327.                  int[] records = new int[3000];
  328.                  int size = 0;

  329.          // row check
  330.                  for(int r = 0; r < row; r++)
  331.                      for(int c = 0; c < col-1; c++)
  332.                 if(board[r][c]!=0 && board[r][c]==board[r][c+1]) {
  333.                                  records[size++] = r;
  334.                                  records[size++] = c;
  335.                              count++;
  336.                                          if(c == col-2 && count>;=2) {
  337.                                                  records[size++] = r;
  338.                                      records[size++] = c+1;
  339.                                  count = 0;
  340.                                          }
  341.                                          if(c == col-2 && count == 1) {
  342.                                                  count = 0;
  343.                                                  size -= 2;
  344.                                          }
  345.                         }
  346.                             else if(count >;= 2) {
  347.                              records[size++] = r;
  348.                                  records[size++] = c;
  349.                                  count = 0;
  350.                         }
  351.                             else {
  352.                                  size -= 2*count;
  353.                                  count = 0;
  354.                             }
  355.          // col check
  356.                  for(int c = 0; c < col; c++)
  357.                      for(int r = 0; r < row-1; r++)
  358.                 if(board[r][c]!=0 && board[r][c]==board[r+1][c]) {
  359.                                  records[size++] = r;
  360.                                  records[size++] = c;
  361.                              count++;
  362.                                          if(r == row-2 && count>;=2) {
  363.                                                  records[size++] = r+1;
  364.                                      records[size++] = c;
  365.                                  count = 0;
  366.                                          }
  367.                                          if(r == row-2 && count == 1) {
  368.                                                  count = 0;
  369.                                                  size -= 2;
  370.                                          }
  371.                         }
  372.                             else if(count >;= 2) {
  373.                              records[size++] = r;
  374.                                  records[size++] = c;
  375.                                  count = 0;
  376.                         }
  377.                             else {
  378.                                  size -= 2*count;
  379.                                  count = 0;
  380.                             }
  381.         
  382.                 for(int i=0; i<size; ) {
  383.                         int r = records[i];
  384.                         int c = records[i+1];
  385.                         board[r][c] = 0;
  386.                         int index = findBlock(r, c);
  387.                         if(index!=-1)
  388.                             blocks.remove(index);
  389.                         i += 2;
  390.                 }
  391.                
  392.                 if(size >;= 6) {
  393.                     score += calculateScore(size/2-2);
  394.                     scoreField.setText(""+score);
  395.                     int l = score/100;
  396.                     if(l >; level) {
  397.                         for(int i=level; i<l; i++)
  398.                                 upSpeed();
  399.                 level = l;
  400.                                 levelField.setText(""+level);
  401.                         }
  402.                 }
  403.                 size = 0;
  404.         }  // end delete Success
  405.        
  406.         public int calculateScore(int k) {
  407.             if(k < 1) return 0;
  408.             
  409.             int result = 2;
  410.             for(int i=1; i<k; i++)
  411.                 result *= 2;
  412.             return result + 10;            
  413.         }     // end calculate score   
  414.        
  415.         public void newGame() {
  416.             blocks.clear();
  417.             for(int i=0; i<row; i++)
  418.                 for(int j=0; j<col; j++)
  419.                     board[i][j] = 0;
  420.             score = 0;
  421.             scoreField.setText("0");
  422.                 level = 0;
  423.                 levelField.setText("0");
  424.             speed = 25;
  425.             count = 200;
  426.             aCount = 200;  
  427.             gameover = false;
  428.         }  // end n
  429.           
  430. }  //end board class


  431. class Block {
  432.         private int x, y;
  433.         private int colorNum;
  434.         private int regard = 500;  //~~~~~~~~~~~~~~~~~

  435.         public Block(int x, int y , int c) {
  436.                 this.x = x;
  437.                 this.y = y;
  438.                 colorNum = c;
  439.         }

  440.         public void move() { y += 2; }
  441.         public int getX() { return x; }
  442.         public int getY() { return y; }
  443.         public int getColorNum() { return colorNum; }
  444.         public int getRegard() { return regard; }
  445.         public void setColorNum(int n) { colorNum = n; }
  446.         public void setRegard(int r) { regard = r; }
  447.         public void setX(int val) { x = val; }
  448.         public void setY(int val) { y = val; }
  449. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2004-06-18 10:31 |只看该作者

小弟写的一个类似俄罗斯的游戏, 多多指点

henbucuo

论坛徽章:
0
3 [报告]
发表于 2004-06-20 18:03 |只看该作者

小弟写的一个类似俄罗斯的游戏, 多多指点

看帖不回帖啊:(

论坛徽章:
0
4 [报告]
发表于 2004-06-20 20:42 |只看该作者

小弟写的一个类似俄罗斯的游戏, 多多指点

羡慕啊,我们这些菜鸟还在挣扎中

论坛徽章:
0
5 [报告]
发表于 2004-06-21 01:33 |只看该作者

小弟写的一个类似俄罗斯的游戏, 多多指点

试了一下,还不太会玩

论坛徽章:
0
6 [报告]
发表于 2005-02-01 21:42 |只看该作者

小弟写的一个类似俄罗斯的游戏, 多多指点

太长也.不敢试哦.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP