蜘蛛紙牌演示教學_第1頁
蜘蛛紙牌演示教學_第2頁
蜘蛛紙牌演示教學_第3頁
蜘蛛紙牌演示教學_第4頁
蜘蛛紙牌演示教學_第5頁
已閱讀5頁,還剩19頁未讀, 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、蜘蛛紙牌2015-5-122015-5-122015-5-12源程序代碼分析四個部分組成Spider.javaSpiderMenuBar.javaPKCard.javaAboutDialog.java功能區功能區開局開局: :進入游戲后進入游戲后, ,單擊開局單擊開局, ,游戲正游戲正式開始式開始. .這時發牌這時發牌區會自動發區會自動發1010張牌張牌疊加在牌面顯示區疊加在牌面顯示區中的牌上中的牌上. .發牌發牌: :單擊發牌后單擊發牌后, ,發牌區向牌面顯示發牌區向牌面顯示區發區發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框架對象 /生成菜單組 JMenu jNewGame = new JMenu(游戲); JMenu jHelp = new JMenu(幫助); /生成

3、菜單項 JMenuItem jItemAbout = new JMenuItem(關于); JMenuItem jItemOpen = new JMenuItem(開局); JMenuItem jItemPlayAgain = new JMenuItem(重新發牌);JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem(簡單:單一花色); JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem(中級:雙花色); JRadioButtonMenuItem jRMI

4、temHard = new JRadioButtonMenuItem(高級:四花色); JMenuItem jItemExit = new JMenuItem(退出); JMenuItem jItemValid = new JMenuItem(顯示可行操作); /*構造函數,生成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(); /為一個按鈕創建一個互斥作用域,當“選中”一個按鈕事,講關閉其他所有的按鈕 group.add(jRMItemEasy);group.add(jRMItemNormal); gro

6、up.add(jRMItemHard); jHelp.add(jItemAbout); this.add(jNewGame);this.add(jHelp); /為組件添加事件監聽,點擊菜單觸發相應的方法 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); /默認選上“簡單級別”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(); ); /構造名為show的Thread

12、線程:顯示可以執行的操作 class Show extends Thread public void run()main.showEnableOperator(); 2015-5-122.PKCard.java2.PKCard.java功能:功能:黑桃8和黑桃9會閃爍一下,即一張顯示牌本身,一張顯示“白牌”PKCard.java代碼import java.awt.*;import java.awt.event.*;import javax.swing.*;/創建名為PKCard的標簽類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; /標簽主容器為空 Spider main = null;boolean canMove = false; boolean isFront = false; /紙牌為背面顯示 PKCard previousCard = null; /表示上面一張紙牌為空 public void mouseClicked(M

14、ouseEvent arg0) /啟動flash線程,不斷的獲取下一張牌,直到完成 public void flashCard(PKCard card) new Flash(card).start(); if(main.getNextCard(card) != null) card.flashCard(main.getNextCard(card); class Flash extends Thread /構造名為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) /用鼠標拖動紙牌事件的處理方法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); /紙牌移動(x,y)個位置public void moving(int x, in

17、t y) PKCard card = main.getNextCard(this); Point p = this.getLocation(); /將組件移動到容器中指定的順序索引。 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)/構造函數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; /將紙牌移動到點pointpublic void moveto(Point point) this.setLocation(point); this.initPoint = point; /判斷牌是否能移動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功能:功能:單擊單擊“幫助幫助”“”“關于關于”單擊“聲明”AboutDialog.javaAboutDialog.java代碼代碼import javax.swing.*;import java.awt.*;/*“關于”窗口 */public class AboutDialog extends JDialog/創建對話框窗口類JPanel jMainPane = new JPanel();JTabbedPane jTabbedPane =

23、new JTabbedPane();private JPanel jPanel1 = new JPanel();private JPanel jPanel2 = new JPanel();private JTextArea jt1 = new JTextArea(將電腦多次分發給你的牌按照相同的花色由大至小排列起來。直到桌面上的牌全都消失。); private JTextArea jt2 = new JTextArea(小玩怡情,大玩傷身!); /*構造函數*/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); /字自動換行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(游戲規則, null, jPanel1, null);jTabbedPane.addTab(聲明, null, jPanel2, null);/選項卡顯示標題;顯示圖標;顯示組件;工具提示jMa

26、inPane.add(jTabbedPane);c.add(jMainPane);pack();/調整窗口大小,以適合組件的大小和布局this.setVisible(true); 2015-5-124.Spider.java4.Spider.java功能:功能:簡單:單一花色簡單:單一花色高級:四花色高級:四花色中級:雙花色中級:雙花色異常及成功處異常及成功處理理Spider.javaSpider.java代碼代碼import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;public cl

27、ass Spider extends JFrame/整型變量,表示難度等級為:簡單public statiac final int EASY = 1;public static final int NATURAL = 2;public static final int HARD = 3;private int grade = Spider.EASY; /設定初始難度等級為簡單private Container pane = null; /創建一個容器對象/生成紙牌數組private PKCard cards = new PKCard104;private JLabel clickLabel =

28、 null;/創建右下角發牌區的鼠標響應區域 private int c = 0; /表示紙牌的數量 private int n = 0; /紙牌的等級private int a = 0; /紙牌的所在列號private int finish = 0; /紙牌從小到大排列成功的次數Hashtable table = null; private JLabel groundLabel = null;public static void main(String args) Spider spider = new Spider(); spider.setVisible(true); /構造函數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); /設置背景顏色 pane.setLayout(null); clickLabel = new JLabel(); clickLabel.setBounds(883, 606,

30、 121, 96);/設置響應區的坐標 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;/創建背景框數組 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(); ); /開始新游戲public void newGame() this.randomCards(); this.s

33、etCardsLocation(); this.setGroundLabelZOrder(); this.deal();/返回紙牌的數量public int getC()return c; /設置等級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; /通過難度等級,為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; /隨機生成牌號 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; /還原設置,下次玩的時候可以重新選擇等級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;/初始化待展開的紙牌 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; /游戲運行,分牌public void deal() this.setNA();for (int i = 0; i 10; i+)if (this.getLastCardLocation(i)=null)JOptionPane.showMessageDialog(this, 有空位不能發牌!, 提示, JOptionPane.WARNING_MESSAGE);return; int x = 20; for (int i = 0; i 10; i+) Point lastPoint = this.getLastCar

41、dLocation(i); /這張牌應“背面向上” if (c = 0)lastPoint.y += 5; /當還沒發過紙牌時,每列紙牌y坐標相差5個像素 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移動到容器中指定的順序索引。 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);/判斷紙牌是否可回收,以及回收的次數if (card != null & card.isCardCanMove()this.haveFinish(n); x += 101; c

43、 += 10; /獲得當前紙牌上面的那張紙牌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; /獲得當前紙牌下面的那張紙牌 public PKCard getNextCard(PKCard card) Point point = new Point(card.getLocation(); point.y += 5; card = (PKCard) table.get(point); if (card !

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論