本文实例讲述了java gui编程之贪吃蛇游戏简单实现方法。分享给大家供大家参考,具体如下:
例子简单,界面简陋 请见谅
项目结构如下
constant.jvava 代码如下:
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
|
package snake;
/**
*
* @author hjn
*
*/
public class constant {
/**
* 蛇方移动方向:左边
*/
public static final int left = 0 ;
/**
* 蛇方移动方向:右边
*/
public static final int right = 1 ;
/**
* 蛇方移动方向:上边
*/
public static final int up = 3 ;
/**
* 蛇方移动方向:下边
*/
public static final int down = 4 ;
/**
* 界面列数
*/
public static final int cols = 30 ;
/**
* 界面行数
*/
public static final int rows = 30 ;
/**
* 每个格子边长
*/
public static final int boder_size = 15 ;
}
|
node.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
|
package snake;
/**
* 格子
*
* @author hjn
*
*/
public class node {
/**
* 所在行数
*/
private int row;
/**
* 所在列数
*/
private int col;
public node() {
};
public node( int row, int col) {
this .row = row;
this .col = col;
};
/**
* 蛇将要移动一格时头部格子将所到格子
*
* @param dir
* 蛇前进方向
* @param node
* 蛇头所在的格子
*/
public node( int dir, node node) {
if (dir == constant.left) {
this .col = node.getcol() - 1 ;
this .row = node.getrow();
} else if (dir == constant.right) {
this .col = node.getcol() + 1 ;
this .row = node.getrow();
} else if (dir == constant.up) {
this .row = node.getrow() - 1 ;
this .col = node.getcol();
} else {
this .row = node.getrow() + 1 ;
this .col = node.getcol();
}
}
/**
* 重写equals方法
*/
public boolean equals(object obj) {
if (obj instanceof node) {
node node = (node) obj;
if ( this .col == node.col && this .row == node.row) {
return true ;
} else {
return false ;
}
} else {
return false ;
}
}
public int getrow() {
return row;
}
public void setrow( int row) {
this .row = row;
}
public int getcol() {
return col;
}
public void setcol( int col) {
this .col = col;
}
public string tostring() {
return "col:" + this .col + " row:" + this .row;
}
}
|
egg.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
|
package snake;
import java.awt.color;
import java.awt.graphics;
import java.util.random;
/**
* 蛋,蛇的食物
*
* @author nan
*
*/
public class egg extends node {
/**
* 蛋的颜色
*/
color color;
/**
* 随机函数
*/
public static random random = new random();
/**
* 构造函数 蛋出现在固定位置
*
* @param row
* 所在第几行数
* @param col
* 所在第几列数
*/
public egg( int row, int col) {
super (row, col);
this .color = color.green;
}
/**
* 构造函数 蛋随机出现
*
*/
public egg() {
super ();
int col = random.nextint(constant.cols - 4 ) + 2 ;
int row = random.nextint(constant.rows - 4 ) + 2 ;
this .setcol(col);
this .setrow(row);
}
/**
* 画蛋
* @param g 画笔
*/
void draw(graphics g) {
if ( this .color == color.green) {
this .color = color.red;
} else {
this .color = color.green;
}
g.setcolor( this .color);
int bodersize = constant.boder_size;
g.filloval( this .getcol() * bodersize, this .getrow() * bodersize,
bodersize, bodersize);
}
public color getcolor() {
return color;
}
public void setcolor(color color) {
this .color = color;
}
}
|
snake.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
|
package snake;
import java.awt.color;
import java.awt.graphics;
import java.awt.event.keyevent;
import java.util.arraylist;
import java.util.list;
/**
* 蛇
*
* @author hjn
*
*/
public class snake {
/**
* 前进的方向
*/
int dir;
/**
* 蛇的身体,由一个格子node集合组成
*/
list<node> nodelist = new arraylist<node>();
/**
* 是否越界
*/
boolean isoverstep = false ;
/**
* 构造方法默认开始方向向左 ,蛇身有3个格子 ,位置在20行,15列
*/
public snake() {
this .dir = constant.left;
for ( int i = 0 ; i < 3 ; i++) {
node node = new node( 20 , 15 + i);
this .nodelist.add(node);
}
}
/**
* 蛇前进
*/
void forward() {
addnode();
nodelist.remove(nodelist.size() - 1 );
}
/**
* 蛇前进的时候头部增加格子,私有方法
*/
private void addnode() {
node node = nodelist.get( 0 );
node = new node(dir, node);
nodelist.add( 0 , node);
}
/**
* 是否吃到蛋,蛇身是否有格子跟蛋重叠,所以重写了node的equals方法
*
* @param egg蛋
* @return boolean
*/
boolean eategg(egg egg) {
if (nodelist.contains(egg)) {
addnode();
return true ;
} else {
return false ;
}
}
/**
* 画自己
*
* @param g画笔
*/
void draw(graphics g) {
g.setcolor(color.black);
for ( int i = 0 ; i < this .nodelist.size(); i++) {
node node = this .nodelist.get(i);
if (node.getcol() > (constant.cols - 2 ) || node.getcol() < 2
|| node.getrow() > (constant.rows - 2 ) || node.getrow() < 2 ) {
this .isoverstep = true ;
}
g.fillrect(node.getcol() * constant.boder_size, node.getrow()
* constant.boder_size, constant.boder_size,
constant.boder_size);
}
forward();
}
/**
* 键盘事件,来确定前进方向,有左右上下4个方向
*
* @param e键盘监听事件
*/
void keypress(keyevent e) {
int key = e.getkeycode();
switch (key) {
case keyevent.vk_left:
if ( this .dir != constant.left)
this .dir = constant.left;
break ;
case keyevent.vk_right:
if ( this .dir != constant.right)
this .dir = constant.right;
break ;
case keyevent.vk_up:
if ( this .dir != constant.up)
this .dir = constant.up;
break ;
case keyevent.vk_down:
if ( this .dir != constant.down)
this .dir = constant.down;
break ;
default :
break ;
}
}
public int getdir() {
return dir;
}
public void setdir( int dir) {
this .dir = dir;
}
public list<node> getnodelist() {
return nodelist;
}
public void setnodelist(list<node> nodelist) {
this .nodelist = nodelist;
}
public boolean isoverstep() {
return isoverstep;
}
public void setoverstep( boolean isoverstep) {
this .isoverstep = isoverstep;
}
}
|
主界面mainframe.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
|
package snake;
import java.awt.color;
import java.awt.font;
import java.awt.frame;
import java.awt.graphics;
import java.awt.event.keyadapter;
import java.awt.event.keyevent;
import java.awt.event.windowadapter;
import java.awt.event.windowevent;
/**
* 贪吃蛇展示页面
*
* @author hjn
*
*/
public class mainframe extends frame {
/**
* 版本
*/
private static final long serialversionuid = -5227266702753583633l;
/**
* 背景颜色
*/
color color = color.gray;
/**
* 蛋
*/
static egg egg = new egg();
/**
* 蛇
*/
snake snake = new snake();
/**
* 游戏是否失败
*/
boolean gameover = false ;
/**
* 给画笔起一个线程
*/
paintthread paintthread = new paintthread();
/**
* 构造方法
*/
public mainframe() {
init();
}
/**
* 界面初始化
*/
void init() {
this .setbounds( 200 , 200 , constant.cols * constant.boder_size,
constant.rows * constant.boder_size);
this .setresizable( true );
this .repaint();
/**
* 窗口关闭监听事件
*/
this .addwindowlistener( new windowadapter() {
@override
public void windowclosing(windowevent e) {
system.exit( 0 );
}
});
/**
* 添加键盘监听事件
*/
this .addkeylistener( new keymomiter());
/**
* 画笔线程启动
*/
new thread(paintthread).start();
}
/**
* 画笔画界面
*/
public void paint(graphics g) {
color c = g.getcolor();
g.setcolor(color.gray);
g.fillrect( 0 , 0 , constant.cols * constant.boder_size, constant.rows
* constant.boder_size);
g.setcolor(color.dark_gray);
for ( int i = 0 ; i < constant.rows; i++) {
g.drawline( 0 , i * constant.boder_size, constant.cols
* constant.boder_size, i * constant.boder_size);
}
for ( int i = 0 ; i < constant.cols; i++) {
g.drawline(i * constant.boder_size, 0 , i * constant.boder_size,
constant.rows * constant.boder_size);
}
g.setcolor(color.yellow);
g.setfont( new font( "宋体" , font.bold, 20 ));
g.drawstring( "score:" + getscore(), 10 , 60 );
if (gameover) {
g.setcolor(color.red);
g.drawstring( "game over" , 100 , 60 );
this .paintthread.pause = true ;
}
g.setcolor(c);
if (snake.eategg(egg)) {
egg = new egg();
}
snake.draw(g);
egg.draw(g);
}
/**
* 获取分数
*
* @return int 分数
*/
int getscore() {
return snake.getnodelist().size();
}
/**
* 画笔的线程
*
* @author hjn
*/
class paintthread implements runnable {
private boolean isrun = true ;
private boolean pause = false ;
@override
public void run() {
while (isrun) {
if (pause) {
continue ;
} else {
if (snake.isoverstep == true ) {
gameover = true ;
}
repaint();
}
try {
thread.sleep( 100 );
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
/**
* 暂停
*/
public void pause() {
this .pause = true ;
}
/**
* 重新开始
*/
public void restart() {
this .pause = true ;
snake = new snake();
}
/**
* 游戏结束
*/
public void gameover() {
isrun = false ;
}
}
/**
* 停止
*/
void stop() {
gameover = true ;
}
/**
* 键盘监听器
*
* @author hjn
*
*/
class keymomiter extends keyadapter {
@override
public void keypressed(keyevent e) {
super .keypressed(e);
int key = e.getkeycode();
if (key == keyevent.vk_f2) {
paintthread.restart();
} else {
snake.keypress(e);
}
}
}
/**
* 启动程序入口
*
* @param args
*/
@suppresswarnings ( "deprecation" )
public static void main(string[] args) {
mainframe mainframe = new mainframe();
mainframe.show();
}
}
|
运行效果:
附:完整实例代码点击此处本站下载。
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/h348592532/article/details/14451639