服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java swing框架实现的贪吃蛇游戏完整示例

Java swing框架实现的贪吃蛇游戏完整示例

2021-03-04 09:57101tx Java教程

这篇文章主要介绍了Java swing框架实现的贪吃蛇游戏,结合完整实例形式分析了java使用swing框架结合awt图形绘制实现贪吃蛇游戏的具体步骤与相关实现技巧,需要的朋友可以参考下

本文实例讲述了java swing框架实现的贪吃蛇游戏。分享给大家供大家参考,具体如下:

java是门高级语言,做游戏时适合做后台,但是用它也可以做游戏。闲来无事做的时候可以用来写点小游戏,练习练习预防早衰哈哈!

闲话不说了

下面是以前练习的作品,不怕大家笑话,那个时候用了一个礼拜才做出来的。

源码如下供大家学习。

使用的是java的 swing  jframe jpanel jbutton   当然你也可以使用awt

先来看看运行效果:

Java swing框架实现的贪吃蛇游戏完整示例

具体代码:

?
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
package tcs;
/**
 *
 *
 *
 * @author tx
 */
import java.awt.color;
import java.awt.container;
import java.awt.font;
import java.awt.graphics;
import java.awt.event.keyevent;
import java.awt.event.keylistener;
import java.awt.event.mouseadapter;
import java.awt.event.mouseevent;
import java.awt.event.mouselistener;
import java.util.arraylist;
import java.util.arrays;
import java.util.collection;
import java.util.random;
import java.util.timer;
import java.util.timertask;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jpanel;
public class snack extends jpanel implements keylistener {
  public jbutton bt = new jbutton("重新开始");
  public arraylist<treasure> bw = new arraylist<treasure>();
  public body[] b = new body[5];
  public string state = "";
  public arraylist<point> p = new arraylist<point>();
  public static int score;
  public snack() {
    this.addkeylistener(this);
    shengc();
  }
  public void shengc() {
    for (int i = 0; i < b.length; i++) {
      b[i] = new body();
      b[i].x = 10 - i * 10;
      b[i].y = 150;
    }
  }
  public int x = 0, y = 0;
  public void paint(graphics g) {
    super.paint(g);
    g.setcolor(new color(165,41,10));//rgb定义颜色的方法
    g.setfont(new font(font.sans_serif, font.bold, 20));
    for (int i = 0; i < b.length; i++) {
      body z1 = b[i];
      g.drawstring("o", b[i].x, b[i].y);
    }
    g.setcolor(color.blue);
    g.setfont(new font(font.sans_serif, font.bold, 20));
    g.drawstring("score:" + score, 30, 30);
    paintjs(g);
    paintbw(g);
  }
  public void paintjs(graphics g) {
    g.setcolor(color.black);
    if (state.length() > 1) {
      g.drawstring(state, 140, 200);
    }
  }
  public void paintbw(graphics g) {
    g.setfont(new font(font.sans_serif, font.bold, 25));
    g.setcolor(color.red);
    for (int i = 0; i < bw.size(); i++) {
      g.drawstring("o", bw.get(i).x, bw.get(i).y);
    }
  }
  public boolean yj() {
    if ((b[0].x < 400 && b[0].x > 0) && (b[0].y < 400 && b[0].y > 0)) {
      return false;
    } else {
      state = "game over";
      return true;
    }
  }
  public void stmove() {
    if (pzjc() == false && (yj() == false)) {
      b[0].speed = 8;//此处可提升速度增加难度
      b[0].move();
      p.add(new point(b[0].x, b[0].y, b[0].fx));
      if (p.size() > b.length) {
        p.remove(p.get(0));
        // system.out.println(p.size());
      }
    }
  }
  public int jl(body a, treasure b) {
    int jl = 0;
    jl = (int) math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y)
        * (a.y - b.y));
    return jl;
  }// 暂时无用
  public void ssmove() {
    if (p.size() >= b.length) {
      for (int i = 0; i < b.length - 1; i++) {
        b[i + 1].fx = p.get(i).fx;
        b[i + 1].x = p.get(i).x;
        b[i + 1].y = p.get(i).y;
      }
    }
  }
  random r = new random();
  public void bzbw() {
    if (bw.size() < 1) {
      treasure s = new treasure();
      s.x = r.nextint(300) + 50;
      s.y = r.nextint(300) + 50;
      bw.add(s);
    }
  }
  public void bwxs() {
    timer t = new timer();
    t.schedule(new timertask() {
      public void run() {
      }
    }, 0, 8000);
  }
  public boolean pzjc() {
    for (int i = 1; i < p.size(); i++) {
      if (p.get(0).equals(p.get(i))) {
        state = "game over";
        return true;
      }
    }
    return false;
  }
  public void crush() {
    if (bw.size() > 0) {
      if (jl(b[0], bw.get(0)) < 8) {
        bw.remove(0);
        b = arrays.copyof(b, b.length + 1);
        b[b.length - 1] = new body();
        score += 10;
      }
    }
  }
  public void gameover() {
    mouselistener k = new mouseadapter() {
      public void mouseclicked(mouseevent e) {
        super.mouseclicked(e);
        state = "";
        b = arrays.copyof(b, 5);
        p.clear();
        shengc();
        score = 0;
        bt.setvisible(false);
      }
    };
    if (state.length() > 1) {
      this.add(bt);
      bt.setvisible(true);
      bt.setbounds(150, 150, 100, 30);
      bt.addmouselistener(k);
    }
      if(bt.isvisible()==false){this.remove(bt);}
    this.requestfocus();
  }
  public void zmaction() {
    timer timer = new timer();
    timer.schedule(new timertask() {
      public void run() {
        bzbw();// 生成宝物
        stmove();// 蛇头运动
        ssmove();// 蛇身运动
        crush();// 碰撞检测
        gameover();
        repaint();
      }
    }, 10, 83);
  }
  public static void main(string[] args) {
    jframe jf = new jframe("服务器之家 - 贪吃蛇游戏测试");
    jf.setbounds(0, 0, 400, 400);
    jf.setvisible(true);
    jf.setlayout(null);
    container c = new container();
    c = jf.getcontentpane();
    c.setbackground(color.white);
    jf.setdefaultcloseoperation(jframe.exit_on_close);
    snack s = new snack();
    s.setvisible(true);
    s.setbounds(0, 0, 600, 600);
    s.setlocation(0, 0);
    s.setbackground(color.orange);
    jf.add(s);
    s.zmaction();
    s.requestfocus();
  }
  public void keytyped(keyevent e) {
  }
  public void keypressed(keyevent e) {
    int k = e.getkeycode();
    switch (k) {
    case keyevent.vk_up:
      if (b[0].fx != "sz" && b[0].fx != "xz") {
        b[0].fx = "sz";
      }
      break;
    case keyevent.vk_down:
      if (b[0].fx != "sz" && b[0].fx != "xz") {
        b[0].fx = "xz";
      }
      break;
    case keyevent.vk_left:
      if (b[0].fx != "zz" && b[0].fx != "yz") {
        b[0].fx = "zz";
      }
      break;
    case keyevent.vk_right:
      if (b[0].fx != "zz" && b[0].fx != "yz") {
        b[0].fx = "yz";
      }
      break;
    }
    repaint();
  }
  public void keyreleased(keyevent e) {
  }
}

body类

?
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
package tcs;
public class body {
public int x=0;
public int y=0;
public int speed;
private string str;
public string fx;
public body(){
  fx="yz";
}
public int getx() {
  return x;
}
public void setx(int x) {
  this.x = x;
}
public int gety() {
  return y;
}
public void sety(int y) {
  this.y = y;
}
public string getstr() {
  return str;
}
public void setstr(string str) {
  this.str = str;
}
public void sz(){
  this.y+=-speed;
}
public void xz(){
  this.y+=speed;
}
public void zz(){
  this.x+=-speed;
}
public void yz(){
  this.x+=speed;
}
public void move(){
  if(fx=="xz"){
    xz();
  }
  if(fx=="sz"){
    sz();
  }
  if(fx=="zz"){
    zz();
  }
  if(fx=="yz"){
    yz();
  }
}
}

宝物类

?
1
2
3
4
5
6
package tcs;
public class treasure {
public int x;
public int y;
public string str;
}

point类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package tcs;
public class point {
public int x;
public int y;
public string fx;
public point(int x,int y,string fx){
  this.x=x;
  this.y=y;
  this.fx=fx;
}
public boolean equals(object o){
  if(o instanceof point){
    point p=(point)o;
    if(p.x==this.x&&p.y==this.y){
      return true;
    }
  }
  if(o==this){return true;}
  if(o==null){return false;}
  return false;}
}

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/tx101q/article/details/54811079

延伸 · 阅读

精彩推荐