程序效果:

代码:

//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. java程序员保持天天快乐的6个习惯

    忍不住感叹,我第一次对Buffer(在社交媒体上发布最简单的方式)有所想法已经差不多是两年前的事了.并且,在我有想法的一年半前,我还在前面那家新创公司工作的时...... 忍不住感叹,我第一次对Buf ...

  2. Java基本概念(2)J2EE里面的2是什么意思

    J2EE里面的2是什么意思 J2SE,J2SE,J2ME中2的含义要追溯要1998年.1998年Java 1.2版本发布,1999年发布Java 1.2的标准版,企业版,微型版三个版本,为了区分这三个 ...

  3. Java之方法重载篇(我重载了,你要如何来调用我。。)

      一.课前引言 请看一下代码,你发现什么特殊之处了吗? public class MethodOverload { public static void main(String[] args) { ...

  4. ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: com.goodfan.entity.BeanA.beanB, no session or session was closed

    1. 问题, 当使用JSONArray.fromObject(List<BeanA>)时, beanA中含有BeanB的属性beanB时,会报这个错 2. 解决办法: 使用jsonconf ...

  5. for循环语句的笔试

    首先对for循环的初始条件进行调用打印A,之后判断语句输出B,第三个条件先不使用,直接进入循环内部 打印D,再到C:第二次判断i=1,A这个条件不使用了,进入B判定,再是D,C,最后进行判定输出B

  6. 【转】php pdo连接数据库 解决中文乱码问题(wordpress mysql 问号?? ??)

    原文链接:http://blog.csdn.net/ysydao/article/details/11002295 PHP 用pdo连接数据库时出现中文乱码问题解决办法 1.array(PDO::MY ...

  7. 关于Lind.DDD.Api客户端的使用与知识分享

    回到目录 关于Lind.DDD.Api的使用与客户端的调用 作者:张占岭 花名:仓储大叔 框架:Lind.DDD,Lind.DDD.Api 目录 Api里注册全局校验特性 1 Api中设置全局的Cor ...

  8. 对于SSH框架的选择

    选择框架:SSH 对于Web开发来说,SSH框架可以提高开发效率,还可以方便需求的变更以及对后期维护方面更容易操作.SSH也是目前稍微流行的Web开发框架. 选择框架描述: 首先说明一下SSH并不是一 ...

  9. DOM中的事件对象

    三.事件对象事件对象event1.DOM中的事件对象(1).type:获取事件类型(2).target:事件目标(3).stopPropagation() 阻止事件冒泡(4).preventDefau ...

  10. 盘点国内11家已经获得融资的移动CRM平台

    盘点国内11家已经获得融资的移动CRM平台 亿欧网盘点了目前国内已经获得融资的11家移动CRM平台,它们分别是:纷享销客.红圈营销.小满科技.腾腾科技.麦客.美洽.销售易.快消总管.EC营客通.店小三 ...