1 import java.awt.*;
2 import java.awt.event.*;
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Random;
6 import javax.sound.sampled.*;
7 import javax.swing.*;
8
9 class Tile{
10 int x;
11 int y;
12
13 public Tile(int x0,int y0){
14 x = x0;
15 y = y0;
16 }
17 }
18
19 public class SnakeDemo extends JComponent{
20 /**
21 *
22 */
23 private static final long serialVersionUID = 3794762291171148906L;
24 private final int MAX_SIZE = 400;//蛇身体最长为400节
25 private Tile temp = new Tile(0,0);
26 private Tile temp2 = new Tile(0,0);
27 private Tile head = new Tile(227,100);//头部的位置初始化为(227,100)
28 private Tile[] body = new Tile[MAX_SIZE];
29 private String direction = "R";//默认向右走
30 private String current_direction = "R";//当前方向
31 private boolean first_launch = false;
32 private boolean iseaten = false;
33 private boolean isrun = true;
34 private int randomx,randomy;
35 private int body_length = 5;//身体长度初始化为5
36 private Thread run;
37 private JLabel label = new JLabel("当前长度:");
38 private JLabel label2 = new JLabel("所花时间:");
39 private JLabel label3 = new JLabel("说 明:");
40 private JTextArea explain = new JTextArea("此游戏是一个贪吃蛇Demo版本,实现简单地移动,得分,判断撞墙和撞自己的功能,"
41 + "初始长度为6,头部为红色,身体的颜色渐变。游戏本身代码只有300行,加上一些显示,计时和音效后多了几百行。\n"
42 + "游戏界面按上下左右键实现移动,按ESC重新开始,按空格键可以实现暂停和开始");
43 private JLabel Score = new JLabel("6");
44 private JLabel Time = new JLabel("");
45 private Font f = new Font("微软雅黑",Font.PLAIN,15);
46 private Font f2 = new Font("微软雅黑",Font.PLAIN,13);
47 private JPanel p = new JPanel();
48 private int hour =0;
49 private int min =0;
50 private int sec =0 ;
51 private boolean pause = false;
52
53 public SnakeDemo(){
54 String lookAndFeel =UIManager.getSystemLookAndFeelClassName();
55 try {
56 UIManager.setLookAndFeel(lookAndFeel);
57 } catch (ClassNotFoundException e1) {
58 // TODO 自动生成的 catch 块
59 e1.printStackTrace();
60 } catch (InstantiationException e1) {
61 // TODO 自动生成的 catch 块
62 e1.printStackTrace();
63 } catch (IllegalAccessException e1) {
64 // TODO 自动生成的 catch 块
65 e1.printStackTrace();
66 } catch (UnsupportedLookAndFeelException e1) {
67 // TODO 自动生成的 catch 块
68 e1.printStackTrace();
69 }
70
71 //布局
72 add(label);
73 label.setBounds(500, 10, 80, 20);
74 label.setFont(f);
75 add(Score);
76 Score.setBounds(500, 35, 80, 20);
77 Score.setFont(f);
78 add(label2);
79 label2.setBounds(500, 60, 80, 20);
80 label2.setFont(f);
81 add(Time);
82 Time.setBounds(500, 85, 80, 20);
83 Time.setFont(f);
84 add(p);
85 p.setBounds(498, 110, 93, 1);
86 p.setBorder(BorderFactory.createLineBorder(Color.black));
87
88 add(label3);
89 label3.setBounds(500, 115, 80, 20);
90 label3.setFont(f);
91 add(explain);
92 explain.setBounds(498, 138, 100, 350);
93 explain.setFont(f2);
94 explain.setLineWrap(true);
95 explain.setOpaque(false);
96
97 for(int i = 0; i < MAX_SIZE;i++)
98 {
99 body[i] = new Tile(0,0);
100 }
101
102 addKeyListener(new KeyAdapter() {
103 public void keyPressed(KeyEvent e) {
104 super.keyPressed(e);
105 if(e.getKeyCode() == KeyEvent.VK_RIGHT)
106 {
107 if(isrun && current_direction != "L")
108 {
109 direction = "R";
110 }
111 }
112 if(e.getKeyCode() == KeyEvent.VK_LEFT)
113 {
114 if(isrun && current_direction != "R")
115 {
116 direction = "L";
117 }
118 }
119 if(e.getKeyCode() == KeyEvent.VK_UP)
120 {
121 if(isrun && current_direction != "D")
122 {
123 direction = "U";
124 }
125 }
126 if(e.getKeyCode() == KeyEvent.VK_DOWN)
127 {
128 if(isrun && current_direction != "U")
129 {
130
贪吃蛇java源代码
最新推荐文章于 2021-04-26 11:29:38 发布