俄罗斯方块(Java实现)
Posted maxuewei2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了俄罗斯方块(Java实现)相关的知识,希望对你有一定的参考价值。
//Box.java
1 package tetris; 2 public class Box { 3 private final int M = 30, N = 12; 4 private int[][] ls = new int[M][N]; 5 private int score=0; 6 int getM() { 7 return M; 8 } 9 int getN() { 10 return N; 11 } 12 int getScore() { 13 return score; 14 } 15 boolean isOut(int x,int y){ 16 return x<0|y<0|x>M-1|y>N-1?true:false; 17 } 18 int getFlag(int x, int y) { 19 return isOut(x,y)?2:ls[x][y]; 20 } 21 int clearFlag(int x, int y) { 22 if(isOut(x,y))return -1; 23 ls[x][y]=0; 24 return 0; 25 } 26 int setFlag_1(int x, int y) { 27 if(isOut(x,y))return -1; 28 ls[x][y]=1; 29 return 0; 30 } 31 int setFlag_2(int x, int y) { 32 if(isOut(x,y))return -1; 33 ls[x][y]=2; 34 return 0; 35 } 36 /*boolean isFlag_2(int x, int y) { 37 return isOut(x,y)?true:ls[x][y]== 2 ? true:false ; 38 }*/ 39 void clear(int x, int maxX) { 40 if(isOut(x,0)||isOut(maxX,0))return ; 41 for (int i = x; i <= maxX; i++) { 42 boolean isAllNotBlank = true; 43 for (int j = 0; j <= N - 1; j++) { 44 if (ls[i][j]== 0) { 45 isAllNotBlank = false; 46 break; 47 } 48 } 49 if (isAllNotBlank) { 50 for (int k = i - 1; k >= 0; k--) { 51 for (int j = 0; j <= N - 1; j++) { 52 ls[k + 1][j]=ls[k][j]; 53 } 54 } 55 score+=100; 56 } 57 } 58 } 59 }
//Shape.java
1 package tetris; 2 import java.awt.Point; 3 import java.util.Random; 4 public class Shape { 5 private final Box box; 6 private Point[] ps = new Point[4]; 7 private Point[] tps = new Point[5]; 8 private final int[][][]shapes={ 9 {{-1,0,0,0,1,0,2,0},{0,0,0,-1,0,2,0,1}}, 10 {{- 1,0, 1, - 1,0,0,1,0},{0,- 1,1,-1,1,1,1,0},{-1,0,0,0,-1,1,1,0},{0,0,0,-1,0,1,1,1}}, 11 {{-1,0,0,0,1,1,1,0},{0,1,1,-1,1,1,1,0},{-1,0,-1,-1,0,0,1,0},{0,0,0,-1,0,1,1,-1}}, 12 {{0,0,1,-1,1,1,1,0},{0,0,1,-1,1,0,2,0},{0,0,0,-1,0,1,1,0},{0,0,1,0,1,1,2,0}}, 13 {{0,0,1,0,0,1,1,1}}, 14 {{-1,0,0,-1,-1,1,0,0},{-1,0,0,0,0,1,1,1}}, 15 {{- 1,0, - 1,- 1,0, 1,0,0},{- 1,1,0,0,0,1,1,0}} 16 }; 17 private int change = 0,shape,x,y; 18 int left,down; 19 private Random random; 20 boolean isPaused = false; 21 Shape(Box b) { 22 box = b; 23 for (int i = 0; i <= 3; i++) { 24 ps[i] = new Point(); 25 } 26 for (int i = 0; i <= 4; i++) { 27 tps[i] = new Point(); 28 } 29 random = new Random(); 30 getNewShape(); 31 } 32 private void clearTpsFlag(){ 33 for (int i = 0; i <= 3; i++) { 34 box.clearFlag(tps[i].x, tps[i].y); 35 } 36 } 37 private int setPsFlag_1() { 38 for (int i = 0; i <= 3; i++) { 39 box.setFlag_1(ps[i].x, ps[i].y); 40 } 41 return 0; 42 } 43 private int setPsFlag_2() { 44 for (int i = 0; i <= 3; i++) { 45 box.setFlag_2(ps[i].x, ps[i].y); 46 } 47 return 0; 48 } 49 private synchronized int tps() { 50 for (int i = 0; i <= 3; i++) { 51 tps[i].x = ps[i].x; 52 tps[i].y = ps[i].y; 53 } 54 tps[4].x=x; 55 tps[4].y=y; 56 return 0; 57 } 58 private synchronized int backupChange() { 59 for (int i = 0; i <= 3; i++) { 60 ps[i].x = tps[i].x; 61 ps[i].y = tps[i].y; 62 } 63 x=tps[4].x; 64 y=tps[4].y; 65 return 0; 66 } 67 private synchronized int check() { 68 System.out.println(ps[0]+","+ps[1]+","+ps[2]+","+ps[3]); 69 for (int i = 0; i <= 3;i++) { 70 if (!(box.getFlag(ps[i].x, ps[i].y)==0||box.getFlag(ps[i].x, ps[i].y)==1)){ 71 backupChange(); 72 return 5;} 73 } 74 return 0; 75 } 76 private void getNewShape(){ 77 System.out.println("lll"); 78 int i = Math.abs(random.nextInt()) % 7; 79 shape=i; 80 x=1; 81 y=box.getN()/2; 82 for(int j=0;j<=3;j++){ 83 ps[j].setLocation(x+shapes[i][0][j*2], y+shapes[i][0][j*2+1]); 84 } 85 left=0; 86 down=0; 87 change=0; 88 } 89 void changeShape(){ 90 tps(); 91 change++; 92 change%=shapes[shape].length; 93 for(int j=0;j<=3;j++){ 94 ps[j].setLocation(x+shapes[shape][change][j*2], y+shapes[shape][change][j*2+1]); 95 } 96 int g=check(); 97 if(g==0){ 98 clearTpsFlag(); 99 setPsFlag_1(); 100 } 101 } 102 synchronized int move(int dir) { 103 tps(); 104 switch(dir){ 105 case 0:for (int i = 0; i <= 3; i++) {ps[i].y--;}y--;break; 106 case 1:for (int i = 0; i <= 3; i++) {ps[i].y++;}y++;break; 107 case 2:for (int i = 0; i <= 3; i++) {ps[i].x++;}x++;break; 108 default: 109 } 110 int g = check(); 111 if(g!=0&&dir==2){ 112 int x = ps[0].x; 113 int maxX = ps[3].x;// x+4>M-1?M-1:x+4; 114 setPsFlag_2(); 115 box.clear(x, maxX); 116 if(x==1){return -1;} 117 getNewShape(); 118 } 119 if(g==0){ 120 clearTpsFlag(); 121 setPsFlag_1(); 122 } 123 return 0; 124 } 125 }
//Tetris.java
1 package tetris; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Container; 6 import java.awt.Graphics; 7 import java.awt.GridLayout; 8 import java.awt.event.KeyAdapter; 9 import java.awt.event.KeyEvent; 10 import java.io.BufferedReader; 11 import java.io.BufferedWriter; 12 import java.io.FileNotFoundException; 13 import java.io.FileReader; 14 import java.io.FileWriter; 15 import java.io.IOException; 16 import javax.swing.JButton; 17 import javax.swing.JFrame; 18 import javax.swing.JOptionPane; 19 import javax.swing.JPanel; 20 import javax.swing.JTextField; 21 22 public class Tetris extends JFrame implements Runnable{ 23 private static final long serialVersionUID = 279494108361487144L; 24 final Color BC = Color.GRAY; 25 final Color FC = Color.LIGHT_GRAY; 26 Box box; 27 Shape shape; 28 int a = 0, b = 0, c = 0; 29 JPanel panel, scorePanel; 30 JTextField scoreField, bestScoreField; 31 JButton[][] bs; 32 Tetris(Box b) { 33 super("Tetris"); 34 box = b; 35 shape =new Shape(b); 36 bs = new JButton[box.getM()][box.getN()]; 37 for (int i = 0; i <= box.getM() - 1; i++) { 38 for (int j = 0; j <= box.getN() - 1; j++) { 39 bs[i][j] = new JButton(); 40 bs[i][j].setBackground(BC); 41 } 42 } 43 Container container = getContentPane(); 44 container.setLayout(new BorderLayout()); 45 scorePanel = new JPanel(); 46 scorePanel.setLayout(new BorderLayout()); 47 scoreField = new JTextField(10); 48 bestScoreField = new JTextField(20); 49 bestScoreField.setText(getBestScores()); 50 bestScoreField.setEditable(false); 51 scoreField.setText("SCORE: " + new Integer(box.getScore()).toString()); 52 scoreField.setEditable(false); 53 scorePanel.add(scoreField, BorderLayout.NORTH); 54 scorePanel.add(bestScoreField, BorderLayout.SOUTH); 55 container.add(scorePanel, BorderLayout.NORTH); 56 panel = new JPanel(); 57 panel.setLayout(new GridLayout(box.getM(), box.getN())); 58 for (int i = 0; i <= box.getM() - 1; i++) { 59 for (int j = 0; j <= box.getN() - 1; j++) { 60 panel.add(bs[i][j]); 61 } 62 } 63 container.add(panel, BorderLayout.CENTER); 64 this.addKeyListener(new KeyAdapter() { 65 public void keyPressed(KeyEvent e) { 66 int c = e.getKeyCode(); 67 // System.out.print(c); 68 switch (c) { 69 case KeyEvent.VK_LEFT : 70 //shape.left++; 71 shape.move(0); 72 break; 73 case KeyEvent.VK_RIGHT : 74 //shape.left--; 75 shape.move(1); 76 break; 77 case KeyEvent.VK_UP : 78 shape.changeShape(); 79 break; 80 case KeyEvent.VK_DOWN : 81 shape.down++; 82 break; 83 case KeyEvent.VK_SPACE : 84 if (shape.isPaused == true) { 85 shape.isPaused = false; 86 } else { 87 shape.isPaused = true; 88 } 89 break; 90 default : } 91 } 92 }); 93 this.setFocusable(true); 94 setLocation(200, 10); 95 setSize(20 * box.getN(), 20 * box.getM() + 20); 96 setVisible(true); 97 } 98 private int down() throws InterruptedException { 99 System.out.println("ddd"); 100 int dd=shape.move(2); 101 scoreField.setText("Score: " + new Integer(box.getScore()).toString()); 102 if(dd==-1){gameOver();} 103 return 0; 104 } 105 public void run() { 106 while (true) { 107 try { 108 if (shape.isPaused == true) { 109 // System.out.println("PAUSED"); 110 Thread.sleep(500); 111 } else { 112 //System.out.println("start1"); 113 down(); 114 for (int i = 0; i <= box.getM() - 1; i++) { 115 for (int j = 0; j <= box.getN() - 1; j++) { 116 if(box.getFlag(i, j)==0) 117 bs[i][j].setBackground(BC); 118 else bs[i][j].setBackground(FC); 119 } 120 } 121 if (shape.down >=1) { 122 shape.down--; 123 Thread.sleep(50); 124 continue; 125 } 126 Thread.sleep(250); 127 } 128 } catch (InterruptedException e) { 129 e.printStackTrace(); 130 } 131 } 132 } 133 String getBestScores() { 134 BufferedReader reader = null; 135 try { 136 reader = new BufferedReader(new FileReader("score.txt")); 137 } catch (FileNotFoundException e) { 138 e.printStackTrace(); 139 } 140 try { 141 a = Integer.parseInt(reader.readLine()); 142 b = Integer.parseInt(reader.readLine()); 143 c = Integer.parseInt(reader.readLine()); 144 } catch (NumberFormatException e) { 145 e.printStackTrace(); 146 } catch (IOException e) { 147 e.printStackTrace(); 148 } 149 String bestS = new String("Best Score: " + new Integer(a).toString() 150 + " " + new Integer(b).toString() + " " 151 + new Integer(c).toString()); 152 return bestS; 153 } 154 int gameOver() { 155 JOptionPane.showMessageDialog(null, "你死了~~~.", "GAME OVER", 156 JOptionPane.PLAIN_MESSAGE); 157 if (box.getScore() > a) { 158 c = b; 159 b = a; 160 a = box.getScore(); 161 } 162 else if (box.getScore() > b) { 163 c = b; 164 b = box.getScore(); 165 } 166 else if (box.getScore()> c) { 167 c = box.getScore(); 168 } 169 save(); 170 System.exit(0); 171 return 0; 172 } 173 int save() { 174 BufferedWriter writer = null; 175 try { 176 writer = new BufferedWriter(new FileWriter("score.txt")); 177 } catch (IOException e) { 178 e.printStackTrace(); 179 } 180 String d = new Integer(a).toString(), e = new Integer(b).toString(), 181 f = new Integer(c).toString(); 182 try { 183 writer.write(d); 184 writer.newLine(); 185 writer.write(e); 186 writer.newLine(); 187 writer.write(f); 188 writer.flush(); 189 writer.close(); 190 } catch (NumberFormatException ev) { 191 ev.printStackTrace(); 192 } catch (IOException ev) { 193 ev.printStackTrace(); 194 } 195 return 0; 196 } 197 public static void main(String[] args) { 198 Box box = new Box(); 199 Tetris tetris = new Tetris(box); 200 tetris.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 201 Thread thread1 = new Thread(tetris); 202 thread1.start(); 203 } 204 205 }
以上是关于俄罗斯方块(Java实现)的主要内容,如果未能解决你的问题,请参考以下文章