- 论坛徽章:
- 0
|
- // 试用鼠标拖动方块, 在同一行放开 (只对已经落下的)
- // javac TettyBlocks.java
- // java TettyBlocks
- // 由于时间不够, 写的很粗糙, 大家多多指点
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.geom.*;
- import java.util.*;
- import javax.swing.*;
- public class TettyBlocks {
- public static void main(String[] args) {
- JFrame frame = new BoardFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.show();
- }
- }
- class BoardFrame extends JFrame {
- private Board board;
- private RepaintThread thread;
- private JTextField scoreField = new JTextField(6);
- private JButton pauseResume = new JButton("Pause");
- private JButton play = new JButton("Play");
- private JButton newGame = new JButton("New Game");
- private JButton help = new JButton("Help");
- private JLabel score = new JLabel("Score");
- private JButton speedUp = new JButton("Speed Up");
- private JTextField levelField = new JTextField(2);
- public BoardFrame() {
- setTitle("Tetty Blocks Game-----by M_ZH");
- setSize(500, 600);
- setResizable(false);
- setVisible(true);
- board = new Board(20, 6, scoreField);
- thread = new RepaintThread(board);
- Container contentpane = getContentPane();
- contentpane.add(board, BorderLayout.CENTER);
- JPanel buttonPanel = new JPanel();
- contentpane.add(buttonPanel, BorderLayout.SOUTH);
- scoreField.setText("0");
- scoreField.setEditable(false);
-
- JPanel messagePanel = new JPanel();
- messagePanel.setSize(100, 300);
- messagePanel.setLayout(new FlowLayout());
- messagePanel.add(score);
- messagePanel.add(scoreField);
- board.levelField(levelField);
- levelField.setText("0");
- levelField.setEditable(false);
- messagePanel.add(levelField);
- messagePanel.add(new JLabel("level"), BorderLayout.SOUTH);
-
- contentpane.add(messagePanel, BorderLayout.EAST);
- newGame.setEnabled(false);
- pauseResume.setEnabled(false);
-
- addButton(buttonPanel, play,
- new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- thread.start();
- play.setEnabled(false);
- newGame.setEnabled(true);
- pauseResume.setEnabled(true);
- }
- });
-
-
- addButton(buttonPanel, newGame,
- new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- if(pauseResume.getText()=="Resume"||board.gameOver()) {
- board.restartBoard();
- synchronized(thread) {
- thread.notify();
- }
- pauseResume.setText("Pause");
- }
- board.newGame();
- }
- });
-
- addButton(buttonPanel, pauseResume,
- new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- if(evt.getActionCommand()=="Pause") {
- board.pauseBoard();
- pauseResume.setText("Resume");
- }
- else if(evt.getActionCommand()=="Resume") {
- board.restartBoard();
- synchronized(thread) {
- thread.notify();
- }
- pauseResume.setText("Pause");
- }
- }
- });
-
- addButton(buttonPanel, help,
- new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- String s = "Try: Using mouse drag blocks\n "+
- "then release in the same row.";
- JOptionPane.showMessageDialog(null, s);
- }
- });
-
- addButton(buttonPanel, speedUp,
- new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- board.upSpeed();
- }
- });
- }
- public void addButton(Container c, JButton b,
- ActionListener listener) {
- c.add(b);
- b.addActionListener(listener);
- }
- }
- class RepaintThread extends Thread {
- private Board board;
- private boolean paused = false;
-
- public RepaintThread(Board b) { board = b; }
- public synchronized void run() {
- try {
- while(true) {
- if(board.getCount() >; board.getAcount()) {
- board.generateBlocks();
- board.setCount(0);
- }
- board.increaseCount();
- board.repaint();
- sleep(board.getSpeed());
-
- if (board.isPause()) {
- wait();
- }
-
- }
- } catch(InterruptedException e) {}
- }
- }
- class Board extends JPanel implements MouseListener {
- private int[][] board;
- private int row, col;
- private JTextField scoreField;
- private int width=300, height=500;
- private int blockWidth, blockHeight;
- private ArrayList blocks = new ArrayList();
- private Color[] colors = { Color.black, Color.red, Color.yellow,
- Color.blue, Color.cyan, Color.magenta, Color.green };
- private int score = 0;
- private boolean paused = false;
- private boolean gameover = false;
- private int level = 0;
- private JTextField levelField;
- private int aCount = 200;
- private int count = 200;
- private int speed = 25;
-
- public void levelField(JTextField l) { levelField = l; }
-
- public void setCount(int n) { count = n; }
- public void increaseCount() { count++; }
- public int getCount() { return count; }
- public int getSpeed() { return speed; }
- public void upSpeed() {
- if(speed >;= 1)
- speed -= 1;
- if(aCount >; 50)
- aCount -= 5;
- }
-
- public boolean gameOver() { return gameover; }
- public int getAcount() { return aCount; }
-
- public synchronized void pauseBoard() {
- paused = true;
- }
-
- public synchronized void restartBoard() {
- paused = false;
- }
-
- public synchronized boolean isPause() {
- return paused;
- }
-
- public Board(int r, int c, JTextField sc) {
- row = r;
- col = c;
- blockWidth = (int) (width/c);
- blockHeight = (int) (height/r);
- board = new int[r][c];
- addMouseListener(this);
- scoreField = sc;
- }
- public void add(Block b) { blocks.add(b); }
- public void generateBlocks() {
- int ran = (int) (Math.random()*4) + 1;
- add(new Block(ran-1, 0, random()));
- add(new Block(ran, 0, random()));
- add(new Block(ran+1, 0, random()));
- }
- // a random number between 1 and 6
- public int random() {
- return (int)(Math.random()*6)+1;
- }
-
- public void setRegard() {
- for(int i=0; i<blocks.size(); i++) {
- Block b = (Block) blocks.get(i);
- int x = b.getX();
- int y = b.getY();
- int j = y/blockHeight + 1;
- if(j==1&&board[j][x]!=0) {
- gameover = true;
- pauseBoard();
- }
- for(; j<row&&board[j][x]==0; j++);
- b.setRegard((int)(j*blockHeight));
- }
- }
- public boolean canMove(Block b) {
- return b.getY()+blockHeight < b.getRegard();
- }
-
- public void paint(Graphics g) {
- g.setColor(Color.black);
- g.fillRect(0, 0, width, height);
- setRegard();
- for(int i=0; i<blocks.size(); i++) {
-
- Block b = (Block) blocks.get(i);
- g.setColor(colors[b.getColorNum()]);
- int xx = b.getY();
- int yy = blockHeight;
- int zz = b.getRegard();
- if(b.getY()+blockHeight >; b.getRegard())
- b.setY(b.getRegard() - blockHeight);
- g.fillRect(b.getX()*blockWidth+1, b.getY()+1, blockWidth-1, blockHeight-1);
-
- if(canMove(b)) {
- board[b.getY()/blockHeight][b.getX()] = 0;
- b.move();
- }
- else {
- board[(int)(b.getY()/blockHeight)][b.getX()] = b.getColorNum();
- deleteSuccess();
- }
- }
- } //end paint
-
- public int findBlock(int r, int c) {
- int yval = r*blockHeight;
- for(int i=0; i<blocks.size(); i++) {
- Block b = (Block) blocks.get(i);
- if(b.getX()==c && Math.abs(yval - b.getY())<3)
- return i;
- }
- return -1;
- }
- private int tempR, tempC;
- public void mousePressed(MouseEvent e) {
- tempC = e.getX()/blockWidth;
- tempR = e.getY()/blockHeight;
- }
- public void mouseReleased(MouseEvent e) {
- int i = findBlock(tempR, tempC);
- if(i == -1)
- return;
- Block b = (Block) blocks.get(i);
-
- if(e.getX() >;= width || e.getX() <=0 ) return;
- if(e.getY() >;= height || e.getY() <=0 ) return;
-
- int c = e.getX()/blockWidth;
- int r = e.getY()/blockHeight;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- if(isLegalSwap(tempR, tempC, r, c)) {
- int j = findBlock(r, c);
- if(j==-1) {
- b.setX(c);
- b.setY(r*blockHeight);
- board[tempR][tempC] = 0;
- return;
- }
- Block b2 = (Block) blocks.get(j);
- int colorN = b.getColorNum();
- b.setColorNum(b2.getColorNum());
- b2.setColorNum(colorN);
-
- int temp = board[r][c];
- board[r][c] = board[tempR][tempC];
- board[tempR][tempC] = temp;
-
- }
- } //end mouse released
- public boolean isLegalSwap(int r1, int c1, int r2, int c2) {
- if(board[r1][c1]==0) return false;
- if(r1!=r2) return false;
- if(board[r2][c2]!=0) return true;
-
- if(c2 >; c1) {
- int i = c1 + 1;
- for(; i < c2 && r1!=19; i++)
- if(board[r1+1][i]==0)
- return false;
- }
- else if(c2 < c1) {
- int i = c1 - 1;
- for(; i >; c2 && r1!=19; i--)
- if(board[r1+1][i]==0)
- return false;
- }
-
- return true;
- }
- public void mouseClicked(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
-
- public void deleteSuccess() {
- int count = 0, val;
- int[] records = new int[3000];
- int size = 0;
- // row check
- for(int r = 0; r < row; r++)
- for(int c = 0; c < col-1; c++)
- if(board[r][c]!=0 && board[r][c]==board[r][c+1]) {
- records[size++] = r;
- records[size++] = c;
- count++;
- if(c == col-2 && count>;=2) {
- records[size++] = r;
- records[size++] = c+1;
- count = 0;
- }
- if(c == col-2 && count == 1) {
- count = 0;
- size -= 2;
- }
- }
- else if(count >;= 2) {
- records[size++] = r;
- records[size++] = c;
- count = 0;
- }
- else {
- size -= 2*count;
- count = 0;
- }
- // col check
- for(int c = 0; c < col; c++)
- for(int r = 0; r < row-1; r++)
- if(board[r][c]!=0 && board[r][c]==board[r+1][c]) {
- records[size++] = r;
- records[size++] = c;
- count++;
- if(r == row-2 && count>;=2) {
- records[size++] = r+1;
- records[size++] = c;
- count = 0;
- }
- if(r == row-2 && count == 1) {
- count = 0;
- size -= 2;
- }
- }
- else if(count >;= 2) {
- records[size++] = r;
- records[size++] = c;
- count = 0;
- }
- else {
- size -= 2*count;
- count = 0;
- }
-
- for(int i=0; i<size; ) {
- int r = records[i];
- int c = records[i+1];
- board[r][c] = 0;
- int index = findBlock(r, c);
- if(index!=-1)
- blocks.remove(index);
- i += 2;
- }
-
- if(size >;= 6) {
- score += calculateScore(size/2-2);
- scoreField.setText(""+score);
- int l = score/100;
- if(l >; level) {
- for(int i=level; i<l; i++)
- upSpeed();
- level = l;
- levelField.setText(""+level);
- }
- }
- size = 0;
- } // end delete Success
-
- public int calculateScore(int k) {
- if(k < 1) return 0;
-
- int result = 2;
- for(int i=1; i<k; i++)
- result *= 2;
- return result + 10;
- } // end calculate score
-
- public void newGame() {
- blocks.clear();
- for(int i=0; i<row; i++)
- for(int j=0; j<col; j++)
- board[i][j] = 0;
- score = 0;
- scoreField.setText("0");
- level = 0;
- levelField.setText("0");
- speed = 25;
- count = 200;
- aCount = 200;
- gameover = false;
- } // end n
-
- } //end board class
- class Block {
- private int x, y;
- private int colorNum;
- private int regard = 500; //~~~~~~~~~~~~~~~~~
- public Block(int x, int y , int c) {
- this.x = x;
- this.y = y;
- colorNum = c;
- }
- public void move() { y += 2; }
- public int getX() { return x; }
- public int getY() { return y; }
- public int getColorNum() { return colorNum; }
- public int getRegard() { return regard; }
- public void setColorNum(int n) { colorNum = n; }
- public void setRegard(int r) { regard = r; }
- public void setX(int val) { x = val; }
- public void setY(int val) { y = val; }
- }
复制代码 |
|