本文實例講述了java swing框架實現的貪吃蛇游戲。分享給大家供大家參考,具體如下:
java是門高級語言,做游戲時適合做后臺,但是用它也可以做游戲。閑來無事做的時候可以用來寫點小游戲,練習練習預防早衰哈哈!
閑話不說了
下面是以前練習的作品,不怕大家笑話,那個時候用了一個禮拜才做出來的。
源碼如下供大家學習。
使用的是java的 swing jframe jpanel jbutton 當然你也可以使用awt
先來看看運行效果:
具體代碼:
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