hooyantsing's Blog

第25次课程

字数统计: 783阅读时长: 4 min
2019/06/19

源辰74班

第25次课程

06.19.2019

**内容    **

** Java_OOP3_2[蒋斌]**

1.[游戏]蜘蛛王扑克牌

此为标准蜘蛛王扑克牌,不含发牌后自动排序

Class Card

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import javax.swing.*;
public class Card extends JLabel{
    private String name;
    private boolean up;
    
    public Card(){
       
    }
    public Card(String name,boolean up) {
       this.name = name;
       this.up = up;
       //设置牌的高度和宽度
       this.setSize(71,96);
       if(up) {
           //正面
           turnFront();
       }else {
           //反面
           turnRear();
       }
    }
    //正面贴图
    public void turnFront() {
       this.setIcon(new  ImageIcon("images/"+name+".gif"));
    }
    //反面贴图
    public void turnRear() {
       this.setIcon(new  ImageIcon("images/rear.gif"));
    }
}

**Class **CardsContair

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//单个玩家
public class CardsContair {
    private Card[] c;
    private int index;
    
    public CardsContair(){
       c = new Card[10];
    }
    
    public void add(Card card) {
       if(index>=c.length) {
           //数组扩容
           Card[] newC = new  Card[c.length*2];
           for(int i=0;i<c.length;i++) {
               newC[i] = c[i];
           }
           c = newC;
       }
       c[index] = card;
       index++;
    }
    
    public int getSize() {
       return index;
    }
}

**Class **Common

制作移动效果时,可以借鉴move方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.awt.Point;
public class Common {
    //移动效果方法,用于发牌
    public static void move(Card card,Point  from,Point to) {
       if(to.x != from.x) {
           double k = (1.0) * (to.y -  from.y) / (to.x - from.x);
           double b = to.y - to.x * k;
           int flag = 0;
           if(from.x < to.x) {
               flag = 10;
           }else {
               flag = -10;
           }
           for(int  i=from.x;Math.abs(i-to.x)>20;i+=flag) {
               double y = k * i + b;
               card.setLocation(i,(int)y);
               try {
                  //延迟3s 完成
                  Thread.sleep(3);
               }catch(InterruptedException  e) {
                  e.printStackTrace();
               }
           }
       }
       //位置校准
       card.setLocation(to);
    }
    //排序
}

Class Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.awt.*;
import java.util.Random;

import javax.sql.CommonDataSource;
import javax.swing.*;

public class Main extends JFrame{
    private Container contentPane;
    private Card[] cards = new Card[56];
    //创建三个玩家
    private CardsContair[] playContair = new CardsContair[3];
    //预留地主牌
    private CardsContair landLord = new CardsContair();
    
    public void start() {
        init();
        cardInit();
    }
    
    public void init(){
        //设置窗口大小
        this.setSize(830, 630);
        //关闭窗口即终止程序
        this.setDefaultCloseOperation(3);
        //窗口居中
        this.setLocationRelativeTo(null);
        //空布局
        this.setLayout(null);
        //初始化容器
        contentPane = this.getContentPane();
        //设置容器背景颜色(R,G,B)
        contentPane.setBackground(new Color(0,112,26));    
        //窗口显示(非隐藏)
        this.setVisible(true);
    }
    
    public void cardInit() {
        int count = 1;
        for(int i=1;i<=5;i++) {
            for(int j=1;j<=13;j++) {
                if(i==5&&j>=3) {
                    break;
                }else {
                    cards[count] = new Card(i+"-"+j,false);
                    //控件的位置
                    cards[count].setLocation(300+(i*20), 50);
                    //将cards[count]控件添加到contentPane容器上
                    contentPane.add(cards[count]);
                    count++;
                }
            }
        }
        //洗牌
        Random r = new Random();
        for(int i=0;i<1000;i++) {
            int a = r.nextInt(53)+1;
            int b = r.nextInt(53)+1;
            Card temp = cards[a];
            cards[a] = cards[b];
            cards[b] = temp;
        }
        for(int i=0;i<3;i++) {
            playContair[i] = new CardsContair();
        }
        //发牌
        
        for(int i=1;i<=54;i++) {
            if(i>=52) {
                landLord.add(cards[i]);
                Common.move(cards[i], cards[i].getLocation(), new Point(300+(i-52)*80,16));
                continue;
            }
            switch((i-1)%3) {
                case 0:
                    Common.move(cards[i], cards[i].getLocation(), new Point(50,60+i*5));
                    playContair[0].add(cards[i]);
                    cards[i].turnFront();
                    break;
                case 1:
                    Common.move(cards[i], cards[i].getLocation(), new Point(180+i*7,450));
                    playContair[1].add(cards[i]);
                    cards[i].turnFront();
                    break;
                case 2:
                    Common.move(cards[i], cards[i].getLocation(), new Point(700,60+i*5));
                    playContair[2].add(cards[i]);
                    cards[i].turnFront();
                    break;
            }
        //牌组整理
        contentPane.setComponentZOrder(cards[i], 0);    
        }    
    }
}

Main Test

1
2
3
4
5
public class Test {
    public static void main(String[] args) {
       new Main().start();;
    }
}

效果图:

a115b9605ea34946752c32ff818da9cc.png

CATALOG