前言
大富翁,又名地产大亨。是一种多人策略图版游戏。参与者分得游戏金钱,凭运气(掷骰子)及交易策略,买地、建楼以赚取租金。英文原名monopoly意为“垄断”,因为最后只得一个胜利者,其余均破产收场。
《大富翁》游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想。
主要需求
可多人参与的大富翁游戏,玩家有初始资金,通过掷骰子,玩家移动指定骰子点数步骤,根据对应格子上的交易策略,来决定是赚钱还是亏钱,其他玩家破产,即唯一玩家胜利。
主要设计
1、用户数据设定-人物设置:设置两个玩家的角色头像和用户名
2、用户数据设定-场景设置:选择不同的地图
3、用户数据设定-游戏设置:游戏天数,胜利金钱,玩家初始金钱
4、设置默认胜利条件:破产为失败
5、掷骰子效果
6、角色移动的步数效果
7、不同地图的策略设计算法:不同的格子,效果不同
功能截图
用户数据设定页面-人物设置
用户数据设定页面-场景设置
用户数据设定页面-游戏设置
开始游戏界面
掷骰子
移动效果
触发策略效果
金币不足时提示
游戏结束
代码实现
游戏配置窗口
public class FrameConfig extends JFrame {
private JButton jbnStart = new JButton("开始游戏");
//private JButton jbnradom = new JButton("随机");
private JButton jbnCancel = new JButton("重置设定");
private JButton jbnPlayer01 = new JButton("1P确认角色");
private JLabel jbnPlayerNameLabel01 = new JLabel("名字:");
private JTextField jbnPlayerNameField01 = new JTextField(12);
private JButton jbnPlayerName01 = new JButton("1P确认名字");
private JButton jbnPlayer02 = new JButton("2P确认角色");
private JLabel jbnPlayerNameLabel02 = new JLabel("名字:");
private JTextField jbnPlayerNameField02 = new JTextField(12);
private JButton jbnPlayerName02 = new JButton("2P确认名字");
private JTabbedPane tabs;
private ImageIcon[] img = Photo.PLAYER_CHOOSE;
private JLabel jlPlayer01Choose = null;
private final JLabel jlPlayer01Selected = new JLabel(
Photo.PLAYER_01_SELECTED);
private JButton leftButton01;
private JButton rightButton01;
private JLabel jlPlayer02Choose = null;
private final JLabel jlPlayer02Selected = new JLabel(
Photo.PLAYER_02_SELECTED);
private JButton leftButton02;
private JButton rightButton02;
private int[] chooses = { 0, 0 };
private int[] selected = { -1, -2 };
private String[] selectedName = { "", "" };
private JFrameGame jFrameGame;
public FrameConfig(WaitFrame wFrame,JFrameGame jFrameGame) {
wFrame.setVisible(false);
this.jFrameGame = jFrameGame;
setTitle("用户数据设定");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局管理器为边界布局
this.setLayout(new BorderLayout());
// 添加主面板
this.add(this.createMainPanel(), BorderLayout.CENTER);
// 添加按钮面板
this.add(this.createButtonPanel(), BorderLayout.SOUTH);
this.setResizable(false);
this.setSize(380, 370);
// 居中对齐
FrameUtil.setFrameCenter(this);
setVisible(true);
}
private JTabbedPane createMainPanel() {
this.tabs = new JTabbedPane();
this.tabs.setOpaque(false);
this.tabs.add("人物设置", this.createPlayerSelectPanel());
this.tabs.setToolTipTextAt(0, "完成人物设置");
this.tabs.add("场景设置", this.createMapSelectPanel());
this.tabs.setToolTipTextAt(1, "可以设置游戏场景");
this.tabs.add("游戏设置", this.createGameSelectPanel());
this.tabs.setToolTipTextAt(2, "可以设置游戏胜利条件等...");
return tabs;
}
private Component createGameSelectPanel() {
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.setBackground(new Color(235,236,237));
// --------------------------------
final JPanel dayPanel = new JPanel();
dayPanel.setBorder(BorderFactory.createTitledBorder(""));
JLabel day = new JLabel("游戏天数");
final String[] days = { "无限制", "20", "40", "80", "120", "240", "480" };
final Choice daysChoice = new Choice();
for (String a : days) {
daysChoice.add(a);
}
daysChoice.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
String str = days[daysChoice.getSelectedIndex()];
if (str.equals("无限制")) {
GameRunning.GAME_DAY = -1;
} else {
GameRunning.GAME_DAY = Integer.parseInt(str);
}
}
});
dayPanel.add(day);
dayPanel.add(daysChoice);
// --------------------------------
JPanel moneyPanel = new JPanel();
moneyPanel.setBorder(BorderFactory.createTitledBorder(""));
JLabel money = new JLabel("胜利金钱");
final String[] money_ = { "无限制", "10000", "20000", "40000", "80000",
"200000" };
final Choice moneyChoice = new Choice();
for (String a : money_) {
moneyChoice.add(a);
}
moneyChoice.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
String str = money_[moneyChoice.getSelectedIndex()];
if (str.equals("无限制")) {
GameRunning.MONEY_MAX = -1;
} else {
GameRunning.MONEY_MAX = Integer.parseInt(str);
}
}
});
moneyPanel.add(money);
moneyPanel.add(moneyChoice);
// ---------------------------------
// --------------------------------
JPanel cashPanel = new JPanel();
cashPanel.setBorder(BorderFactory.createTitledBorder(""));
JLabel cash = new JLabel("玩家初始金钱");
final String[] cash_ = { "1000", "2000", "5000", "7000", "10000",
"20000" };
final Choice cashChoice = new Choice();
for (String a : cash_) {
cashChoice.add(a);
}
cashChoice.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
String str = cash_[cashChoice.getSelectedIndex()];
GameRunning.PLAYER_CASH = Integer.parseInt(str);
// System.out.println(GameRunning.PLAYER_CASH);
}
});
cashPanel.add(cash);
cashPanel.add(cashChoice);
JPanel infoPanel = new JPanel();
infoPanel.setBorder(BorderFactory.createTitledBorder(""));
JLabel info = new JLabel();
info.setText("<html>可以改变游戏的胜利条件.<strong>(默认破产为失败)</strong></html>");
infoPanel.add(info);
panel.add(dayPanel);
panel.add(moneyPanel);
panel.add(cashPanel);
panel.add(infoPanel);
return panel;
}
private JPanel createMapSelectPanel() {
JPanel jp = new JPanel();
jp.setLayout(new GridLayout());
jp.setBackground(new Color(235,236,237));
JPanel lPane = new JPanel(new BorderLayout());
String[] maps = { "\"LOVE地图\"", "\"鬼屋地图\"", "\"好运地图\"" };
final ImageIcon[] maps1 = {
new ImageIcon("images/other/1.png"),
new ImageIcon("images/other/2.png"),
new ImageIcon("images/other/3.png") };
final JList jlst = new JList(maps);
jlst.setSelectedIndex(0);
final JLabel mapV = new JLabel(maps1[0]);
final JButton ok = new JButton("确定");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
GameRunning.MAP = jlst.getSelectedIndex() + 1;
ok.setText("已选");
}
});
jlst.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
mapV.setIcon(maps1[jlst.getSelectedIndex()]);
ok.setText("确定");
}
});
lPane.add(jlst);
lPane.add(ok, BorderLayout.SOUTH);
JPanel rPane = new JPanel();
rPane.add(mapV);
JSplitPane jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
false, lPane, rPane);
jp.add(jSplitPane);
return jp;
}
private JPanel createPlayerSelectPanel() {
JPanel jp = new JPanel();
jp.setLayout(null);
jp.setBackground(new Color(235,236,237));
// 增加1P面板
addPlayer01Config(12, 0, jp);
// 增加2P面板
addPlayer02Config(212, 0, jp);
// 增加重置按钮
addCancelButton(jp);
return jp;
}
private void addCancelButton(JPanel panel) {
jbnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
reLoad();
}
private void reLoad() {
leftButton01.setEnabled(true);
rightButton01.setEnabled(true);
jbnPlayer01.setEnabled(true);
jlPlayer01Selected.setVisible(false);
jlPlayer01Choose.setIcon(img[0]);
jbnPlayerNameField01.setText("");
jbnPlayerNameField01.setEditable(true);
jbnPlayerName01.setEnabled(true);
selected[0] = -1;
chooses[0] = 0;
leftButton02.setEnabled(true);
rightButton02.setEnabled(true);
jbnPlayer02.setEnabled(true);
jlPlayer02Selected.setVisible(false);
jlPlayer02Choose.setIcon(img[0]);
jbnPlayerNameField02.setText("");
jbnPlayerNameField02.setEditable(true);
jbnPlayerName02.setEnabled(true);
selected[1] = -2;
chooses[1] = 0;
repaint();
}
});
jbnCancel.setBounds(256 + 7, 235, 80, 30);
panel.add(jbnCancel);
}
private void addPlayer01Config(int x, int y, JPanel jp) {
// 创建 人物图像label
jlPlayer01Choose = new JLabel(img[chooses[0]]);
jlPlayer01Choose.setBounds(x + 8, y, 128, 128);
// 创建人物图像已选择label
jlPlayer01Selected.setBounds(x + 8, y, 128, 128);
jlPlayer01Selected.setVisible(false);
// 创建左按钮
leftButton01 = this.createButton(x, 92 + y, Photo.BUTTON_LEFT, 'a');
// 添加监听事件
leftButton01.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 设置为循环
if (chooses[0] <= 0) {
chooses[0] = img.length;
}
jlPlayer01Choose.setIcon(img[--chooses[0]]);
}
});
jp.add(leftButton01);
// 创建右按钮
rightButton01 = this.createButton(128 + x, 92 + y, Photo.BUTTON_RIGHT,
'd');
// 添加监听事件
rightButton01.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// 设置循环
if (chooses[0] >= img.length - 1) {
chooses[0] = -1;
}
jlPlayer01Choose.setIcon(img[++chooses[0]]);
}
});
jp.add(rightButton01);
// 增加确定框
jbnPlayer01.setBounds(12 + x, 128 + y, 120, 30);
// 增加事件监听
jbnPlayer01.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if ((chooses[0] != selected[1])) {
// 设置不能点击
leftButton01.setEnabled(false);
rightButton01.setEnabled(false);
jbnPlayer01.setEnabled(false);
// 增加选择图片
jlPlayer01Selected.setVisible(true);
selected[0] = chooses[0];
}
}
});
jp.add(jbnPlayer01);
jp.add(jlPlayer01Selected);
jp.add(jlPlayer01Choose);
// 增加名字框
jbnPlayerNameLabel01.setBounds(x + 12, y + 128 + 36, 50, 30);
jbnPlayerNameField01.setBounds(x + 12 + 30, y + 128 + 36, 120 - 30, 30);
jbnPlayerName01.setBounds(x + 12, y + 128 + 36 + 36, 120, 30);
// 按钮添加监听
jbnPlayerName01.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!jbnPlayerNameField01.getText().equals("")) {
selectedName[0] = jbnPlayerNameField01.getText();
jbnPlayerNameField01.setEditable(false);
jbnPlayerName01.setEnabled(false);
}
}
});
jp.add(jbnPlayerNameLabel01);
jp.add(jbnPlayerNameField01);
jp.add(jbnPlayerName01);
}
private void addPlayer02Config(int x, int y, JPanel jp) {
// 创建 人物图像label
jlPlayer02Choose = new JLabel(img[chooses[1]]);
jlPlayer02Choose.setBounds(x + 8, y, 128, 128);
// 创建人物图像已选择label
jlPlayer02Selected.setBounds(x + 8, y, 128, 128);
jlPlayer02Selected.setVisible(false);
// 创建左按钮
leftButton02 = this.createButton(x, 92 + y, Photo.BUTTON_LEFT, 'a');
// 添加监听事件
leftButton02.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 设置为循环
if (chooses[1] <= 0) {
chooses[1] = img.length;
}
jlPlayer02Choose.setIcon(img[--chooses[1]]);
}
});
jp.add(leftButton02);
// 创建右按钮
rightButton02 = this.createButton(128 + x, 92 + y, Photo.BUTTON_RIGHT,
'd');
// 添加监听事件
rightButton02.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// 设置循环
if (chooses[1] >= img.length - 1) {
chooses[1] = -1;
}
jlPlayer02Choose.setIcon(img[++chooses[1]]);
}
});
jp.add(rightButton02);
// 增加确定框
jbnPlayer02.setBounds(12 + x, 128 + y, 120, 30);
// 增加事件监听
jbnPlayer02.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (selected[0] != chooses[1]) {
// 设置不能点击
leftButton02.setEnabled(false);
rightButton02.setEnabled(false);
jbnPlayer02.setEnabled(false);
// 增加选择图片
jlPlayer02Selected.setVisible(true);
selected[1] = chooses[1];
}
}
});
jp.add(jbnPlayer02);
jp.add(jlPlayer02Selected);
jp.add(jlPlayer02Choose);
// 增加名字框
jbnPlayerNameLabel02.setBounds(x + 12, y + 128 + 36, 50, 30);
jbnPlayerNameField02.setBounds(x + 12 + 30, y + 128 + 36, 120 - 30, 30);
jbnPlayerName02.setBounds(x + 12, y + 128 + 36 + 36, 120, 30);
// 按钮添加监听
jbnPlayerName02.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!jbnPlayerNameField02.getText().equals("")) {
selectedName[1] = jbnPlayerNameField02.getText();
jbnPlayerNameField02.setEditable(false);
jbnPlayerName02.setEnabled(false);
}
}
});
jp.add(jbnPlayerNameLabel02);
jp.add(jbnPlayerNameField02);
jp.add(jbnPlayerName02);
}
public JButton createButton(int x, int y, ImageIcon[] img, char keyLinstenr) {
JButton add = new JButton("", img[0]);
add.setPressedIcon(img[3]);
add.setRolloverIcon(img[2]);
add.setMnemonic(keyLinstenr);
add.setBounds(x, y, img[0].getIconWidth(), img[0].getIconHeight());
return add;
}
private JPanel createButtonPanel() {
JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
// 开始按钮添加监听器
jbnStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (selected[0] < 0 || selected[1] < 0) {
JOptionPane.showMessageDialog(null, "请完成人物设置!");
} else if (selectedName[0].equals("")
|| selectedName[1].equals("")) {
JOptionPane.showMessageDialog(null, "请完成名字设置!");
} else {
int choose = JOptionPane.showConfirmDialog(null, "是否开始?");
if (choose == JOptionPane.OK_OPTION) {
// 开始游戏
startGame();
}
}
}
private void startGame() {
setVisible(false);
jFrameGame.setVisible(true);
Control control = jFrameGame.getPanelGame().getControl();
// 处理玩家数据配置
dealPlayers(control);
// 控制器启动
control.start();
}
private void dealPlayers(Control control) {
List<PlayerModel> tempPlayer = control.getPlayers();
// 传入名字
tempPlayer.get(0).setName(selectedName[0]);
tempPlayer.get(1).setName(selectedName[1]);
// 传入使用角色编号
tempPlayer.get(0).setPart(selected[0]);
tempPlayer.get(1).setPart(selected[1]);
// 传入 角色对立角色
tempPlayer.get(0).setOtherPlayer(tempPlayer.get(1));
tempPlayer.get(1).setOtherPlayer(tempPlayer.get(0));
}
});
jp.add(jbnStart);
//jp.add(jbnradom);
return jp;
}
}
游戏总控制器
public class Control {
public static long tick;
public static int rate = 30;
private JPanelGame panel;
private GameRunning run = null;
private List<Port> models = new ArrayList<Port>();
private List<PlayerModel> players = null;
private BuildingsModel building = null;
private BackgroundModel background = null;
private LandModel land = null;
private TextTipModel textTip = null;
private DiceModel dice = null;
private EventsModel events = null;
private EffectModel effect = null;
private Music music = null;
private Timer gameTimer = null;
public Control() {
// 创建一个游戏状态
this.run = new GameRunning(this, players);
// 初始化游戏对象
this.initClass();
// 向游戏状态中加入玩家模型
this.run.setPlayers(players);
}
public void setPanel(JPanelGame panel) {
this.panel = panel;
}
private void initClass() {
// 创建一个新的事件模型
this.events = new EventsModel();
this.models.add(events);
// 创建一个新的场景效果模型
this.effect = new EffectModel();
this.models.add(effect);
// 创建新的背景模型
this.background = new BackgroundModel();
this.models.add(background);
// 创建新的土地模型
this.land = new LandModel();
this.models.add(land);
// 创建新的文本显示模型
this.textTip = new TextTipModel();
this.models.add(textTip);
// 创建一个新的建筑模型
this.building = new BuildingsModel(land);
this.models.add(building);
// 创建一个新的玩家数组
this.players = new ArrayList<PlayerModel>();
this.players.add(new PlayerModel(1, this));
this.players.add(new PlayerModel(2, this));
this.models.add(players.get(0));
this.models.add(players.get(1));
// 创建一个新的骰子模型
this.dice = new DiceModel(run);
this.models.add(dice);
// 创建一个播放器
this.music = new Music();
}
private void createGameTimer() {
this.gameTimer = new Timer();
this.gameTimer.schedule(new TimerTask() {
@Override
public void run() {
tick++;
// 更新各对象
for (Port temp : models) {
temp.updata(tick);
}
// UI更新
panel.repaint();
}
}, 0, (1000 / rate));
}
public void start() {
// 创建一个计时器
this.createGameTimer();
// 刷新对象初始数据
for (Port temp : this.models) {
temp.startGameInit();
}
// 游戏环境开始
this.run.startGameInit();
// panel 初始化
this.panel.startGamePanelInit();
// 游戏背景音乐
this.startMusic();
// 游戏开始产生地图效果
this.effect.showImg("start");
}
private void startMusic() {
music.start();
}
public List<PlayerModel> getPlayers() {
return players;
}
public BuildingsModel getBuilding() {
return building;
}
public BackgroundModel getBackground() {
return background;
}
public LandModel getLand() {
return land;
}
public EffectModel getEffect() {
return effect;
}
public TextTipModel getTextTip() {
return textTip;
}
public GameRunning getRunning() {
return run;
}
public DiceModel getDice() {
return dice;
}
public EventsModel getEvents() {
return events;
}
public JPanelGame getPanel() {
return panel;
}
public void pressButton() {
PlayerModel player = this.run.getNowPlayer();
if (player.getInHospital() > 0 || player.getInPrison() > 0) {
this.run.nextState();
if (player.getInHospital() > 0) {
this.textTip.showTextTip(player, player.getName() + "住院中.", 3);
} else if (player.getInPrison() > 0) {
this.textTip.showTextTip(player, player.getName() + "在监狱.", 3);
}
this.run.nextState();
} else {
// 设置骰子对象开始转动时间
this.dice.setStartTick(Control.tick);
// 设置骰子对象结束转动时间
this.dice.setNextTick(this.dice.getStartTick()
+ this.dice.getLastTime());
// 将运行对象点数传入骰子对象
this.dice.setPoint(this.run.getPoint());
// 转换状态至“移动状态”
this.run.nextState();
// 骰子转动完毕后玩家移动
this.run.getNowPlayer().setStartTick(this.dice.getNextTick() + 10);
this.run.getNowPlayer().setNextTick(
this.run.getNowPlayer().getStartTick()
+ this.run.getNowPlayer().getLastTime()
* (this.run.getPoint() + 1));
}
}
public void movePlayer() {
// 人物运动
for (int i = 0; i < (60 / this.run.getNowPlayer().getLastTime()); i++) {
// 移动玩家
if (GameRunning.MAP == 1){
this.move01();
} else if (GameRunning.MAP == 2){
this.move02();
} else if (GameRunning.MAP == 3) {
this.move03();
}
}
}
public void prassBuilding() {
// 当前玩家
PlayerModel player = this.run.getNowPlayer();
// 该地点房屋
Building building = this.building.getBuilding(player.getY() / 60,
player.getX() / 60);
if (building != null && player.getX() % 60 == 0
&& player.getY() % 60 == 0) {
// 经过房屋发生事件
int event = building.passEvent();
// 进入经过房屋事件处理
disposePassEvent(building, event, player);
}
}
private void disposePassEvent(Building b, int event, PlayerModel player) {
switch (event) {
case GameState.ORIGIN_PASS_EVENT:
// 中途经过原点
passOrigin(b, player);
break;
default:
break;
}
}
private void passOrigin(Building b, PlayerModel player) {
this.textTip.showTextTip(player, player.getName() + " 路过原点,奖励 "
+ ((Origin) b).getPassReward() + "金币.", 3);
player.setCash(player.getCash() + ((Origin) b).getPassReward());
}
private void move02() {
int dice = this.run.getPoint() + 1;
PlayerModel p = this.run.getNowPlayer();
// 单位移动像素
int movePixel = 1;
if (p.getX() < 12 * 60 && p.getY() == 0) {
p.setX(p.getX() + movePixel);
} else if (p.getX() == 12 *60 && p.getY() < 2 * 60){
p.setY(p.getY() + movePixel);
} else if (p.getX() == 12 * 60 && p.getY() == 2 * 60){
if ((int)(Math.random() * 2 ) == 0){
p.setX(p.getX() - movePixel);
} else {
p.setY(p.getY() + movePixel);
}
} else if (p.getX() == 12 * 60 && p.getY() > 2 * 60 && p.getY() < 4 * 60){
p.setY(p.getY() + movePixel);
} else if (p.getX() > 8 * 60 && p.getX() <= 12 * 60 && p.getY() == 4 * 60){
p.setX(p.getX() - movePixel);
} else if (p.getX() == 8 * 60 && p.getY() == 4 * 60){
if ((int)(Math.random() * 2 ) == 0){
p.setX(p.getX() - movePixel);
} else {
p.setY(p.getY() + movePixel);
}
} else if (p.getX() > 4 * 60 && p.getX() < 8 * 60 && p.getY() == 4 * 60) {
p.setX(p.getX() - movePixel);
} else if (p.getX() == 8 * 60 && p.getY() > 4 * 60 && p.getY() < 7 * 60){
p.setY(p.getY() + movePixel);
} else if (p.getX() > 4 * 60 && p.getX() <= 8 * 60 && p.getY() == 7 * 60){
p.setX(p.getX() - movePixel);
} else if (p.getX() > 4 * 60 && p.getX() < 12 * 60 && p.getY() == 2 * 60){
p.setX(p.getX() - movePixel);
} else if (p.getX() == 4 * 60 && p.getY() >= 2 * 60 && p.getY() < 7 * 60){
p.setY(p.getY() + movePixel);
} else if (p.getX() > 0 && p.getX() <= 4 * 60 && p.getY() == 7 * 60){
p.setX(p.getX() - movePixel);
} else if (p.getX() == 0 && p.getY() > 0){
p.setY(p.getY() - movePixel);
}
}
private void move01() {
int dice = this.run.getPoint() + 1;
PlayerModel p = this.run.getNowPlayer();
// 单位移动像素
int movePixel = 1;
Boolean turn = dice % 2 != 0;
if (p.getX() < 9 * 60 && p.getY() == 0) {
// 上面
if (p.getX() == 4 * 60 && turn) {
// 分岔点情况
p.setY(p.getY() + movePixel);
} else {
p.setX(p.getX() + movePixel);
}
} else if (p.getX() == 9 * 60 && p.getY() >= 0 && p.getY() < 60) {
// [0,9]
// ↓
p.setY(p.getY() + movePixel);
} else if (p.getX() >= 8 * 60 && p.getX() < 12 * 60
&& p.getY() >= 1 * 60 && p.getY() <= 60 * 1.5) {
// →
p.setX(p.getX() + movePixel);
} else if (p.getX() == 12 * 60 && p.getY() >= 1 * 60
&& p.getY() < 7 * 60) {
// ↓
p.setY(p.getY() + movePixel);
} else if (p.getX() > 0 && p.getY() == 7 * 60) {
// ←
p.setX(p.getX() - movePixel);
} else if (p.getX() == 0 && p.getY() > 0) {
// ↑
p.setY(p.getY() - movePixel);
} else if (p.getX() == 4 * 60 && p.getY() > 0 && p.getY() < 7 * 60) {
// ↓
p.setY(p.getY() + movePixel);
}
}
private void move03() {
PlayerModel p = this.run.getNowPlayer();
// 单位移动像素
int movePixel = 1;
if (p.getX() < 12 * 60 && p.getY() == 0) {
p.setX(p.getX() + movePixel);
} else if (p.getX() == 12 *60 && p.getY() < 7 * 60){
p.setY(p.getY() + movePixel);
} else if (p.getX() > 0 && p.getY() == 7 * 60){
p.setX(p.getX() - movePixel);
} else if (p.getX() == 0 && p.getY() > 0){
p.setY(p.getY() - movePixel);
}
}
public void playerStopJudge() {
// 当前玩家
PlayerModel player = this.run.getNowPlayer();
if (player.getInHospital() > 0) {
this.textTip.showTextTip(player, player.getName() + "当前在医院,不能移动.",
2);
// 更换玩家状态
this.run.nextState();
} else if (player.getInPrison() > 0) {
this.textTip.showTextTip(player, player.getName() + "当前在监狱,不能移动.",
2);
// 更换玩家状态
this.run.nextState();
} else {
// 进行玩家操作(买房 事件等)
this.playerStop();
}
}
public void playerStop() {
// 当前玩家
PlayerModel player = this.run.getNowPlayer();
// 该地点房屋
Building building = this.building.getBuilding(player.getY() / 60,
player.getX() / 60);
if (building != null) {// 获取房屋
int event = building.getEvent();
// 触发房屋信息
disposeStopEvent(building, event, player);
}
}
private void disposeStopEvent(Building b, int event, PlayerModel player) {
switch (event) {
case GameState.HOSPITAL_EVENT:
// 停留在医院
stopInHospital(b, player);
break;
case GameState.HUOSE_EVENT:
// 停留在可操作土地
stopInHouse(b, player);
break;
case GameState.LOTTERY_EVENT:
// 停留在乐透点上
stopInLottery(b, player);
break;
case GameState.NEWS_EVENT:
// 停留在新闻点上
stopInNews(b, player);
break;
case GameState.ORIGIN_EVENT:
// 停留在原点
stopInOrigin(b, player);
break;
case GameState.PARK_EVENT:
// 停留在公园
stopInPack(b, player);
break;
case GameState.POINT_EVENT:
// 停留在点卷位
stopInPoint(b, player);
break;
case GameState.PRISON_EVENT:
// 停留在监狱
stopInPrison(b, player);
break;
case GameState.SHOP_EVENT:
// 停留在商店
stopInShop(b, player);
break;
}
}
private void stopInShop(Building b, PlayerModel player) {
if (player.getNx() > 0){
// 为商店的货架从新生成商品
((Shop_) b).createCards();
// 为商店面板更新新的卡片商品
this.panel.getShop().addCards((Shop_) b);
// 將商店面板推送至頂
this.panel.getShop().moveToFront();
} else {
this.run.nextState();
}
}
private void stopInPrison(Building b, PlayerModel player) {
int days = (int) (Math.random() * 3) + 2;
player.setInPrison(days);
int random = (int) (Math.random() * ((Prison) b).getEvents().length);
String text = ((Prison) b).getEvents()[random];
this.textTip.showTextTip(player, player.getName() + text + "停留"
+ (days - 1) + "天.", 3);
new Thread(new MyThread(run, 1)).start();
}
private void stopInPoint(Building b, PlayerModel player) {
player.setNx(((Point) b).getPoint() + player.getNx());
this.textTip.showTextTip(player, player.getName() + " 获得 "
+ ((Point) b).getPoint() + "点卷.", 3);
new Thread(new MyThread(run, 1)).start();
}
private void stopInPack(Building b, PlayerModel player) {
int random = (int) (Math.random() * ((Park) b).getImgageEvents().length);
switch (random) {
case 0:
case 1:
// 减一金币
player.setCash(player.getCash() - 1);
break;
case 2:
// 减200金币
player.setCash(player.getCash() - 200);
break;
case 3:
// 加200金币
player.setCash(player.getCash() + 200);
break;
}
// 在事件层显示事件
this.events.showImg(((Park) b).getImgageEvents()[random], 3, new Point(
320, 160, 0));
new Thread(new MyThread(run, 3)).start();
}
private void stopInOrigin(Building b, PlayerModel player) {
this.textTip.showTextTip(player, player.getName() + " 在起点停留,奖励 "
+ ((Origin) b).getReward() + "金币.", 3);
player.setCash(player.getCash() + ((Origin) b).getReward());
new Thread(new MyThread(run, 1)).start();
}
private void stopInNews(Building b, PlayerModel player) {
int random = (int) (Math.random() * ((News) b).getImgageEvents().length);
switch (random) {
case 0:
case 1:
// 设置天数
player.setInHospital(player.getInHospital() + 4);
// 玩家位置切换到医院位置
if (LandModel.hospital != null) {
player.setX(LandModel.hospital.x);
player.setY(LandModel.hospital.y);
}
break;
case 2:
case 3:
player.setCash(player.getCash() - 1000);
break;
case 4:
player.setCash(player.getCash() - 1500);
break;
case 5:
player.setCash(player.getCash() - 2000);
break;
case 6:
case 7:
player.setCash(player.getCash() - 300);
break;
case 8:
player.setCash(player.getCash() - 400);
break;
case 9:
// 点卷小于不能发生事件
if (player.getNx() < 40) {
stopInNews(b, player);
return;
}
player.setNx(player.getNx() - 40);
break;
case 10:
player.setCash(player.getCash() - 500);
break;
case 11:
player.setCash(player.getCash() + 1000);
break;
case 12:
case 13:
player.setCash(player.getCash() + 2000);
break;
case 14:
player.setCash(player.getCash() + 3999);
player.setNx(player.getNx() + 100);
break;
case 15:
player.setNx(player.getNx() + 300);
break;
case 16:
for (int i = 0; i < player.getCards().size();i++){
// System.out.println(player.getCards().get(i).getcName());
// 嫁祸卡
if (player.getCards().get(i).getName().equals("CrossingCard")){
player.getCards().remove(i);
// 对手减少金钱.
player.getOtherPlayer().setCash(player.getOtherPlayer().getCash() - 3000);
this.textTip.showTextTip(player, player.getName() + "将一笔\"3000元\"嫁祸给 "+ player.getOtherPlayer().getName()+"。真是人算不如天算啊.", 6);
this.events.showImg(((News) b).get3000(), 3, new Point(
420, 160, 0));
new Thread(new MyThread(run, 3)).start();
return;
}
}
player.setCash(player.getCash() - 3000);
break;
}
// 在事件层显示事件
this.events.showImg(((News) b).getImgageEvents()[random], 3, new Point(
420, 160, 0));
new Thread(new MyThread(run, 3)).start();
}
private void stopInLottery(Building b, PlayerModel player) {
// 未制作
new Thread(new MyThread(run, 1)).start();
}
private void stopInHouse(Building b, PlayerModel player) {
if (b.isPurchasability()) {// 玩家房屋
if (b.getOwner() == null) { // 无人房屋
// 执行买房操作
this.buyHouse(b, player);
} else {// 有人房屋
if (b.getOwner().equals(player)) {// 自己房屋
// 执行升级房屋操作
this.upHouseLevel(b, player);
} else {// 别人房屋
// 执行交税操作
this.giveTax(b, player);
}
}
}
}
private void giveTax(Building b, PlayerModel player) {
if (b.getOwner().getInHospital() > 0) {
// 增加文本提示
this.textTip.showTextTip(player, b.getOwner().getName()
+ "正在住院,免交过路费.", 3);
} else if (b.getOwner().getInPrison() > 0) {
// 增加文本提示
this.textTip.showTextTip(player, b.getOwner().getName()
+ "正在监狱,免交过路费.", 3);
} else {
int revenue = b.getRevenue();
// 该玩家减少金币
player.setCash(player.getCash() - revenue);
// 业主得到金币
b.getOwner().setCash(b.getOwner().getCash() + revenue);
// 增加文本提示
this.textTip.showTextTip(player, player.getName() + "经过"
+ b.getOwner().getName() + "的地盘,过路费:" + revenue + "金币.", 3);
}
new Thread(new MyThread(run, 1)).start();
}
private void upHouseLevel(Building b, PlayerModel player) {
if (b.canUpLevel()) {
// 升级房屋
int price = b.getUpLevelPrice();
String name = b.getName();
String upName = b.getUpName();
int choose = JOptionPane.showConfirmDialog(null,
"亲爱的:" + player.getName() + "\r\n" + "是否升级这块地?\r\n" + name
+ "→" + upName + "\r\n" + "价格:" + price + " 金币.");
if (choose == JOptionPane.OK_OPTION) {
if (player.getCash() >= price) {
b.setLevel(b.getLevel() + 1);
// 减少需要的金币
player.setCash(player.getCash() - price);
// 增加文本提示
this.textTip.showTextTip(player, player.getName() + " 从 "
+ name + " 升级成 " + upName + ".花费了 " + price
+ "金币. ", 3);
} else {
// 增加文本提示
this.textTip.showTextTip(player, player.getName()
+ " 金币不足,操作失败. ", 3);
}
}
}
new Thread(new MyThread(run, 1)).start();
}
private void buyHouse(Building b, PlayerModel player) {
int price = b.getUpLevelPrice();
int choose = JOptionPane.showConfirmDialog(
null,
"亲爱的:" + player.getName() + "\r\n" + "是否购买下这块地?\r\n"
+ b.getName() + "→" + b.getUpName() + "\r\n" + "价格:"
+ price + " 金币.");
if (choose == JOptionPane.OK_OPTION) {
// 购买
if (player.getCash() >= price) {
b.setOwner(player);
b.setLevel(1);
// 将该房屋加入当前玩家的房屋列表下
player.getBuildings().add(b);
// 减少需要的金币
player.setCash(player.getCash() - price);
this.textTip.showTextTip(player, player.getName()
+ " 买下了一块空地.花费了: " + price + "金币. ", 3);
} else {
this.textTip.showTextTip(player, player.getName()
+ " 金币不足,操作失败. ", 3);
}
}
new Thread(new MyThread(run, 1)).start();
}
private void stopInHospital(Building b, PlayerModel player) {
int days = (int) (Math.random() * 4) + 2;
player.setInHospital(days);
int random = (int) (Math.random() * ((Hospital) b).getEvents().length);
String text = ((Hospital) b).getEvents()[random];
this.textTip.showTextTip(player, player.getName() + text + "停留"
+ (days - 1) + "天.", 3);
new Thread(new MyThread(run, 1)).start();
}
public void cardsBuff() {
List<Card>delete = new ArrayList<Card>();
for (Card a : this.run.getNowPlayer().getEffectCards()) {
int buff = a.cardBuff();
cardBuff(a, buff,delete);
}
this.run.getNowPlayer().getEffectCards().removeAll(delete);
this.run.nextState();
}
private void cardBuff(Card card, int buff,List<Card>delete) {
switch (buff) {
case GameState.CARD_BUFF_TORTOISE:
// 乌龟卡BUff
buffTortoiseCard((TortoiseCard) card,delete);
break;
case GameState.CARD_BUFF_STOP:
// 停留卡Buff
buffStopCard(card,delete);
break;
}
}
private void buffStopCard(Card card,List<Card>delete) {
// 增加文本提示
this.textTip.showTextTip(card.geteOwner(), card.geteOwner().getName()
+ " 受\"停留卡\" 作用,不能移动.. ", 2);
// 移除卡片
delete.add(card);
this.run.nextState();
new Thread(new MyThread(run, 1)).start();
}
private void buffTortoiseCard(TortoiseCard card,List<Card>delete) {
if (card.getLife() <= 0) {
delete.add(card);
return;
} else {
card.setLife(card.getLife() - 1);
}
this.textTip.showTextTip(card.geteOwner(), card.geteOwner().getName()
+ " 受\"乌龟卡\" 作用,只能移动一步.. ", 2);
this.run.setPoint(0);
}
public void useCards() {
PlayerModel p = this.run.getNowPlayer();
while (true) {
if (p.getCards().size() == 0) {
// 无卡片,跳过阶段
this.run.nextState();
break;
} else {
Object[] options = new Object[p.getCards().size() + 1];
int i;
for (i = 0; i < p.getCards().size(); i++) {
options[i] = p.getCards().get(i).getcName() + "\r\n";
}
options[i] = "跳过,不使用";
int response = JOptionPane.showOptionDialog(null,
" " + p.getName() + ",选择需要使用的卡片", "卡片使用阶段.",
JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
if (response != i && response != -1) {
// 获得卡片
int th = p.getCards().get(response).useCard();
// 使用卡片
useCard(p.getCards().get(response), th);
} else {
// 不使用,跳过阶段.
this.run.nextState();
break;
}
}
}
}
private void useCard(Card card, int th) {
switch (th) {
case GameState.CARD_ADDLEVEL:
// 使用加盖卡
useAddLevelCard(card);
break;
case GameState.CARD_AVERAGERPOOR:
// 使用均贫卡
useAveragerPoorCard(card);
break;
case GameState.CARD_CHANGE:
// 使用换屋卡
useChangeCard(card);
break;
case GameState.CARD_CONTROLDICE:
// 使用遥控骰子卡
useControlDiceCard(card);
break;
case GameState.CARD_HAVE:
// 使用购地卡
useHaveCard(card);
break;
case GameState.CARD_REDUCELEVEL:
// 使用降级卡
useReduceLevelCard(card);
break;
case GameState.CARD_ROB:
// 使用抢夺卡
useRobCard(card);
break;
case GameState.CARD_STOP:
// 使用停留卡
useStopCard(card);
break;
case GameState.CARD_TALLAGE:
// 使用查税卡
useTallageCard(card);
break;
case GameState.CARD_TORTOISE:
// 使用乌龟卡
useTortoiseCard(card);
break;
case GameState.CARD_TRAP:
// 使用陷害卡
useTrapCard(card);
break;
case GameState.CARD_CROSSING:
// 使用嫁祸卡
useCrossingCard(card);
break;
}
}
private void useCrossingCard(Card card) {
Object[] options1 = { "重新选择" };
JOptionPane.showOptionDialog(null, " 嫁祸卡在大事件发生时会自动使用.",
"卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options1,
options1[0]);
}
private void useTrapCard(Card card) {
Object[] options = { "确认使用", "重新选择" };
int response = JOptionPane.showOptionDialog(null, "确认使用\"陷害卡\"将 \""
+ card.getOwner().getOtherPlayer().getName() + "\"入狱2天?",
"卡片使用阶段.", JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
if (response == 0) {
// 使用
PlayerModel cPlayer = card.getOwner().getOtherPlayer();
// 设置天数
cPlayer.setInPrison(cPlayer.getInPrison() + 2);
// 玩家位置切换到医院位置
if (LandModel.prison != null) {
cPlayer.setX(LandModel.prison.x);
cPlayer.setY(LandModel.prison.y);
}
// 增加文本提示
this.textTip
.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 使用了 \"陷害卡\",将 \""
+ card.getOwner().getOtherPlayer().getName()
+ "\"入狱2天.", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
}
}
private void useTortoiseCard(Card card) {
Object[] options = { card.getOwner().getName(),
card.getOwner().getOtherPlayer().getName(), "重新选择" };
int response = JOptionPane.showOptionDialog(null,
" 请选择目标玩家,对其打出\"乌龟卡\".", "卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (response == 0) {
card.getOwner().getEffectCards().add(card);
card.seteOwner(card.getOwner());
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 对自己使用了\"乌龟卡\". ", 2);
card.getOwner().getCards().remove(card);
} else if (response == 1) {
card.getOwner().getOtherPlayer().getEffectCards().add(card);
card.seteOwner(card.getOwner().getOtherPlayer());
this.textTip.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 对\"" + card.getOwner().getOtherPlayer().getName()
+ "\"使用了\"乌龟卡\". ", 2);
card.getOwner().getCards().remove(card);
}
}
private void useTallageCard(Card card) {
Object[] options = { "确认使用", "重新选择" };
int response = JOptionPane.showOptionDialog(null, "确认使用\"查税卡\"从 \""
+ card.getOwner().getOtherPlayer().getName() + "\"手中获得 10%税款?",
"卡片使用阶段.", JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
if (response == 0) {
// 使用
int money = (int) (card.getOwner().getOtherPlayer().getCash() / 10);
card.getOwner().setCash(card.getOwner().getCash() + money);
card.getOwner()
.getOtherPlayer()
.setCash(card.getOwner().getOtherPlayer().getCash() - money);
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 使用了 \"查税卡\",从 \""
+ card.getOwner().getOtherPlayer().getName()
+ "\"手中获得 10%税款", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
}
}
private void useStopCard(Card card) {
Object[] options = { card.getOwner().getName(),
card.getOwner().getOtherPlayer().getName(), "重新选择" };
int response = JOptionPane.showOptionDialog(null,
" 请选择目标玩家,对其打出\"停留卡\".", "卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (response == 0) {
card.getOwner().getEffectCards().add(card);
card.seteOwner(card.getOwner());
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 对自己使用了\"停留卡\". ", 2);
card.getOwner().getCards().remove(card);
} else if (response == 1) {
card.getOwner().getOtherPlayer().getEffectCards().add(card);
card.seteOwner(card.getOwner().getOtherPlayer());
this.textTip.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 对\"" + card.getOwner().getOtherPlayer().getName()
+ "\"使用了\"停留卡\". ", 2);
card.getOwner().getCards().remove(card);
}
}
private void useRobCard(Card card) {
if (card.getOwner().getCards().size() >= PlayerModel.MAX_CAN_HOLD_CARDS) {
// 无法使用
Object[] options = { "重新选择" };
JOptionPane.showOptionDialog(null, " 您的卡片数量已经达到上限,无法使用\"抢夺卡\"",
"卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
} else if (card.getOwner().getOtherPlayer().getCards().size() == 0) {
// 无法使用
Object[] options = { "重新选择" };
JOptionPane.showOptionDialog(null, " \""
+ card.getOwner().getOtherPlayer().getName()
+ "\"没有卡片,无法使用\"抢夺卡\"", "卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
} else {
PlayerModel srcPlayer = card.getOwner().getOtherPlayer();
// 随机选取一张
// System.out.println(srcPlayer.getCards().size() + "zhang");
Card getCard = srcPlayer.getCards().get((int) (srcPlayer.getCards().size() * Math.random()));
// 对手丧失卡片
srcPlayer.getCards().remove(getCard);
// 卡片拥有者获得
card.getOwner().getCards().add(getCard);
// 更改获得卡片拥有者
getCard.setOwner(card.getOwner());
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 使用了 \"抢夺卡\",抢夺了 \"" + srcPlayer.getName() + "\"的一张\""
+ getCard.getcName() + ".\". ", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
}
}
private void useReduceLevelCard(Card card) {
Building building = this.building.getBuilding(
card.getOwner().getY() / 60, card.getOwner().getX() / 60);
if (building.getOwner() != null
&& building.getOwner().equals(card.getOwner().getOtherPlayer())) {// 是对手的房屋
if (building.getLevel() > 0) { // 可以降级
// 降级
building.setLevel(building.getLevel() - 1);
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner()
.getName()
+ " 使用了 \"降级卡\",将\""
+ card.getOwner().getOtherPlayer().getName()
+ "\"的房屋等级降低一级. ", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
} else {
// 无法使用,不可降级
Object[] options = { "重新选择" };
JOptionPane.showOptionDialog(null, " 当前房屋不可降级", "卡片使用阶段.",
JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
}
} else {
// 无法使用.
Object[] options = { "重新选择" };
JOptionPane.showOptionDialog(null, " 当前房屋不能使用该卡片.", "卡片使用阶段.",
JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, options[0]);
}
}
private void useHaveCard(Card card) {
// 该地点房屋
Building building = this.building.getBuilding(
card.getOwner().getY() / 60, card.getOwner().getX() / 60);
if (building.getOwner() != null
&& building.getOwner().equals(card.getOwner().getOtherPlayer())) {// 是对方的房屋
Object[] options = { "确认使用", "重新选择" };
int response = JOptionPane.showOptionDialog(null,
"确认使用\"购地卡\"将此地收购?需要花费:" + building.getAllPrice() + " 金币.",
"卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (response == 0) {
if (card.getOwner().getCash() >= building.getAllPrice()) {
// 金币交换
building.getOwner().setCash(
building.getOwner().getCash()
+ building.getAllPrice());
card.getOwner().setCash(
card.getOwner().getCash() - building.getAllPrice());
building.setOwner(card.getOwner());
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner()
.getName() + " 使用了 \"购地卡\",收购获得了该土地. ", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
} else {
Object[] options1 = { "重新选择" };
JOptionPane.showOptionDialog(null, " 金币不足,无法购买房屋!",
"卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options1,
options1[0]);
}
}
} else {
Object[] options1 = { "重新选择" };
JOptionPane.showOptionDialog(null, "此房屋无法使用该卡片.", "卡片使用阶段.",
JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options1, options1[0]);
}
}
private void useControlDiceCard(Card card) {
Object[] options = { "1点", "2点", "3点", "4点", "5点", "6点", "重新选择" };
int response = JOptionPane.showOptionDialog(null,
"确认使用\"遥控骰子卡\"遥控骰子点数?", "卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (response == -1 || response == 6) {
return;
} else {
// 使用
this.run.setPoint(response);
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 使用了 \"遥控骰子卡\".", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
}
}
private void useChangeCard(Card card) {
Building building = this.building.getBuilding(
card.getOwner().getY() / 60, card.getOwner().getX() / 60);
if (building.getOwner() != null
&& building.getOwner().equals(card.getOwner().getOtherPlayer())) {// 是对手房屋
Object[] options = { "确认使用", "重新选择" };
int response = JOptionPane.showOptionDialog(null,
"确认使用\"换屋卡\"与对手交换一块同类型的房屋(随机)", "卡片使用阶段.",
JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, options[0]);
if (response == 0) {
// 找寻相等级别房屋
int thisBuildingLevel = building.getLevel();
Building changeBuilding = null;
for (Building a : card.getOwner().getBuildings()) {
if (a.getLevel() == thisBuildingLevel) {
changeBuilding = a;
break;
}
}
// 找到同类型房屋
if (changeBuilding != null) {
changeBuilding.setOwner(card.getOwner().getOtherPlayer());
building.setOwner(card.getOwner());
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner()
.getName()
+ " 使用了 \"换屋卡\",将某处房屋与"
+ card.getOwner().getOtherPlayer().getName()
+ "该地的房屋进行交换.. ", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
} else {
Object[] options1 = { "重新选择" };
JOptionPane.showOptionDialog(null, " 当前房屋不可使用\"换屋卡\"",
"卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options1,
options1[0]);
}
}
} else {
Object[] options = { "重新选择" };
JOptionPane.showOptionDialog(null, " 当前房屋不可使用\"换屋卡\"", "卡片使用阶段.",
JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, options[0]);
}
}
private void useAveragerPoorCard(Card card) {
Object[] options = { "确认使用", "重新选择" };
int response = JOptionPane.showOptionDialog(null,
"确认使用\"均贫卡\"与对手平分现金?", "卡片使用阶段.", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (response == 0) {
// 使用
int money = (int) (card.getOwner().getCash() + card.getOwner()
.getOtherPlayer().getCash()) / 2;
card.getOwner().setCash(money);
card.getOwner().getOtherPlayer().setCash(money);
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner().getName()
+ " 使用了 \"均贫卡\",与对手平分了现金,现在双方现金数为:" + money + " 金币. ", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
}
}
private void useAddLevelCard(Card card) {
Building building = this.building.getBuilding(
card.getOwner().getY() / 60, card.getOwner().getX() / 60);
if (building.getOwner() != null
&& building.getOwner().equals(card.getOwner())) {// 是自己的房屋
if (building.canUpLevel()) { // 可升级
// 升级
building.setLevel(building.getLevel() + 1);
// 增加文本提示
this.textTip.showTextTip(card.getOwner(), card.getOwner()
.getName() + " 使用了 \"加盖卡\",将房屋等级提升一级. ", 2);
// 减去卡片
card.getOwner().getCards().remove(card);
} else {
// 无法使用,不可升级
Object[] options = { "重新选择" };
JOptionPane.showOptionDialog(null, " 当前房屋不可升级.", "卡片使用阶段.",
JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
}
} else {
// 无法使用.
Object[] options = { "重新选择" };
JOptionPane.showOptionDialog(null, " 当前房屋不能使用该卡片.", "卡片使用阶段.",
JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, options[0]);
}
}
public void exitShop() {
new Thread(new MyThread(run, 1)).start();
}
public void buyCard(Shop_ shop) {
int chooseCard = this.panel.getShop().getChooseCard();
if (chooseCard >= 0
&& this.panel.getShop().getCard().get(chooseCard) != null) {
// 购买卡片 如果购买成功
if (this.buyCard(shop, chooseCard)) {
// UI消去卡片
this.panel.getShop().getCard().get(chooseCard).setEnabled(false);
// 初始化已选卡片
this.panel.getShop().setChooseCard(-1);
}
}
}
public boolean buyCard(Shop_ shop, int p) {
if (this.panel.getShop().getCard().get(p) != null) {
if (this.run.getNowPlayer().getCards().size() >= PlayerModel.MAX_CAN_HOLD_CARDS) {
JOptionPane.showMessageDialog(null, "您最大可持有:"
+ PlayerModel.MAX_CAN_HOLD_CARDS + "张卡片,目前已经不能再购买了!");
return false;
}
if (this.run.getNowPlayer().getNx() < shop.getCards().get(p)
.getPrice()) {
JOptionPane.showMessageDialog(null, "当前卡片需要:"
+ shop.getCards().get(p).getPrice() + "点卷,您的点卷不足.");
return false;
}
// 设置卡片拥有者
shop.getCards().get(p).setOwner(this.run.getNowPlayer());
// 向玩家卡片库中添加卡片
this.run.getNowPlayer().getCards().add(shop.getCards().get(p));
// 减去对应点卷
this.run.getNowPlayer().setNx(
this.run.getNowPlayer().getNx()
- shop.getCards().get(p).getPrice());
}
return true;
}
public void gameOver () {
this.run.setNowPlayerState(GameRunning.GAME_STOP);
this.panel.getBackgroundUI().moveToFront();
this.panel.getRunning().moveToFront();
this.panel.getPlayerInfo().moveToFront();
this.panel.getEffect().moveToFront();
this.music.gameOver();
this.effect.showImg("timeover2");
}
}
游戏运转处理
public class GameRunning {
private List<PlayerModel> players = null;
private PlayerModel nowPlayer = null;
private int point;
public static int STATE_CARD = 1;
public static int STATE_CARD_EFFECT = 2;
public static int STATE_THROWDICE = 3;
public static int STATE_MOVE = 4;
public static int GAME_STOP = 5;
private int nowPlayerState;
public static int day = 1;
public static int MAP = 1;
public static int GAME_DAY = -1;
public static int MONEY_MAX = -1;
public static int PLAYER_CASH = 1000;
private Control control;
public GameRunning(Control control, List<PlayerModel> players) {
this.control = control;
this.players = players;
}
public int getNowPlayerState() {
return this.nowPlayerState;
}
public void nextState() {
// 判断游戏是否得出结果
if (gameContinue()) {
if (this.nowPlayerState == STATE_CARD) {
// “掷点状态”
this.nowPlayerState = STATE_CARD_EFFECT;
// 卡片BUFF
this.control.cardsBuff();
} else if (this.nowPlayerState == STATE_CARD_EFFECT) {
// “卡片生效状态”
this.nowPlayerState = STATE_THROWDICE;
} else if (this.nowPlayerState == STATE_THROWDICE) {
// 移动状态
this.nowPlayerState = STATE_MOVE;
} else if (this.nowPlayerState == STATE_MOVE) {
// 换人操作
this.nowPlayerState = STATE_CARD;
this.nextPlayer();
// 产生一个点数
this.setPoint((int) (Math.random() * 6));
// 完毕后执行下一个玩家的动作 - STATE_CARD
this.control.useCards();
}
}
}
public PlayerModel getNowPlayer() {
return this.nowPlayer;
}
public void setNowPlayerState(int nowPlayerState) {
this.nowPlayerState = nowPlayerState;
}
public PlayerModel getNotNowPlayer() {
return this.nowPlayer.equals(this.players.get(0)) ? this.players.get(1)
: this.players.get(0);
}
private void nextPlayer() {
// 减少时间
if (this.nowPlayer.getInPrison() > 0) {
this.nowPlayer.setInPrison(this.nowPlayer.getInPrison() - 1);
}
if (this.nowPlayer.getInHospital() > 0) {
this.nowPlayer.setInHospital(this.nowPlayer.getInHospital() - 1);
}
// 换人
if (this.nowPlayer.equals(this.players.get(0))) {
this.nowPlayer = this.players.get(1);
} else {
this.nowPlayer = this.players.get(0);
// 结束后游戏天数增加
day++;
}
}
public boolean gameContinue() {
PlayerModel p1 = this.nowPlayer;
PlayerModel p2 = this.nowPlayer.getOtherPlayer();
// 天数
if (GAME_DAY > 0 && day >= GAME_DAY) {
this.control.gameOver();
return false;
}
// 最大金钱
if (MONEY_MAX > 0 && p1.getCash() >= MONEY_MAX) {
this.control.gameOver();
return false;
} else if (MONEY_MAX > 0 && p2.getCash() >= MONEY_MAX) {
this.control.gameOver();
return false;
}
// 破产
if (p1.getCash() < 0) {
this.control.gameOver();
return false;
} else if (p2.getCash() < 0) {
this.control.gameOver();
return false;
}
return true;
}
public void setPlayers(List<PlayerModel> players) {
this.players = players;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public int getDay() {
return day;
}
public void startGameInit() {
// 设定当前游戏玩家
this.nowPlayer = this.players.get(0);
// 设定当前玩家状态为“使用卡片”
this.nowPlayerState = STATE_CARD;
// 随机设定点数
this.setPoint((int) (Math.random() * 6));
// 首个玩家使用卡片
this.control.useCards();
}
}
总结
通过此次的《大富翁》游戏实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。
java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。
以上就是Java实现经典大富翁游戏的示例详解的详细内容,更多关于Java大富翁游戏的资料请关注编程网其它相关文章!