在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Java游戏开发 → JavaSwing的斗地主(小游戏源码)

JavaSwing的斗地主(小游戏源码)

Java游戏开发

下载此实例
  • 开发语言:Java
  • 实例大小:0.09M
  • 下载次数:67
  • 浏览次数:1884
  • 发布时间:2019-05-16
  • 实例类别:Java游戏开发
  • 发 布 人:conanclement
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 游戏 斗地主 swing

实例介绍

【实例简介】

【实例截图】

from clipboard


from clipboard


【核心代码】

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com;
 
import java.awt.Color;
import java.awt.Container;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
 
public class Main extends JFrame implements ActionListener {
 
    public Container container = null;// 定义容器
    JMenuItem start, exit, about;// 定义菜单按钮
    JButton landlord[]=new JButton[2];//抢地主按钮
    JButton publishCard[]=new JButton[2];//出牌按钮
    int dizhuFlag;//地主标志
    int turn;
    JLabel dizhu; //地主图标
    List<Card> currentList[] =new ArrayList[3]; //  当前的出牌
    List<Card> playerList[] = new ArrayList[3]; // 定义3个玩家表
    List<Card> lordList;//地主牌
    Card card[] = new Card[56]; // 定义54张牌
    JTextField time[]=new JTextField[3]; //计时器
    Time t; //定时器(线程)
    boolean nextPlayer=false; //转换角色
    public Main(){
 
        Init();// 初始化
        SetMenu();// 创建菜单 按钮(抢地主,发牌,计时器)
        this.setVisible(true);
        CardInit();//发牌
     
        getLord(); //发完牌开始抢地主
        time[1].setVisible(true);
        //线程安全性,把非主线程的UI控制放到里面
        SwingUtilities.invokeLater(new NewTimer(this,10));
         
    }
    // 抢地主
    public void getLord(){
        //System.out.println(CardType.c0.toString());
        for(int i=0;i<2;i  )
            landlord[i].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 > 2))
                    break;
                else {
                    card[count] = new Card(this, i   "-"   j, false);
                    card[count].setLocation(300 (i*20), 50);
                    container.add(card[count]);
                    count  ;
                }
            }
        }
         
        //打乱顺序
        for(int i=0;i<200;i  ){
            Random random=new Random();
            int a=random.nextInt(54) 1;
            int b=random.nextInt(54) 1;
            Card k=card[a];
            card[a]=card[b];
            card[b]=k;
        }
        //开始发牌
        for(int i=0;i<3;i  )
            playerList[i]=new ArrayList<Card>(); //玩家牌
            lordList=new ArrayList<Card>();//地主牌三张
        int t=0;
        for(int i=1;i<=54;i  )
        {
            if(i>=52)//地主牌
            {
                Common.move(card[i], card[i].getLocation(),new Point(300 (i-52)*80,16),t);
                lordList.add(card[i]);
                continue;
            }
            switch ((t  )%3) {
            case 0:
                //左边玩家
                Common.move(card[i], card[i].getLocation(),new Point(50,60 i*5),t);
                playerList[0].add(card[i]);
                card[i].turnFront(); //显示正面
                break;
            case 1:
                //我
                Common.move(card[i], card[i].getLocation(),new Point(180 i*7,450),t);
                playerList[1].add(card[i]);
                card[i].turnFront(); //显示正面
                break;
            case 2:
                //右边玩家
                Common.move(card[i], card[i].getLocation(),new Point(700,60 i*5),t);
                playerList[2].add(card[i]);
                card[i].turnFront(); //显示正面
                break;
            }
            //card[i].turnFront(); //显示正面
            container.setComponentZOrder(card[i], 0);
        }
        //发完牌排序,从大到小
        for(int i=0;i<3;i  )
        {
            Common.order(playerList[i]);
            Common.rePosition(this,playerList[i],i);//重新定位
        }
        dizhu=new JLabel(new ImageIcon("images/dizhu.gif"));
        dizhu.setVisible(false);
        dizhu.setSize(40, 40);
        container.add(dizhu);
    }
 
    // 初始化窗体
    public void Init() {
 
        this.setTitle("斗地主游戏---陈悦馨");
        this.setSize(830, 620);
        setResizable(false);
        setLocationRelativeTo(getOwner()); // 屏幕居中
        container = this.getContentPane();
        container.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        container.setBackground(new Color(0, 112, 26)); // 背景为绿色
 
    }
 
    // 创建菜单 功能按钮
    public void SetMenu() {
        JMenuBar jMenuBar = new JMenuBar();
        JMenu game = new JMenu("游戏");
        JMenu help = new JMenu("帮助");
 
        start = new JMenuItem("新游戏");
        exit = new JMenuItem("退出");
        about = new JMenuItem("关于");
 
        start.addActionListener(this);
        exit.addActionListener(this);
        about.addActionListener(this);
 
        game.add(start);
        game.add(exit);
        help.add(about);
 
        jMenuBar.add(game);
        jMenuBar.add(help);
        this.setJMenuBar(jMenuBar);
         
        landlord[0]=new JButton("抢地主");
        landlord[1]=new JButton("不     抢");
        publishCard[0]= new JButton("出牌");
        publishCard[1]= new JButton("不要");
        for(int i=0;i<2;i  )
        {
            publishCard[i].setBounds(320 i*100, 400, 60, 20);
            landlord[i].setBounds(320 i*100, 400,75,20);
            container.add(landlord[i]);
            landlord[i].addActionListener(this);
            landlord[i].setVisible(false);
            container.add(publishCard[i]);
            publishCard[i].setVisible(false);
            publishCard[i].addActionListener(this);
        }
        for(int i=0;i<3;i  ){
            time[i]=new JTextField("倒计时:");
            time[i].setVisible(false);
            container.add(time[i]);
        }
        time[0].setBounds(140, 230, 60, 20);
        time[1].setBounds(374, 360, 60, 20);
        time[2].setBounds(620, 230, 60, 20);
         
        for(int i=0;i<3;i  )
        {
            currentList[i]=new ArrayList<Card>();
        }
         
    }
 
    //点击鼠标事件
    public void actionPerformed(ActionEvent e) {
         
        if (e.getSource() == exit) {
            this.dispose();
        }
        if (e.getSource() == about) {
            JOptionPane.showMessageDialog(this, "QQ361106306,小柒");
        }
        if (e.getSource() == start) {
             this.restart();
             
        }
        if(e.getSource()==landlord[0])
        {
            time[1].setText("抢地主");
            t.isRun=false; //时钟终结
        }
        if(e.getSource()==landlord[1])
        {
            time[1].setText("不抢");
            t.isRun=false; //时钟终结
        }
        //如果是不要
        if(e.getSource()==publishCard[1])
        {
            this.nextPlayer=true;
            currentList[1].clear();
            time[1].setText("不要");
             
        }
        //如果是出牌按钮
        if(e.getSource()==publishCard[0]){
             
            List<Card> c = new ArrayList<Card>();
            //点选出牌
            for(int i=0;i<playerList[1].size();i  )
            {
                Card card=playerList[1].get(i);
                if(card.clicked)
                {
                    c.add(card);//把点中的牌放入新集合
                }
            }
            /** 给点选的牌排序 */
            /*for(int i=0;i<c.size();i  ){
                System.out.println("点选的牌是:" c.get(i).name);
            }*/
             
             
            int flag=0;
             
            //如果我主动出牌
            if(time[0].getText().equals("不要")&&time[2].getText().equals("不要"))
            {
             
                if(Common.jugdeType(c)!=CardType.c0)
                    flag=1;//表示可以出牌
            }//如果我跟牌
            else{
             
                flag=Common.checkCards(c,currentList);
            }
            //判断是否符合出牌
            if(flag==1)
            {
                currentList[1]=c;
                playerList[1].removeAll(currentList[1]);//移除走的牌
                //定位出牌
                Point point=new Point();
                point.x=(770/2)-(currentList[1].size() 1)*15/2;;
                point.y=310;
                for(int i=0,len=currentList[1].size();i<len;i  )
                {
                    Card card=currentList[1].get(i);
                    Common.move(card, card.getLocation(), point,10);
                    point.x =15;
                }
                //抽完牌后重新整理牌
                Common.rePosition(this, playerList[1], 1);
                time[1].setVisible(false);
                this.nextPlayer=true;
            }
 
        }
    }
     
     
    private void restart() {
        System.out.println("重新开始");
     
    /*  for(int i=0;i<3;i  ){
            currentList[i]=null;
        }
        for(int i=0;i<3;i  ){
            playerList[i]=null;
        }
        lordList=null;
        for(int i=0;i<card.length;i  ){
            card[i]=null;
        }
        nextPlayer=false;*/
         
    //  this.CardInit();//发牌
         
    }
    public static void main(String args[]) {
            new Main();
    }
 
}
class NewTimer implements Runnable{
 
    Main main;
    int i;
    public NewTimer(Main m,int i){
        this.main=m;
        this.i=i;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        main.t=new Time(main,12);//从10开始倒计时
        main.t.start();
    }
     
}

标签: 游戏 斗地主 swing

实例下载地址

JavaSwing的斗地主(小游戏源码)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警