俄罗斯方块java代码_俄罗斯方块(Java实现)

俄罗斯方块java代码_俄罗斯方块(Java实现)//Tetris.java1packagetetris;23importjava.awt.BorderLayout;4importjava.awt.Color;5importjava.awt.Container;6importjava.awt.Graphics;7importjava.awt.GridLayout;8importjava.awt.event.KeyAdapter;9…

//Tetris.java

1 packagetetris;2

3 importjava.awt.BorderLayout;4 importjava.awt.Color;5 importjava.awt.Container;6 importjava.awt.Graphics;7 importjava.awt.GridLayout;8 importjava.awt.event.KeyAdapter;9 importjava.awt.event.KeyEvent;10 importjava.io.BufferedReader;11 importjava.io.BufferedWriter;12 importjava.io.FileNotFoundException;13 importjava.io.FileReader;14 importjava.io.FileWriter;15 importjava.io.IOException;16 importjavax.swing.JButton;17 importjavax.swing.JFrame;18 importjavax.swing.JOptionPane;19 importjavax.swing.JPanel;20 importjavax.swing.JTextField;21

22 public class Tetris extends JFrame implementsRunnable{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 =newShape(b);36 bs = newJButton[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] = newJButton();40 bs[i][j].setBackground(BC);41 }42 }43 Container container =getContentPane();44 container.setLayout(newBorderLayout());45 scorePanel = newJPanel();46 scorePanel.setLayout(newBorderLayout());47 scoreField = new JTextField(10);48 bestScoreField = new JTextField(20);49 bestScoreField.setText(getBestScores());50 bestScoreField.setEditable(false);51 scoreField.setText(“SCORE: ” + newInteger(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 = newJPanel();57 panel.setLayout(newGridLayout(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(newKeyAdapter() {65 public voidkeyPressed(KeyEvent e) {66 int c =e.getKeyCode();67 //System.out.print(c);

68 switch(c) {69 caseKeyEvent.VK_LEFT :70 //shape.left++;

71 shape.move(0);72 break;73 caseKeyEvent.VK_RIGHT :74 //shape.left–;

75 shape.move(1);76 break;77 caseKeyEvent.VK_UP :78 shape.changeShape();79 break;80 caseKeyEvent.VK_DOWN :81 shape.down++;82 break;83 caseKeyEvent.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() throwsInterruptedException {99 System.out.println(“ddd”);100 int dd=shape.move(2);101 scoreField.setText(“Score: ” + newInteger(box.getScore()).toString());102 if(dd==-1){gameOver();}103 return 0;104 }105 public voidrun() {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 elsebs[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: ” + newInteger(a).toString()150 + ” ” + new Integer(b).toString() + ” “

151 + newInteger(c).toString());152 returnbestS;153 }154 intgameOver() {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 intsave() {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 = newInteger(b).toString(),181 f = newInteger(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 voidmain(String[] args) {198 Box box = newBox();199 Tetris tetris = newTetris(box);200 tetris.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);201 Thread thread1 = newThread(tetris);202 thread1.start();203 }204

205 }

今天的文章俄罗斯方块java代码_俄罗斯方块(Java实现)分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/25872.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注