Java实现经典大富翁游戏的示例详解
目录
- 前言
- 主要设计
- 功能截图
- 代码实现
- 总结
前言
大富翁,又名地产大亨。是一种多人策略图版游戏。参与者分得游戏金钱,凭运气(掷骰子)及交易策略,买地、建楼以赚取租金。英文原名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; /** * 人物1 **/ private JLabel jlPlayer01Choose = null; private final JLabel jlPlayer01Selected = new JLabel( Photo.PLAYER_01_SELECTED); private JButton leftButton01; private JButton rightButton01; /** * 人物2 **/ private JLabel jlPlayer02Choose = null; private final JLabel jlPlayer02Selected = new JLabel( Photo.PLAYER_02_SELECTED); private JButton leftButton02; private JButton rightButton02; /** * 1P 2P可选人物 */ private int[] chooses = { 0, 0 }; /** * 1P 2P已选人物 */ private int[] selected = { -1, -2 }; /** * 1P 2P已填名字 */ 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); } /** * 增加1P面板 */ 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); } /** * 增加2P面板 */ 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; } }
游戏总控制器
/** * * 游戏总控制器 * * * @author Administrator * */ public class Control { /** * * 游戏tick值 * */ 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; } } /** * * 停留卡Buff * * */ 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(); } /** * * 乌龟卡BUff * */ 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; } /** * * 游戏结束~ * * * @param winer */ 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"); } }
游戏运转处理
/** * * 游戏运转处理 * * @author MOVELIGHTS * */ 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; /** * * 游戏上限天数 - 1为无上限 * */ public static int GAME_DAY = -1; /** * * 游戏金钱上线(即胜利条件)-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大富翁游戏的资料请关注我们其它相关文章!
赞 (0)