程序效果:

代码:

//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

俄罗斯方块(Java实现)的更多相关文章

  1. 201604-2 俄罗斯方块 Java

    大家谁能帮我看看是哪里不对,提交到系统中是0分,在Eclipse中可以得出例子中的结果 思路: 题目中有两个关键点:如何模拟下落的过程,如何判断方块下落在哪里停止. 在数据的存储上,需要保存整个&qu ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. java俄罗斯方块游戏代码

    java俄罗斯方块游戏代码: package com; import java.awt.Color; import java.awt.Graphics; import java.awt.event.K ...

  4. 俄罗斯方块游戏 --- java

    俄罗斯方块游戏 如有疑问请查看:http://zh.wikipedia.org/zh-tw/%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97 更多疑问请参考: ...

  5. Java课设--俄罗斯方块Tetris

    Java程序设计课程作业报告 作业:俄罗斯方块游戏 姓名 赵璐媛 学号 程序得分 90% 作业报告 得分10% 实验总分 100% 作业目的: 掌握基本的图形程序设计方法 掌握Java事件处理程序编写 ...

  6. Java项目--俄罗斯方块

    Java项目--俄罗斯方块 百度盘链接 链接:http://pan.baidu.com/s/1mhQ9SYc 密码:9ujo 一.心得 二.游戏实例 游戏截图 目录结构 三.代码 1.主界面 Tetr ...

  7. Java实例---俄罗斯方块

    代码分析 定义格子 package com.ftl.tetris; /** * 定义格子 */ import java.awt.image.BufferedImage; public class Ce ...

  8. 台哥原创:java 俄罗斯方块源码

    大四的时候,用java开发,耗时一周 界面参照当时用的联想手机里的俄罗斯方块 ​ 这里的级别,标识难度,1是初级,方块下降速度很慢,5是最高级,下降速度最快 ​ 得分:每消除一行,会给10分,同时消除 ...

  9. JAVA SE 框架之俄罗斯方块的效果

    Mygame package com.sun.c; import java.awt.event.KeyListener; import com.sun.v.MyJpanel; import com.s ...

随机推荐

  1. 动态加载js和css

    开发过程中经常需要动态加载js和css,今天特意总结了一下常用的方法. 1.动态加载js 方法一:动态加载js文件 // 动态加载js脚本文件 function loadScript(url) { v ...

  2. nyoj-一笔画问题-欧拉图+联通判定

    一笔画问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...

  3. ahjesus web动态icon

    刚刚逛插件无意间发现的,记录下,里面有demo可以直接run了看效果 http://nicolasbize.com/faviconx/ http://www.miaofree.com/

  4. FactoryMethodPattern(工厂方法)

    /** * 工厂方法模式 * 分为四部分 * 1.产品接口 * 2.产品实例 * 3.工厂接口 * 4.工厂实例 * 工厂类最好用单例模式,但在这里主要是说明工厂方法,就不用单例了 * @author ...

  5. Lind.DDD.Caching分布式数据集缓存介绍

    回到目录 戏说当年 大叔原创的分布式数据集缓存在之前的企业级框架里介绍过,大家可以关注<我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器>,而今天主要对Lind.DDD.Cachin ...

  6. C#浅析单例模式

    第一次写博客,写的不好休怪哈. 版本1:最简单的单例模式 方法一: public class MySingleton { private MySingleton() //构造函数,注意private ...

  7. Pro HTML5 Programming(Second Edition)2.Canvas API(2)

    1.在页面中加入canvas元素 eg: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  8. javascript如何设置DIV背景色为随机色

    随机色有两种格式: 效果预览:http://wjf444128852.github.io/DEMOLIST/JS/test/index.html 1.rgb(xxx,xxx,xxx) 2.#xxxxx ...

  9. html5 video

    先简要概述一下video标签: video:嵌入视频到页面中 1. 声明video标签 单个视频的时候使用src: <video src="http://v2v.cc/~j/theor ...

  10. iOS AutoLayout的用法

    添加约束 代码实现Autolayout的步骤 利用NSLayoutConstraint类创建具体的约束对象 添加约束对象到相应的view上 - (void)addConstraint:(NSLayou ...