• 大小: 18.1MB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2023-07-13
  • 语言: Java
  • 标签: Java  联机  

资源简介

这是本人初次MVC的练习,关于俄罗斯方块的控制代码借鉴了网上的一些资料,不足之处尽请谅解。 注:本程序存在一个偶尔会发生的方块堆积到顶端时无法停止游戏的bug,原因未知。 详见:http://blog.csdn.net/a343902152/article/details/49914759

资源截图

代码片段和文件信息

package Controller;

import MySocket.ExchangeThread;
import entity.Rect;
import model.GameDao;
import view.OfflinePanel;
import view.OnlinePanel;

import javax.swing.*;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 游戏整体控制器,比如命令方块移动,暂停游戏等
 * Create by Zhou
 */
public class GameController {

public static GameController localController;

// 界面
private JPanel panel;
// 时间控制器,加载Gametask,每过一段时间,界面就变化一次
private Timer timer;

private boolean isRunning =false;

public GameDao getGamedao() {
return gamedao;
}

public Rect getCurRect() {
return curRect;
}

public Rect getNextRect() {
return nextRect;
}

// 游戏进程控制器,比如碰撞检测之类的
private GameDao gamedao;

// 当前图形与下一个图形
private Rect curRect;
private Rect nextRect;

// 远程通信用的线程
private ExchangeThread exchangeThread;

private class Gametask extends TimerTask {
private int speed = 5;
        public void run() {

if(!isRunning){
return ;
}

// speed来控制时间间隔。。
         if(speed <= 0){
if(gamedao.isput(curRect)){
if(gamedao.gameover()) {
System.out.println(“begin to end game“);
// 先暂停游戏
isRunning = false;
if(exchangeThread!=null){
exchangeThread.sendMessage(“gameover“);
int myScore = gamedao.score;
int remoteScore = RemoteController.remoteController.getGameDao().score;

String str = Integer.toString(myScore) + “比“ + Integer.toString(remoteScore) + ““;
if (myScore > remoteScore) {
// WIN
JOptionPane.showMessageDialog(panel str + “你赢了“);
} else if (myScore < remoteScore) {
// LOSE
JOptionPane.showMessageDialog(panel str + “你输了“);
} else {
// pingju
JOptionPane.showMessageDialog(panel str + “这是一场平局“);
}
}else{
int myScore = gamedao.score;
JOptionPane.showMessageDialog(panel “游戏结束.“+
“你的得分为:“+Integer.toString(myScore));
}
return;
}
Random random = new Random();
// 已经放下来了的意思
if(exchangeThread!=null){
exchangeThread.sendMessage(“isput“);
}
curRect.setColor(0);
int temp=random.nextInt(7)+1;
curRect = new Rect(nextRect.color);
nextRect = new Rect(temp);
if(exchangeThread!=null){
// 更新rect的命令只发送下一个的
exchangeThread.sendMessage(Integer.toString(temp));
}
if(gamedao.ispop()){
// 消去一行
if(exchangeThread!=null){
exchangeThread.sendMessage(“ispop“);
}
}
                }else{
curRect.down();
if(exchangeThread!=null)
exchangeThread.sendMessage(“down“);
}
// 如果没有放下来,就down
                panel.repaint();
                speed=10-gamedao.level;
         }
         else{
speed--;
}
        }
    }
public GameController(JPanel panel){
this.panel=(OfflinePanel)panel;
}

public GameController(ExchangeThread threadOnlineP

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-11-14 21:05  Project2_newTetris\
     文件         301  2015-11-12 13:27  Project2_newTetris\.classpath
     目录           0  2015-11-14 21:05  Project2_newTetris\.git\
     文件         110  2015-11-12 22:56  Project2_newTetris\.git\FETCH_HEAD
     文件          23  2015-11-12 22:52  Project2_newTetris\.git\HEAD
     文件         315  2015-11-12 22:53  Project2_newTetris\.git\config
     文件          73  2015-11-12 22:52  Project2_newTetris\.git\description
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\hooks\
     文件         177  2015-11-12 22:52  Project2_newTetris\.git\hooks\README.sample
     文件        6112  2015-11-13 23:27  Project2_newTetris\.git\index
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\info\
     文件         113  2015-11-12 22:52  Project2_newTetris\.git\info\exclude
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\logs\
     文件         387  2015-11-12 22:53  Project2_newTetris\.git\logs\HEAD
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\logs\refs\
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\logs\refs\heads\
     文件         387  2015-11-12 22:53  Project2_newTetris\.git\logs\refs\heads\master
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\logs\refs\remotes\
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\logs\refs\remotes\origin\
     文件         144  2015-11-12 22:53  Project2_newTetris\.git\logs\refs\remotes\origin\master
     目录           0  2015-11-14 21:05  Project2_newTetris\.git\objects\
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\objects\02\
     文件        1132  2015-11-12 22:53  Project2_newTetris\.git\objects\02\ff1ae07e2d4e6d42527890c526e296a317c03b
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\objects\11\
     文件          83  2015-11-12 22:53  Project2_newTetris\.git\objects\11\9591274ce4c1200bf568149028590221e4de6d
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\objects\12\
     文件        1217  2015-11-12 22:53  Project2_newTetris\.git\objects\12\14e4cfb1a718b782a431e86415af46213f24f9
     文件         813  2015-11-12 22:53  Project2_newTetris\.git\objects\12\aab59d70f648124ed1e2b22d9999ac6a5a4201
     目录           0  2015-11-14 21:04  Project2_newTetris\.git\objects\15\
     文件         289  2015-11-12 22:53  Project2_newTetris\.git\objects\15\70ffec28bdba3756ea7cc11156665ffaedcf66
     文件         168  2015-11-12 22:53  Project2_newTetris\.git\objects\15\b3e777204395c052ae667bb8a5ecfeddd7d2a6
............此处省略254个文件信息

评论

共有 条评论