服务器之家

服务器之家 > 正文

java简易小游戏制作代码

时间:2021-09-02 14:51     来源/作者:落倾城

java简易小游戏制作

游戏思路:设置人物移动,游戏规则,积分系统,随机移动的怪物,游戏胜负判定,定时器。

游戏内容部分

?
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package 代码部分;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class TestGamePanel extends JPanel implements KeyListener, ActionListener {
    //初始化人物坐标
    int p1X;
    int p1Y;
    int p2X;
    int p2Y;
    boolean isStart = false;    //游戏是否开始
    boolean p1isFail = false;     //游戏是否失败
    boolean p2isFail = false;
    String fx1;         //左:L, 右:R, 上:U, 下:D
    String fx2;
    Timer timer = new Timer(50,this);//定时器
    //积分
    int p1score = 0;
    int p2score = 0;
    //苹果
    int AppleX;
    int AppleY;
    //怪物
    int monster1X;
    int monster1Y;
    int monster2X;
    int monster2Y;
    int monster3X;
    int monster3Y;
    int monster4X;
    int monster4Y;
    int monster5X;
    int monster5Y;
    //随机积分
    Random random = new Random();
    public TestGamePanel() {
        init();
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start();
    }
    //初始化
    public void init() {
        p1X = 25;
        p1Y = 150;
        p2X = 700;
        p2Y = 550;
        fx1 = "L";
        fx2 = "R";
        monster1X = 25*random.nextInt(28);
        monster1Y = 100 + 25*random.nextInt(18);
        monster2X = 25*random.nextInt(28);
        monster2Y = 100 + 25*random.nextInt(18);
        monster3X = 25*random.nextInt(28);
        monster3Y = 100 + 25*random.nextInt(18);
        monster4X = 25*random.nextInt(28);
        monster4Y = 100 + 25*random.nextInt(18);
        monster5X = 25*random.nextInt(28);
        monster5Y = 100 + 25*random.nextInt(18);
        AppleX = 25*random.nextInt(28);
        AppleY = 100 + 25*random.nextInt(18);
        add(kaishi);
        add(chongkai);
        guize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new TestGameRule();
            }
        });
    }
    //游戏功能按钮
    JButton kaishi = new JButton("开始");
    JButton chongkai = new JButton("重新开始");
    JButton guize = new JButton("游戏规则");
    //画板
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        TestGameData.header.paintIcon(this,g,0,0);
        g.setColor(Color.CYAN);
        g.fillRect(0,100,780,520);
        //画人物
        TestGameData.p1player1.paintIcon(this,g,p1X,p1Y);
        TestGameData.p2player1.paintIcon(this,g,p2X,p2Y);
        //画得分
        g.setFont(new Font("华文彩云",Font.BOLD,18));  //设置字体
        g.setColor(Color.RED);
        g.drawString("玩家1:" + p1score,20,20 );
        g.drawString("玩家2:" + p2score,680,20);
        //画苹果
        TestGameData.apple.paintIcon(this,g,AppleX,AppleY);
        //画静态怪物
        TestGameData.monster.paintIcon(this,g,monster1X,monster1Y);
        TestGameData.monster.paintIcon(this,g,monster2X,monster2Y);
        TestGameData.monster.paintIcon(this,g,monster3X,monster3Y);
        TestGameData.monster.paintIcon(this,g,monster4X,monster4Y);
        TestGameData.monster.paintIcon(this,g,monster5X,monster5Y);
        //游戏提示,是否开始
        if(!isStart) {
            g.setColor(Color.BLACK);
            g.setFont(new Font("华文彩云",Font.BOLD,30));
            g.drawString("请点击开始游戏",300,300);
        }
        //游戏结束提示,是否重新开始
        if(p2isFail || p1score == 15) {
            g.setColor(Color.RED);
            g.setFont(new Font("华文彩云",Font.BOLD,30));
            g.drawString("玩家一获胜,请点击重新开始游戏",200,300);
        }
        if(p1isFail || p2score == 15) {
            g.setColor(Color.RED);
            g.setFont(new Font("华文彩云",Font.BOLD,30));
            g.drawString("玩家二获胜,请点击重新开始游戏",200,300);
        }
    }
    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        //控制人物走动
        //玩家1
        if(isStart == true && (p1isFail == false && p2isFail == false)) {
            if(e.getKeyCode() == KeyEvent.VK_D) {
                fx1 = "R";
                p1X += 25;
                if(p1X >= 750) {p1X = 750;}
            }
            else if(e.getKeyCode() == KeyEvent.VK_A) {
                fx1 = "L";
                p1X -= 25;
                if(p1X <= 0) {p1X = 0;}
            }
            else if(e.getKeyCode() == KeyEvent.VK_W) {
                fx1 = "U";
                p1Y -= 25;
                if(p1Y <= 100) {p1Y = 100;}
            }
            else if(e.getKeyCode() == KeyEvent.VK_S) {
                fx1 = "D";
                p1Y += 25;
                if(p1Y >= 600) {p1Y = 600;}
            }
            //玩家2
            if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
                fx2 = "R";
                p2X += 25;
                if(p2X >= 750) {p2X = 750;}
            }
            else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
                fx2 = "L";
                p2X -= 25;
                if(p2X <= 0) {p2X = 0;}
            }
            else if(e.getKeyCode() == KeyEvent.VK_UP) {
                fx2 = "U";
                p2Y -= 25;
                if(p2Y <= 100) {p2Y = 100;}
            }
            else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
                fx2 = "D";
                p2Y += 25;
                if(p2Y >= 600) {p2Y = 600;}
            }
        }
        repaint();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        kaishi.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isStart = true;
            }
        });
        chongkai.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(p1isFail) { p1isFail = !p1isFail; init(); }
                if(p2isFail) { p2isFail = !p2isFail; init(); }
            }
        });
        add(kaishi);
        add(chongkai);
        add(guize);
        if(isStart == true && (p1isFail == false && p2isFail == false)) {
            //让人动起来
            if(fx1.equals("R")) {
                p1X += 25;
                if(p1X >= 750) { p1X = 750; }
            }
            if(fx1.equals("L")) {
                p1X -= 25;
                if(p1X <= 0) { p1X = 0; }
            }
            if(fx1.equals("U")) {
                p1Y -= 25;
                if(p1Y <= 100) { p1Y = 100; }
            }
            if(fx1.equals("D")) {
                p1Y += 25;
                if(p1Y >= 600) { p1Y = 600; }
            }
            if(fx2.equals("R")) {
                p2X += 25;
                if(p2X >= 750) { p2X = 750; }
            }
            if(fx2.equals("L")) {
                p2X -= 25;
                if(p2X <= 0) { p2X = 0; }
            }
            if(fx2.equals("U")) {
                p2Y -= 25;
                if(p2Y <= 100) { p2Y = 100; }
            }
            if(fx2.equals("D")) {
                p2Y += 25;
                if(p2Y >= 600) { p2Y = 600; }
            }
            //让怪物动起来
                //怪物1
                int i = random.nextInt(4) + 1;
                if(i == 1) {
                    monster1X += 5;
                    if(monster1X >= 750) {monster1X = 750;}
                }
                if(i == 2) {
                    monster1X -= 5;
                    if(monster1X <= 0) {monster1X = 0;}
                }
                if(i == 3) {
                    monster1Y += 5;
                    if(monster1Y >= 600) {monster1Y = 600;}
                }
                if(i == 4) {
                    monster1Y -= 5;
                    if(monster1Y <= 100) {monster1Y = 100;}
                }
            //怪物2
            int j = random.nextInt(4) + 1;
            if(j == 1) {
                monster2X += 5;
                if(monster2X >= 750) {monster2X = 750;}
            }
            if(j == 2) {
                monster2X -= 5;
                if(monster2X <= 0) {monster2X = 0;}
            }
            if(j == 3) {
                monster2Y += 5;
                if(monster2Y >= 600) {monster2Y = 600;}
            }
            if(j == 4) {
                monster2Y -= 5;
                if(monster2Y <= 100) {monster2Y = 100;}
            }
            //怪物3
            int k = random.nextInt(4) + 1;
            if(k == 1) {
                monster3X += 5;
                if(monster3X >= 750) {monster3X = 750;}
            }
            if(k == 2) {
                monster3X -= 5;
                if(monster3X <= 0) {monster3X = 0;}
            }
            if(k == 3) {
                monster3Y += 5;
                if(monster3Y >= 600) {monster3Y = 600;}
            }
            if(k == 4) {
                monster3Y -= 5;
                if(monster3Y <= 100) {monster3Y = 100;}
            }
            //怪物4
            int n= random.nextInt(4) + 1;
            if(n == 1) {
                monster4X += 5;
                if(monster4X >= 750) {monster4X = 750;}
            }
            if(n == 2) {
                monster4X -= 5;
                if(monster4X <= 0) {monster4X = 0;}
            }
            if(n == 3) {
                monster4Y += 5;
                if(monster4Y >= 600) {monster4Y = 600;}
            }
            if(n == 4) {
                monster4Y -= 5;
                if(monster4Y <= 100) {monster4Y = 100;}
            }
            //怪物5
            int m = random.nextInt(4) + 1;
            if(m == 1) {
                monster5X += 5;
                if(monster5X >= 750) {monster5X = 750;}
            }
            if(m == 2) {
                monster5X -= 5;
                if(monster5X <= 0) {monster5X = 0;}
            }
            if(m == 3) {
                monster5Y += 5;
                if(monster5Y >= 600) {monster5Y = 600;}
            }
            if(m == 4) {
                monster5Y -= 5;
                if(monster5Y <= 100) {monster5Y = 100;}
            }
            //如果有玩家吃到食物
            if(p1X == AppleX && p1Y == AppleY) {
                p1score++;
                AppleX = 25*random.nextInt(28);
                AppleY = 100 + 25*random.nextInt(18);
            } else if(p2X == AppleX && p2Y == AppleY) {
                p2score++;
                AppleX = 25*random.nextInt(28);
                AppleY = 100 + 25*random.nextInt(18);
            }
            //如果有玩家碰到怪物,判定死亡,游戏结束           后续有修改,暂用
            //怪物1死亡
            if(p1X >= monster1X -25 && p1X <= monster1X +25) {
                if(p1Y == monster1Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p1Y >= monster1Y -25 && p1Y <= monster1Y +25) {
                if(p1X == monster1X) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p2X >= monster1X -25 && p2X <= monster1X +25) {
                if(p2Y == monster1Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            if(p2Y >= monster1Y -25 && p2Y <= monster1Y +25) {
                if(p2X == monster1X) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            //怪物2死亡
            if(p1X >= monster2X -25 && p1X <= monster2X +25) {
                if(p1Y == monster2Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p1Y >= monster2Y -25 && p1Y <= monster2Y +25) {
                if(p1X == monster2X) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p2X >= monster2X -25 && p2X <= monster2X +25) {
                if(p2Y == monster2Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            if(p2Y >= monster2Y -25 && p2Y <= monster2Y +25) {
                if(p2X == monster2X) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            //怪物3死亡
            if(p1X >= monster3X -25 && p1X <= monster3X +25) {
                if(p1Y == monster3Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p1Y >= monster3Y -25 && p1Y <= monster3Y +25) {
                if(p1X == monster3X) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p2X >= monster3X -25 && p2X <= monster3X +25) {
                if(p2Y == monster3Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            if(p2Y >= monster3Y -25 && p2Y <= monster3Y +25) {
                if(p2X == monster3X) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            //怪物4死亡
            if(p1X >= monster4X -25 && p1X <= monster4X +25) {
                if(p1Y == monster4Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p1Y >= monster4Y -25 && p1Y <= monster4Y +25) {
                if(p1X == monster1X) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p2X >= monster4X -25 && p2X <= monster4X +25) {
                if(p2Y == monster4Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            if(p2Y >= monster4Y -25 && p2Y <= monster4Y +25) {
                if(p2X == monster4X) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            //怪物5死亡
            if(p1X >= monster5X -25 && p1X <= monster5X +25) {
                if(p1Y == monster5Y) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p1Y >= monster5Y -25 && p1Y <= monster5Y +25) {
                if(p1X == monster5X) { p1isFail = !p1isFail; p1score = p2score = 0;}
            }
            if(p2X >= monster5X -25 && p2X <= monster5X +25) {
                if(p2Y == monster5Y) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            if(p2Y >= monster5Y -25 && p2Y <= monster5Y+25) {
                if(p2X == monster5X) { p2isFail = !p2isFail; p1score = p2score = 0;}
            }
            //如果有玩家达到指定积分,判定获胜,游戏结束
            if(p1score == 15) { p2isFail = !p2isFail; }
            if(p2score == 15) { p1isFail = !p1isFail; }
            repaint();
        }
        timer.start();
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
    }
}

游戏规则(使用弹窗)部分

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package 代码部分;
 
import javax.swing.*;
import java.awt.*;
 
public class TestGameRule extends JDialog {
    private int num = 1;
    public TestGameRule() {
        TextArea textArea = new TextArea(20,10);
        textArea.setText("游戏中有五个怪物随机移动,碰到怪物算死亡\\\n游戏中有随机出现的苹果,碰到一个苹果加一分,\\\n先达到十五分或者对手死亡算游戏胜利!");
        JScrollPane jScrollPane = new JScrollPane(textArea);
        this.add(jScrollPane);
        this.setBounds(200,200,400,400);
        this.setVisible(true);
        textArea.setEditable(false);
        setResizable(false);
        textArea.setBackground(Color.PINK);
    }
}

图片素材

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package 代码部分;
 
import javax.swing.*;
import java.net.URL;
 
public class TestGameData {
    public static URL headerurl = TestGameData.class.getResource("/图片素材/header.jpg");
    public static URL p1player1url = TestGameData.class.getResource("/图片素材/1.jpg");
    public static URL p2player2url = TestGameData.class.getResource("/图片素材/2.jpg");
    public static URL appleurl = TestGameData.class.getResource("/图片素材/apple.jpg");
    public static URL monsterurl = TestGameData.class.getResource("/图片素材/monster.jpg");
 
    public static ImageIcon p1player1 = new ImageIcon(p1player1url);
    public static ImageIcon p2player1 = new ImageIcon(p2player2url);
    public static ImageIcon header = new ImageIcon(headerurl);
    public static ImageIcon apple = new ImageIcon(appleurl);
    public static ImageIcon monster = new ImageIcon(monsterurl);
}

主函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package 代码部分;
 
import javax.swing.*;
 
public class TestStartGame {
    public static void main(String[] args) {
        //制作窗口
        JFrame jFrame = new JFrame("2D对战小游戏");
        jFrame.setBounds(10,10,790,660);
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //添加游戏面板
        jFrame.add(new TestGamePanel());
        //设置可见
        jFrame.setVisible(true);
    }
}

实现效果

java简易小游戏制作代码

到此这篇关于java简易小游戏制作代码的文章就介绍到这了,更多相关java简易小游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/liuxin2002/article/details/115441170

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部