package net.rectang;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class RectangActionController implements ActionListener, WindowListener, KeyListener{
public static final String NEW_GAME = "new game";
public static final String PASE_GAME = "pase game";
public static final String EXIT_GAME = "exit game";
public static final int SIZE = 10;
public static final int MOVE_RIGHT = 1;
public static final int MOVE_LEFT = 2;
public static final int MOVE_DOWN = 3;
public static final int CHANGE = 4;
public static final int NATURE_DOWN = 5;
private int derector = NATURE_DOWN;
public static final int DEFAULT_SIZE = 5;
private IndexLocation indexLocation = null;
private Rectangle rectang = null;
private Graphics graphics = null;
private int windowWidth;
private int windowHeight;
private boolean flag = true;
private boolean[][] masks = new boolean[12][25];
private int sleepTime = 400;
private JFrame win;
private MainPanel mainPanel;
private static RectangActionController instance;
// static{
// instance = new ActionController();
// }
private RectangActionController(){
}
public synchronized static RectangActionController getInstance(){
if(instance == null){
instance = new RectangActionController();
}
return instance;
}
public JFrame getWin() {
return win;
}
public void setWin(JFrame win) {
this.win = win;
}
public MainPanel getMainPanel() {
return mainPanel;
}
public void setMainPanel(MainPanel mainPanel) {
this.mainPanel = mainPanel;
}
public void startGame(){
win.setSize(175, 332);
win.setContentPane(mainPanel);
win.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals(NEW_GAME)){
gameStart();
}else if(cmd.equals(PASE_GAME)){
}else if(cmd.equals(EXIT_GAME)){
exit();
}
}
private void gameStart(){
flag = true;
for(int i = 0; i < 12; i++){
for(int j = 0; j < 25; j++){
masks[i][j] = false;
if(j ==24){
masks[i][j] = true;
}
}
}
rectang = new Rectangle();
graphics = mainPanel.getRecPanel().getGraphics();
indexLocation = rectang.getAllLocation();
windowWidth = mainPanel.getRecPanel().getWidth();
windowHeight = mainPanel.getRecPanel().getHeight();
GameThread gameThread = new GameThread();
gameThread.start();
}
private class GameThread extends Thread{
public void run(){
int score = 0;
int mask = 0;
sleepTime = 300;
derector = NATURE_DOWN;
while(flag){
synchronized (graphics) {
clearRectang(indexLocation);
if(derector != NATURE_DOWN){
move(derector, indexLocation);
derector = NATURE_DOWN;
}else{
indexLocation = getNextIndex(indexLocation);
}
drawRectang(indexLocation);
if(downStop(indexLocation)){
masks[(indexLocation.getIndexOne().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexOne().getLocationY() - DEFAULT_SIZE) / 10] = true;
masks[(indexLocation.getIndexTwo().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexTwo().getLocationY() - DEFAULT_SIZE) / 10] = true;
masks[(indexLocation.getIndexThree().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexThree().getLocationY() - DEFAULT_SIZE) / 10] = true;
masks[(indexLocation.getIndexFour().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexFour().getLocationY() - DEFAULT_SIZE) / 10] = true;
rectang = new Rectangle();
indexLocation = rectang.getAllLocation();
sleepTime = 300;
score = 0;
for(int j = 23; j > 1; j--){
for(int i = 0; i < 12; i++){
if(!masks[i][j]){
mask = 1;
break;
}
}
if(mask == 0){
score = ((score + 1) / 2) * 2 + 1;
clear(j);
j++;
}
mask = 0;
}
mainPanel.getScoreNumLabel().setText(Integer.parseInt(mainPanel.getScoreNumLabel().getText()) + score + "");
for(int i = 0; i < 12; i++){
if(masks[i][0] == true){
JOptionPane.showMessageDialog(win, "Game Over!");
flag = false;
break;
}
}
}
try {
sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void clear(int j) {
int step = j;
for(int i = 0; i < 12; step--){
if(!masks[i][step - 1]){
masks[i][step] = false;
graphics.clearRect(i * 10 + DEFAULT_SIZE, step * 10 + DEFAULT_SIZE, SIZE, SIZE);
step = j + 1;
i++;
}
}
}
private boolean downStop(IndexLocation indexLocation) {
if(indexLocation.getMaxHeight() >= (windowHeight - 3 * DEFAULT_SIZE)){
return true;
}
if(indexLocation.getIndexOne().getLocationY() >= DEFAULT_SIZE){
if(masks[(indexLocation.getIndexOne().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexOne().getLocationY() + DEFAULT_SIZE) / 10]){
return true;
}
}
if(indexLocation.getIndexTwo().getLocationY() >= DEFAULT_SIZE){
if(masks[(indexLocation.getIndexTwo().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexTwo().getLocationY() + DEFAULT_SIZE) / 10]){
return true;
}
}
if(indexLocation.getIndexThree().getLocationY() >= DEFAULT_SIZE){
if(masks[(indexLocation.getIndexThree().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexThree().getLocationY() + DEFAULT_SIZE) / 10]){
return true;
}
}
if(indexLocation.getIndexFour().getLocationY() >= DEFAULT_SIZE){
if(masks[(indexLocation.getIndexFour().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexFour().getLocationY() + DEFAULT_SIZE) / 10]){
return true;
}
}
return false;
}
}
private void clearRectang(IndexLocation indexLocation){
graphics.clearRect(indexLocation.getIndexOne().getLocationX(), indexLocation.getIndexOne().getLocationY(), SIZE, SIZE);
graphics.clearRect(indexLocation.getIndexTwo().getLocationX(), indexLocation.getIndexTwo().getLocationY(), SIZE, SIZE);
graphics.clearRect(indexLocation.getIndexThree().getLocationX(), indexLocation.getIndexThree().getLocationY(), SIZE, SIZE);
graphics.clearRect(indexLocation.getIndexFour().getLocationX(), indexLocation.getIndexFour().getLocationY(), SIZE, SIZE);
}
private void drawRectang(IndexLocation indexLocation){
graphics.fillRect(indexLocation.getIndexOne().getLocationX(), indexLocation.getIndexOne().getLocationY(), SIZE, SIZE);
graphics.fillRect(indexLocation.getIndexTwo().getLocationX(), indexLocation.getIndexTwo().getLocationY(), SIZE, SIZE);
graphics.fillRect(indexLocation.getIndexThree().getLocationX(), indexLocation.getIndexThree().getLocationY(), SIZE, SIZE);
graphics.fillRect(indexLocation.getIndexFour().getLocationX(), indexLocation.getIndexFour().getLocationY(), SIZE, SIZE);
}
private IndexLocation getNextIndex(IndexLocation indexLocation){
indexLocation.getIndexOne().setLocationY(indexLocation.getIndexOne().getLocationY() + SIZE);
indexLocation.getIndexTwo().setLocationY(indexLocation.getIndexTwo().getLocationY() + SIZE);
indexLocation.getIndexThree().setLocationY(indexLocation.getIndexThree().getLocationY() + SIZE);
indexLocation.getIndexFour().setLocationY(indexLocation.getIndexFour().getLocationY() + SIZE);
return indexLocation;
}
public void move(int derector, IndexLocation indexLocation) {
if(derector == RectangActionController.MOVE_RIGHT){
//System.out.println(windowWidth + "" + windowHeight);
if(indexLocation.getMaxWidth() < windowWidth - DEFAULT_SIZE - SIZE){
indexLocation.getIndexOne().setLocationX(indexLocation.getIndexOne().getLocationX() + SIZE);
indexLocation.getIndexTwo().setLocationX(indexLocation.getIndexTwo().getLocationX() + SIZE);
indexLocation.getInd
没有合适的资源?快使用搜索试试~ 我知道了~
JAVA编的俄罗斯方块
共21个文件
class:10个
java:9个
project:1个
需积分: 0 10 下载量 29 浏览量
2008-12-12
19:49:51
上传
评论
收藏 24KB RAR 举报
温馨提示
这是我编的一个小的JAVA的俄罗斯方块,不是很成熟,只实现了基本功能,是用MVC架构做的。还能玩,就是不太美观。
资源详情
资源评论
资源推荐
收起资源包目录
YangRectang.rar (21个子文件)
YangRectang
.project 488B
bin
net
rectang
RectangThread.class 5KB
Test.class 954B
IndexLocation.class 2KB
MainRectang.class 3KB
Index.class 1KB
Rectangle.class 2KB
MainPanel.class 4KB
RectangActionController$GameThread.class 5KB
MainApp.class 906B
RectangActionController.class 9KB
src
net
rectang
Rectangle.java 3KB
Test.java 825B
MainPanel.java 5KB
IndexLocation.java 2KB
MainRectang.java 3KB
RectangActionController.java 11KB
Index.java 575B
RectangThread.java 6KB
MainApp.java 402B
.classpath 226B
共 21 条
- 1
yang228
- 粉丝: 6
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 重启进行BIOS快捷方式,不需要开机按BIOS键
- 威纶通触摸屏编程软件Easy builder pro V6.09.01.556安装包(2024.04).txt
- WindowsAdminCenter
- 老飞飞搭建基础通用数据库V19数据库.rar
- jquery.js
- 机械设计多工位ACF贴胶带&预压设备sw18可编辑非常好的设计图纸100%好用.zip
- 基于Pytorch复现Point-Transformer,用于ShapeNet数据集点云分割
- 【医学影像分析】2D超声图像的分割检测(Ultrasound Nerve Segmentation - Kaggle数据集)
- 嘎嘎香的五款神仙谷歌插件
- .arch书源导入教程.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0