课程设计说明书 NO.6
图3 显示菜单 通过如下代码实现: JMenu jNewGame = new JMenu(\选项\ JMenu jHelp = new JMenu(\帮助\(2)在选项菜单下面包含7级子菜单,如图4所示: 图4 显示菜单
沈 阳 大 学 课程设计说明书 NO.7
通过如下代码实现: JMenuItem jItemOpen = new JMenuItem(\开局\JMenuItem jItemPlayAgain = new JMenuItem(\重发牌\JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem(\简单\JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem(\较难\JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem(\困难\JMenuItem jItemExit = new JMenuItem(\退出\JMenuItem jItemValid = new JMenuItem(\显示可执行行操作\(3)帮助下面包含2级子菜单,分别为游戏规则和声明,如图5所示: 图5 显示帮助 通过如下代码实现: JTabbedPane jTabbedPane = new JTabbedPane(); private JPanel jPanel1 = new JPanel(); private JPanel jPanel2 = new JPanel(); (4)主窗体通过类Spider实现。将窗体名称设置为“陶时扑克”,框架的大小设置为1024*742,背景颜色设置为黑色,布局管理设置为空,通过如下代码实现:
沈 阳 大 学 课程设计说明书 NO.8
public Spider() { setTitle(\陶时扑克\setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); setSize(1024, 742); setJMenuBar(new SpiderMenuBar(this)); pane = this.getContentPane(); } (5)进入游戏之后,首先选择开始新游戏,通过类Spider调用它的方法newGame方法,采用随机函数随机初始化牌的顺序(这样做的目的是,使游戏性增加可玩性,使每次出现牌的顺序不同),如图6所示。 pane.setBackground(new Color(14, 25, 26)); pane.setLayout(null); 图6 进入新游戏界面 用如下代码实现: public void newGame(){ this.randomCards(); this.setCardsLocation(); this.setGroundLabelZOrder(); this.deal(); } public int getC(){ return c; } public void setGrade(int grade){ this.grade = grade; } public void initCards(){ if (cards[0] != null){ for (int i = 0; i < 104; i++){ pane.remove(cards[i]); } } int n = 0; if (this.grade == Spider.EASY){ n = 1; } else if (this.grade == Spider.NATURAL){ n = 2; } else{ n = 4; } for (int i = 1; i <= 8; i++) {
沈 阳 大 学 课程设计说明书 NO.9
for (int j = 1; j <= 13; j++){ cards[(i - 1) * 13 + j - 1] = new PKCard((i % n + 1) + \+ j, this); } } this.randomCards(); } public void randomCards(){ PKCard temp = null; for (int i = 0; i < 52; i++){ int a = (int) (Math.random() * 104); int b = (int) (Math.random() * 104); temp = cards[a]; cards[a] = cards[b]; cards[b] = temp; } } (6)在游戏界面的右下角做一张图片,显示扑克牌的背面,同时点击图片,系统自动发牌(当游戏无法继续的时候,可以点击该图片,在每副纸牌上发一张牌,使得进入僵局的纸牌游戏得以继续进行),运行界面如图7,图8所示。
沈 阳 大 学