俄罗斯方块(Java实现)

时间:2023-10-23 16:57:38

程序效果:

俄罗斯方块(Java实现)

代码:

//Box.java
1 package tetris;
public class Box {
private final int M = 30, N = 12;
private int[][] ls = new int[M][N];
private int score=0;
int getM() {
return M;
}
int getN() {
return N;
}
int getScore() {
return score;
}
boolean isOut(int x,int y){
return x<0|y<0|x>M-1|y>N-1?true:false;
}
int getFlag(int x, int y) {
return isOut(x,y)?2:ls[x][y];
}
int clearFlag(int x, int y) {
if(isOut(x,y))return -1;
ls[x][y]=0;
return 0;
}
int setFlag_1(int x, int y) {
if(isOut(x,y))return -1;
ls[x][y]=1;
return 0;
}
int setFlag_2(int x, int y) {
if(isOut(x,y))return -1;
ls[x][y]=2;
return 0;
}
/*boolean isFlag_2(int x, int y) {
return isOut(x,y)?true:ls[x][y]== 2 ? true:false ;
}*/
void clear(int x, int maxX) {
if(isOut(x,0)||isOut(maxX,0))return ;
for (int i = x; i <= maxX; i++) {
boolean isAllNotBlank = true;
for (int j = 0; j <= N - 1; j++) {
if (ls[i][j]== 0) {
isAllNotBlank = false;
break;
}
}
if (isAllNotBlank) {
for (int k = i - 1; k >= 0; k--) {
for (int j = 0; j <= N - 1; j++) {
ls[k + 1][j]=ls[k][j];
}
}
score+=100;
}
}
}
}
//Shape.java
1 package tetris;
import java.awt.Point;
import java.util.Random;
public class Shape {
private final Box box;
private Point[] ps = new Point[4];
private Point[] tps = new Point[5];
private final int[][][]shapes={
{{-1,0,0,0,1,0,2,0},{0,0,0,-1,0,2,0,1}},
{{- 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}},
{{-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}},
{{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}},
{{0,0,1,0,0,1,1,1}},
{{-1,0,0,-1,-1,1,0,0},{-1,0,0,0,0,1,1,1}},
{{- 1,0, - 1,- 1,0, 1,0,0},{- 1,1,0,0,0,1,1,0}}
};
private int change = 0,shape,x,y;
int left,down;
private Random random;
boolean isPaused = false;
Shape(Box b) {
box = b;
for (int i = 0; i <= 3; i++) {
ps[i] = new Point();
}
for (int i = 0; i <= 4; i++) {
tps[i] = new Point();
}
random = new Random();
getNewShape();
}
private void clearTpsFlag(){
for (int i = 0; i <= 3; i++) {
box.clearFlag(tps[i].x, tps[i].y);
}
}
private int setPsFlag_1() {
for (int i = 0; i <= 3; i++) {
box.setFlag_1(ps[i].x, ps[i].y);
}
return 0;
}
private int setPsFlag_2() {
for (int i = 0; i <= 3; i++) {
box.setFlag_2(ps[i].x, ps[i].y);
}
return 0;
}
private synchronized int tps() {
for (int i = 0; i <= 3; i++) {
tps[i].x = ps[i].x;
tps[i].y = ps[i].y;
}
tps[4].x=x;
tps[4].y=y;
return 0;
}
private synchronized int backupChange() {
for (int i = 0; i <= 3; i++) {
ps[i].x = tps[i].x;
ps[i].y = tps[i].y;
}
x=tps[4].x;
y=tps[4].y;
return 0;
}
private synchronized int check() {
System.out.println(ps[0]+","+ps[1]+","+ps[2]+","+ps[3]);
for (int i = 0; i <= 3;i++) {
if (!(box.getFlag(ps[i].x, ps[i].y)==0||box.getFlag(ps[i].x, ps[i].y)==1)){
backupChange();
return 5;}
}
return 0;
}
private void getNewShape(){
System.out.println("lll");
int i = Math.abs(random.nextInt()) % 7;
shape=i;
x=1;
y=box.getN()/2;
for(int j=0;j<=3;j++){
ps[j].setLocation(x+shapes[i][0][j*2], y+shapes[i][0][j*2+1]);
}
left=0;
down=0;
change=0;
}
void changeShape(){
tps();
change++;
change%=shapes[shape].length;
for(int j=0;j<=3;j++){
ps[j].setLocation(x+shapes[shape][change][j*2], y+shapes[shape][change][j*2+1]);
}
int g=check();
if(g==0){
clearTpsFlag();
setPsFlag_1();
}
}
synchronized int move(int dir) {
tps();
switch(dir){
case 0:for (int i = 0; i <= 3; i++) {ps[i].y--;}y--;break;
case 1:for (int i = 0; i <= 3; i++) {ps[i].y++;}y++;break;
case 2:for (int i = 0; i <= 3; i++) {ps[i].x++;}x++;break;
default:
}
int g = check();
if(g!=0&&dir==2){
int x = ps[0].x;
int maxX = ps[3].x;// x+4>M-1?M-1:x+4;
setPsFlag_2();
box.clear(x, maxX);
if(x==1){return -1;}
getNewShape();
}
if(g==0){
clearTpsFlag();
setPsFlag_1();
}
return 0;
}
}
//Tetris.java
1 package tetris; import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField; public class Tetris extends JFrame implements Runnable{
private static final long serialVersionUID = 279494108361487144L;
final Color BC = Color.GRAY;
final Color FC = Color.LIGHT_GRAY;
Box box;
Shape shape;
int a = 0, b = 0, c = 0;
JPanel panel, scorePanel;
JTextField scoreField, bestScoreField;
JButton[][] bs;
Tetris(Box b) {
super("Tetris");
box = b;
shape =new Shape(b);
bs = new JButton[box.getM()][box.getN()];
for (int i = 0; i <= box.getM() - 1; i++) {
for (int j = 0; j <= box.getN() - 1; j++) {
bs[i][j] = new JButton();
bs[i][j].setBackground(BC);
}
}
Container container = getContentPane();
container.setLayout(new BorderLayout());
scorePanel = new JPanel();
scorePanel.setLayout(new BorderLayout());
scoreField = new JTextField(10);
bestScoreField = new JTextField(20);
bestScoreField.setText(getBestScores());
bestScoreField.setEditable(false);
scoreField.setText("SCORE: " + new Integer(box.getScore()).toString());
scoreField.setEditable(false);
scorePanel.add(scoreField, BorderLayout.NORTH);
scorePanel.add(bestScoreField, BorderLayout.SOUTH);
container.add(scorePanel, BorderLayout.NORTH);
panel = new JPanel();
panel.setLayout(new GridLayout(box.getM(), box.getN()));
for (int i = 0; i <= box.getM() - 1; i++) {
for (int j = 0; j <= box.getN() - 1; j++) {
panel.add(bs[i][j]);
}
}
container.add(panel, BorderLayout.CENTER);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
// System.out.print(c);
switch (c) {
case KeyEvent.VK_LEFT :
//shape.left++;
shape.move(0);
break;
case KeyEvent.VK_RIGHT :
//shape.left--;
shape.move(1);
break;
case KeyEvent.VK_UP :
shape.changeShape();
break;
case KeyEvent.VK_DOWN :
shape.down++;
break;
case KeyEvent.VK_SPACE :
if (shape.isPaused == true) {
shape.isPaused = false;
} else {
shape.isPaused = true;
}
break;
default : }
}
});
this.setFocusable(true);
setLocation(200, 10);
setSize(20 * box.getN(), 20 * box.getM() + 20);
setVisible(true);
}
private int down() throws InterruptedException {
System.out.println("ddd");
int dd=shape.move(2);
scoreField.setText("Score: " + new Integer(box.getScore()).toString());
if(dd==-1){gameOver();}
return 0;
}
public void run() {
while (true) {
try {
if (shape.isPaused == true) {
// System.out.println("PAUSED");
Thread.sleep(500);
} else {
//System.out.println("start1");
down();
for (int i = 0; i <= box.getM() - 1; i++) {
for (int j = 0; j <= box.getN() - 1; j++) {
if(box.getFlag(i, j)==0)
bs[i][j].setBackground(BC);
else bs[i][j].setBackground(FC);
}
}
if (shape.down >=1) {
shape.down--;
Thread.sleep(50);
continue;
}
Thread.sleep(250);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
String getBestScores() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("score.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
a = Integer.parseInt(reader.readLine());
b = Integer.parseInt(reader.readLine());
c = Integer.parseInt(reader.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String bestS = new String("Best Score: " + new Integer(a).toString()
+ " " + new Integer(b).toString() + " "
+ new Integer(c).toString());
return bestS;
}
int gameOver() {
JOptionPane.showMessageDialog(null, "你死了~~~.", "GAME OVER",
JOptionPane.PLAIN_MESSAGE);
if (box.getScore() > a) {
c = b;
b = a;
a = box.getScore();
}
else if (box.getScore() > b) {
c = b;
b = box.getScore();
}
else if (box.getScore()> c) {
c = box.getScore();
}
save();
System.exit(0);
return 0;
}
int save() {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("score.txt"));
} catch (IOException e) {
e.printStackTrace();
}
String d = new Integer(a).toString(), e = new Integer(b).toString(),
f = new Integer(c).toString();
try {
writer.write(d);
writer.newLine();
writer.write(e);
writer.newLine();
writer.write(f);
writer.flush();
writer.close();
} catch (NumberFormatException ev) {
ev.printStackTrace();
} catch (IOException ev) {
ev.printStackTrace();
}
return 0;
}
public static void main(String[] args) {
Box box = new Box();
Tetris tetris = new Tetris(box);
tetris.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread thread1 = new Thread(tetris);
thread1.start();
} }

程序写于大三上学期。

2016.4.12更新博客。

END