




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、蜘蛛紙牌2015-5-122015-5-122015-5-12源程序代碼分析四個(gè)部分組成Spider.javaSpiderMenuBar.javaPKCard.javaAboutDialog.java功能區(qū)功能區(qū)開(kāi)局開(kāi)局: :進(jìn)入游戲后進(jìn)入游戲后, ,單擊開(kāi)局單擊開(kāi)局, ,游戲正游戲正式開(kāi)始式開(kāi)始. .這時(shí)發(fā)牌這時(shí)發(fā)牌區(qū)會(huì)自動(dòng)發(fā)區(qū)會(huì)自動(dòng)發(fā)1010張牌張牌疊加在牌面顯示區(qū)疊加在牌面顯示區(qū)中的牌上中的牌上. .發(fā)牌發(fā)牌: :單擊發(fā)牌后單擊發(fā)牌后, ,發(fā)牌區(qū)向牌面顯示發(fā)牌區(qū)向牌面顯示區(qū)發(fā)區(qū)發(fā)1010張牌張牌. .1.SpiderMenuBar.java功能:SpiderMenuBar.java代碼i
2、mport javax.swing.JMenuBar;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JRadioButtonMenuItem;import javax.swing.ButtonGroup;public class SpiderMenuBar extends JMenuBar Spider main = null;/生成spider框架對(duì)象 /生成菜單組 JMenu jNewGame = new JMenu(游戲); JMenu jHelp = new JMenu(幫助); /生成
3、菜單項(xiàng) JMenuItem jItemAbout = new JMenuItem(關(guān)于); JMenuItem jItemOpen = new JMenuItem(開(kāi)局); JMenuItem jItemPlayAgain = new JMenuItem(重新發(fā)牌);JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem(簡(jiǎn)單:?jiǎn)我换ㄉ?; JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem(中級(jí):雙花色); JRadioButtonMenuItem jRMI
4、temHard = new JRadioButtonMenuItem(高級(jí):四花色); JMenuItem jItemExit = new JMenuItem(退出); JMenuItem jItemValid = new JMenuItem(顯示可行操作); /*構(gòu)造函數(shù),生成JMenuBar的圖形界面*/ public SpiderMenuBar(Spider spider) this.main = spider; /*初始化“游戲”菜單欄*/ jNewGame.add(jItemOpen);jNewGame.add(jItemPlayAgain); jNewGame.add(jItemV
5、alid); jNewGame.addSeparator(); /添加劃分線 jNewGame.add(jRMItemEasy);jNewGame.add(jRMItemNormal); jNewGame.add(jRMItemHard); jNewGame.addSeparator(); jNewGame.add(jItemExit); ButtonGroup group = new ButtonGroup(); /為一個(gè)按鈕創(chuàng)建一個(gè)互斥作用域,當(dāng)“選中”一個(gè)按鈕事,講關(guān)閉其他所有的按鈕 group.add(jRMItemEasy);group.add(jRMItemNormal); gro
6、up.add(jRMItemHard); jHelp.add(jItemAbout); this.add(jNewGame);this.add(jHelp); /為組件添加事件監(jiān)聽(tīng),點(diǎn)擊菜單觸發(fā)相應(yīng)的方法 jItemOpen.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) main.newGame(); );jItemPlayAgain.addActionListener(new java.awt.event.Actio
7、nListener() public void actionPerformed(java.awt.event.ActionEvent e) if(main.getC() 60)main.deal(); );jItemValid.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) new Show().start(); );jItemExit.addActionListener(new java.awt.event.Actio
8、nListener() public void actionPerformed(java.awt.event.ActionEvent e) main.dispose(); System.exit(0); ); jRMItemEasy.setSelected(true); /默認(rèn)選上“簡(jiǎn)單級(jí)別”jRMItemEasy.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) main.setGrade(Spider.EASY); m
9、ain.initCards() ; main.newGame(); );jRMItemNormal.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) main.setGrade(Spider.NATURAL); main.initCards(); main.newGame(););jRMItemHard.addActionListener(new java.awt.event.ActionListener() public
10、 void actionPerformed(java.awt.event.ActionEvent e) main.setGrade(Spider.HARD); main.initCards(); main.newGame(); );jNewGame.addMenuListener(new javax.swing.event.MenuListener() public void menuSelected(javax.swing.event.MenuEvent e) if(main.getC() 60)jItemPlayAgain.setEnabled(true); elsejItemPlayAg
11、ain.setEnabled(false); public void menuDeselected(javax.swing.event.MenuEvent e) public void menuCanceled(javax.swing.event.MenuEvent e) );jItemAbout.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) new AboutDialog(); ); /構(gòu)造名為show的Thread
12、線程:顯示可以執(zhí)行的操作 class Show extends Thread public void run()main.showEnableOperator(); 2015-5-122.PKCard.java2.PKCard.java功能:功能:黑桃8和黑桃9會(huì)閃爍一下,即一張顯示牌本身,一張顯示“白牌”P(pán)KCard.java代碼import java.awt.*;import java.awt.event.*;import javax.swing.*;/創(chuàng)建名為PKCard的標(biāo)簽類(lèi)public class PKCard extends JLabel implements MouseList
13、ener,MouseMotionListener /紙牌的位置 Point point = null; Point initPoint = null; int value = 0; int type= 0; String name = null; Container pane = null; /標(biāo)簽主容器為空 Spider main = null;boolean canMove = false; boolean isFront = false; /紙牌為背面顯示 PKCard previousCard = null; /表示上面一張紙牌為空 public void mouseClicked(M
14、ouseEvent arg0) /啟動(dòng)flash線程,不斷的獲取下一張牌,直到完成 public void flashCard(PKCard card) new Flash(card).start(); if(main.getNextCard(card) != null) card.flashCard(main.getNextCard(card); class Flash extends Thread /構(gòu)造名為Flash的Thread線程 private PKCard card = null; public Flash(PKCard card)this.card = card; /線程的ru
15、n方法,提示玩家有可行性操作 public void run() boolean is = false;ImageIcon icon = new ImageIcon(images/white.gif);for (int i = 0; i 30 & b 71) a+; else if (b 71) a = -1; if (a != -1)Point p = main.getLastCardLocation(a); if (p = null) p = main.getGroundLabelLocation(a); b = y - p.y; if (b = 96)a = -1; return
16、 a; public void mouseEntered(MouseEvent arg0)public void mouseExited(MouseEvent arg0) /用鼠標(biāo)拖動(dòng)紙牌事件的處理方法public void mouseDragged(MouseEvent arg0)if (canMove)int x = 0; int y = 0; Point p = arg0.getPoint();x = p.x - point.x; y = p.y - point.y; this.moving(x, y); /紙牌移動(dòng)(x,y)個(gè)位置public void moving(int x, in
17、t y) PKCard card = main.getNextCard(this); Point p = this.getLocation(); /將組件移動(dòng)到容器中指定的順序索引。 pane.setComponentZOrder(this, 1); main.table.remove(p); p.x += x; p.y += y; this.setLocation(p); main.table.put(p, this); if (card != null) card.moving(x, y); public void mouseMoved(MouseEvent arg0)/構(gòu)造函數(shù)publi
18、c PKCard(String name, Spider spider) super(); this.type = new Integer(name.substring(0, 1).intValue(); this.value = new Integer(name.substring(2).intValue(); = name; this.main = spider; this.pane = this.main.getContentPane(); this.addMouseListener(this); this.addMouseMotionListener(this);
19、this.setIcon(new ImageIcon(images/rear.gif); this.setSize(71, 96); this.setVisible(true); /令紙牌顯示正面public void turnFront() this.setIcon(new ImageIcon(images/ + name + .gif); this.isFront = true; /令紙牌顯示背面public void turnRear() this.setIcon(new ImageIcon(images/rear.gif); this.isFront = false; this.can
20、Move = false; /將紙牌移動(dòng)到點(diǎn)pointpublic void moveto(Point point) this.setLocation(point); this.initPoint = point; /判斷牌是否能移動(dòng)public void setCanMove(boolean can) this.canMove = can; PKCard card = main.getPreviousCard(this); if (card != null & card.isCardFront() if (!can)if (!card.isCardCanMove()return; e
21、lsecard.setCanMove(can); elseif (this.value + 1 = card.getCardValue() & this.type = card.getCardType()card.setCanMove(can);elsecard.setCanMove(false); /判斷紙牌是否是正面public boolean isCardFront()return this.isFront; public boolean isCardCanMove()return this.canMove; public int getCardValue()return val
22、ue; public int getCardType()return type; 3.AboutDialog.java3.AboutDialog.java功能:功能:?jiǎn)螕魡螕簟皫椭鷰椭薄啊薄瓣P(guān)于關(guān)于”單擊“聲明”AboutDialog.javaAboutDialog.java代碼代碼import javax.swing.*;import java.awt.*;/*“關(guān)于”窗口 */public class AboutDialog extends JDialog/創(chuàng)建對(duì)話框窗口類(lèi)JPanel jMainPane = new JPanel();JTabbedPane jTabbedPane =
23、new JTabbedPane();private JPanel jPanel1 = new JPanel();private JPanel jPanel2 = new JPanel();private JTextArea jt1 = new JTextArea(將電腦多次分發(fā)給你的牌按照相同的花色由大至小排列起來(lái)。直到桌面上的牌全都消失。); private JTextArea jt2 = new JTextArea(小玩怡情,大玩?zhèn)恚?; /*構(gòu)造函數(shù)*/Public AboutDialog()setTitle(蜘蛛牌);setSize(300,200);setResizable(fal
24、se);setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE); Container c = this.getContentPane();jt1.setSize(260,200);jt2.setSize(260,200);jt1.setEditable(false);jt2.setEditable(false);/文本能否編輯jt1.setLineWrap(true); jt2.setLineWrap(true); /字自動(dòng)換行jt1.setFont(new Font(楷體_GB2312, java.awt.Font.BOLD,
25、13);jt1.setForeground(Color.blue);jt2.setFont(new Font(楷體_GB2312, java.awt.Font.BOLD, 13);jt2.setForeground(Color.black);jPanel1.add(jt1);jPanel2.add(jt2);jTabbedPane.setSize(300,200);jTabbedPane.addTab(游戲規(guī)則, null, jPanel1, null);jTabbedPane.addTab(聲明, null, jPanel2, null);/選項(xiàng)卡顯示標(biāo)題;顯示圖標(biāo);顯示組件;工具提示jMa
26、inPane.add(jTabbedPane);c.add(jMainPane);pack();/調(diào)整窗口大小,以適合組件的大小和布局this.setVisible(true); 2015-5-124.Spider.java4.Spider.java功能:功能:簡(jiǎn)單:?jiǎn)我换ㄉ?jiǎn)單:?jiǎn)我换ㄉ呒?jí):四花色高級(jí):四花色中級(jí):雙花色中級(jí):雙花色異常及成功處異常及成功處理理Spider.javaSpider.java代碼代碼import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;public cl
27、ass Spider extends JFrame/整型變量,表示難度等級(jí)為:簡(jiǎn)單public statiac final int EASY = 1;public static final int NATURAL = 2;public static final int HARD = 3;private int grade = Spider.EASY; /設(shè)定初始難度等級(jí)為簡(jiǎn)單private Container pane = null; /創(chuàng)建一個(gè)容器對(duì)象/生成紙牌數(shù)組private PKCard cards = new PKCard104;private JLabel clickLabel =
28、 null;/創(chuàng)建右下角發(fā)牌區(qū)的鼠標(biāo)響應(yīng)區(qū)域 private int c = 0; /表示紙牌的數(shù)量 private int n = 0; /紙牌的等級(jí)private int a = 0; /紙牌的所在列號(hào)private int finish = 0; /紙牌從小到大排列成功的次數(shù)Hashtable table = null; private JLabel groundLabel = null;public static void main(String args) Spider spider = new Spider(); spider.setVisible(true); /構(gòu)造函數(shù)publ
29、ic Spider()setTitle(蜘蛛牌); setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);setSize(1024, 742);setJMenuBar(new SpiderMenuBar(this); pane = this.getContentPane(); pane.setBackground(new Color(0, 112, 26); /設(shè)置背景顏色 pane.setLayout(null); clickLabel = new JLabel(); clickLabel.setBounds(883, 606,
30、 121, 96);/設(shè)置響應(yīng)區(qū)的坐標(biāo) pane.add(clickLabel); clickLabel.addMouseListener(new MouseAdapter() public void mouseReleased(MouseEvent me) if (c 60)Spider.this.deal(); ); this.initCards(); this.randomCards(); this.setCardsLocation(); groundLabel = new JLabel10;/創(chuàng)建背景框數(shù)組 int x = 20; for (int i = 0; i 10; i+) g
31、roundLabeli = new JLabel();groundLabeli.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED); groundLabeli.setBounds(x, 25, 71, 96); x += 101; this.pane.add(groundLabeli); this.setVisible(true); this.deal(); this.addKeyListener(new KeyAdapter()class Show ext
32、ends Threadpublic void run() Spider.this.showEnableOperator(); public void keyPressed(KeyEvent e) if (finish != 8) if (e.getKeyCode() = KeyEvent.VK_D & c 60) Spider.this.deal(); else if (e.getKeyCode() = KeyEvent.VK_M) new Show().start(); ); /開(kāi)始新游戲public void newGame() this.randomCards(); this.s
33、etCardsLocation(); this.setGroundLabelZOrder(); this.deal();/返回紙牌的數(shù)量public int getC()return c; /設(shè)置等級(jí)public void setGrade(int grade)this.grade = grade; /紙牌初始化public void initCards() /如果紙牌已被賦值,即將其從框架的面板中移去 if (cards0 != null) for (int i = 0; i 104; i+)pane.remove(cardsi); int n = 0; /通過(guò)難度等級(jí),為n賦值 if (t
34、his.grade = Spider.EASY)n = 1; else if (this.grade = Spider.NATURAL)n = 2; elsen = 4; /為card賦值 for (int i = 1; i = 8; i+) 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; /隨機(jī)生成牌號(hào) for (int i
35、 = 0; i 52; i+) int a = (int) (Math.random() * 104); int b = (int) (Math.random() * 104); temp = cardsa; cardsa = cardsb; cardsb = temp; /還原設(shè)置,下次玩的時(shí)候可以重新選擇等級(jí)public void setNA()a = 0; n = 0;public void setCardsLocation() table = new Hashtable(); c = 0; finish = 0; n = 0; a = 0; int x = 883; int y = 5
36、80;/初始化待展開(kāi)的紙牌 for (int i = 0; i 6; i+) for (int j = 0; j 5; i-) for (int j = 0; j = 104) continue; pane.add(cardsn); cardsn.turnRear(); cardsn.moveto(new Point(x, y); /將紙牌移到固定位置 table.put(new Point(x, y), cardsn); x += 101; x = 20; y -= 5; public void showEnableOperator() int x = 0; out: while (true
37、)Point point = null; PKCard card = null; doif (point != null)n+;point = this.getLastCardLocation(n); while (point = null)point = this.getLastCardLocation(+n); if (n = 10) n = 0;x+; if (x = 10) break out; card = (PKCard) this.table.get(point); while (!card.isCardCanMove(); while (this.getPreviousCard
38、(card) != null& this.getPreviousCard(card).isCardCanMove()card = this.getPreviousCard(card); if (a = 10)a = 0;for (; a 10; a+)if (a != n)Point p = null; PKCard c = null;doif (p != null)a+; p = this.getLastCardLocation(a);int z = 0; while (p = null)p = this.getLastCardLocation(+a); if (a = 10) a
39、= 0; if (a = n) a+; z+; if (z = 10) break out; c = (PKCard) this.table.get(p); while (!c.isCardCanMove();if (c.getCardValue() = card.getCardValue() + 1)card.flashCard(card); tryThread.sleep(800); catch (InterruptedException e)e.printStackTrace(); c.flashCard(c); a+; if (a = 10)n+; break out; n+;if (
40、n = 10)n = 0; x+; if (x = 10)break out; /游戲運(yùn)行,分牌public void deal() this.setNA();for (int i = 0; i 10; i+)if (this.getLastCardLocation(i)=null)JOptionPane.showMessageDialog(this, 有空位不能發(fā)牌!, 提示, JOptionPane.WARNING_MESSAGE);return; int x = 20; for (int i = 0; i 10; i+) Point lastPoint = this.getLastCar
41、dLocation(i); /這張牌應(yīng)“背面向上” if (c = 0)lastPoint.y += 5; /當(dāng)還沒(méi)發(fā)過(guò)紙牌時(shí),每列紙牌y坐標(biāo)相差5個(gè)像素 elselastPoint.y += 20;table.remove(cardsc + i.getLocation(); cardsc + i.moveto(lastPoint); table.put(new Point(lastPoint), cardsc + i);cardsc + i.turnFront();cardsc + i.setCanMove(true); /將組件card移動(dòng)到容器中指定的順序索引。 this.pane.se
42、tComponentZOrder(cardsc + i, 1);Point point = new Point(lastPoint);if (cardsc + i.getCardValue() = 1)int n = cardsc + i.whichColumnAvailable(point);point.y -= 240;PKCard card = (PKCard) this.table.get(point);/判斷紙牌是否可回收,以及回收的次數(shù)if (card != null & card.isCardCanMove()this.haveFinish(n); x += 101; c
43、 += 10; /獲得當(dāng)前紙牌上面的那張紙牌public PKCard getPreviousCard(PKCard card) Point point = new Point(card.getLocation(); point.y -= 5; card = (PKCard) table.get(point); if (card != null)return card; point.y -= 15; card = (PKCard) table.get(point); return card; /獲得當(dāng)前紙牌下面的那張紙牌 public PKCard getNextCard(PKCard card) Point point = new Point(card.getLocation(); point.y += 5; card = (PKCard) table.get(point); if (card !
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 銷(xiāo)售大區(qū)印章管理辦法
- 金融支付機(jī)構(gòu)管理辦法
- 臨汾市培訓(xùn)經(jīng)費(fèi)管理辦法
- 防疫倉(cāng)庫(kù)物資管理辦法
- 醫(yī)保局大廳管理辦法文件
- 麗水市公共場(chǎng)所管理辦法
- 豐澤縣公共場(chǎng)所管理辦法
- 火電廠技術(shù)改造管理辦法
- 單位合并財(cái)務(wù)交接方案
- 礦區(qū)消防施工方案
- 鉀礦項(xiàng)目投資可行性研究分析報(bào)告(2024-2030版)
- 新疆高校畢業(yè)生三支一扶計(jì)劃招募考試真題2025
- 2024年煤礦安全規(guī)程
- 成分輸血專(zhuān)題知識(shí)
- 高壓氧艙技師試題及答案
- 網(wǎng)絡(luò)安全等級(jí)保護(hù)等級(jí)測(cè)評(píng)方案模板
- 2025年1月國(guó)家開(kāi)放大學(xué)專(zhuān)科《辦公室管理》期末紙質(zhì)考試試題及答案
- 康師傅采購(gòu)流程
- 2025年復(fù)合膜袋項(xiàng)目可行性研究報(bào)告
- 離職賠償協(xié)商協(xié)議書(shū)(2025年)
- 2024年度半導(dǎo)體生產(chǎn)工人勞動(dòng)合同范本3篇
評(píng)論
0/150
提交評(píng)論