tscRubbish

 

 1 package Snake;
 2 
 3 import java.awt.EventQueue;
 4 import javax.swing.JFrame;
 5 
 6 public class Snake extends JFrame {
 7     public Snake() {
 8         initUI();
 9     }
10     private void initUI() {
11         add(new Board());
12 
13         setResizable(false);
14         pack();//调整此窗口的大小,以适合其子组件的首选大小和布局
15 
16         setTitle("Snake");
17         setLocationRelativeTo(null);//设置窗口相对于指定组件的位置。如果组件null,则此窗口将置于屏幕版的中央
18         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19     }
20     public static void main(String[] args) {
21         EventQueue.invokeLater(() -> {
22             JFrame ex = new Snake();
23             ex.setVisible(true);
24         });
25     }
26 }

 

  1 package Snake;
  2 
  3 import java.awt.Color;
  4 import java.awt.Dimension;
  5 import java.awt.Font;
  6 import java.awt.FontMetrics;
  7 import java.awt.Graphics;
  8 import java.awt.Image;
  9 import java.awt.Toolkit;
 10 import java.awt.event.ActionEvent;
 11 import java.awt.event.ActionListener;
 12 import java.awt.event.KeyAdapter;
 13 import java.awt.event.KeyEvent;
 14 import javax.swing.ImageIcon;
 15 import javax.swing.JPanel;
 16 import javax.swing.Timer;
 17 
 18 public class Board extends JPanel implements ActionListener {
 19 
 20     private final int B_WIDTH = 300;
 21     private final int B_HEIGHT = 300;
 22     private final int DOT_SIZE = 10;
 23     private final int ALL_DOTS = 900;
 24     private final int RAND_POS = 29;
 25     private final int DELAY = 140;
 26 
 27     private final int x[] = new int[ALL_DOTS];
 28     private final int y[] = new int[ALL_DOTS];
 29 
 30     private int dots;
 31     private int apple_x;
 32     private int apple_y;
 33 
 34     private boolean leftDirection = false;
 35     private boolean rightDirection = true;
 36     private boolean upDirection = false;
 37     private boolean downDirection = false;
 38     private boolean inGame = true;
 39 
 40     private Timer timer;
 41     private Image ball;
 42     private Image apple;
 43     private Image head;
 44 
 45     public Board() {
 46         initBoard();
 47     }
 48     private void initBoard() {
 49         addKeyListener(new TAdapter());
 50         setBackground(Color.black);
 51         setFocusable(true);
 52 
 53         setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
 54         loadImages();
 55         initGame();
 56     }
 57     private void loadImages() {
 58         ImageIcon iid = new ImageIcon(getClass().getResource("/pic/dot.png"));
 59         ball = iid.getImage();
 60         ImageIcon iia = new ImageIcon(getClass().getResource("/pic/apple.png"));
 61         apple = iia.getImage();
 62         ImageIcon iih = new ImageIcon(getClass().getResource("/pic/head.png"));
 63         head = iih.getImage();
 64     }
 65     private void initGame() {
 66         dots = 3;
 67         for (int z = 0; z < dots; z++) {
 68             x[z] = 50 - z * 10;
 69             y[z] = 50;
 70         }
 71         locateApple();
 72         timer = new Timer(DELAY, this);
 73         timer.start();
 74     }
 75     @Override
 76     public void paintComponent(Graphics g) {
 77         super.paintComponent(g);
 78         doDrawing(g);
 79     }
 80     private void doDrawing(Graphics g) {
 81         if (inGame) {
 82             g.drawImage(apple, apple_x, apple_y, this);
 83             for (int z = 0; z < dots; z++) {
 84                 if (z == 0) {
 85                     g.drawImage(head, x[z], y[z], this);
 86                 } else {
 87                     g.drawImage(ball, x[z], y[z], this);
 88                 }
 89             }
 90             //Toolkit.getDefaultToolkit().sync();
 91         } else {
 92             gameOver(g);
 93         }
 94     }
 95     private void gameOver(Graphics g) {
 96         String msg = "Game Over";
 97         Font small = new Font("Helvetica", Font.BOLD, 14);
 98         FontMetrics metr = getFontMetrics(small);
 99 
100         g.setColor(Color.white);
101         g.setFont(small);
102         g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
103     }
104     private void checkApple() {
105         if ((x[0] == apple_x) && (y[0] == apple_y)) {
106             dots++;
107             locateApple();
108         }
109     }
110     private void move() {
111         for (int z = dots; z > 0; z--) {
112             x[z] = x[(z - 1)];
113             y[z] = y[(z - 1)];
114         }
115         if (leftDirection) {
116             x[0] -= DOT_SIZE;
117         }
118         if (rightDirection) {
119             x[0] += DOT_SIZE;
120         }
121         if (upDirection) {
122             y[0] -= DOT_SIZE;
123         }
124         if (downDirection) {
125             y[0] += DOT_SIZE;
126         }
127     }
128     private void checkCollision() {
129         for (int z = dots; z > 0; z--) {
130             if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
131                 inGame = false;
132             }
133         }
134         if (y[0] >= B_HEIGHT) {
135             inGame = false;
136         }
137         if (y[0] < 0) {
138             inGame = false;
139         }
140         if (x[0] >= B_WIDTH) {
141             inGame = false;
142         }
143         if (x[0] < 0) {
144             inGame = false;
145         }
146         if (!inGame) {
147             timer.stop();
148         }
149     }
150     private void locateApple() {
151         int r = (int) (Math.random() * RAND_POS);
152         apple_x = ((r * DOT_SIZE));
153 
154         r = (int) (Math.random() * RAND_POS);
155         apple_y = ((r * DOT_SIZE));
156     }
157     @Override
158     public void actionPerformed(ActionEvent e) {
159         if (inGame) {
160             checkApple();
161             checkCollision();
162             move();
163         }
164         repaint();
165     }
166     private class TAdapter extends KeyAdapter {
167         @Override
168         public void keyPressed(KeyEvent e) {
169             int key = e.getKeyCode();
170             if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
171                 leftDirection = true;
172                 upDirection = false;
173                 downDirection = false;
174             }
175             if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
176                 rightDirection = true;
177                 upDirection = false;
178                 downDirection = false;
179             }
180             if ((key == KeyEvent.VK_UP) && (!downDirection)) {
181                 upDirection = true;
182                 rightDirection = false;
183                 leftDirection = false;
184             }
185             if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
186                 downDirection = true;
187                 rightDirection = false;
188                 leftDirection = false;
189             }
190         }
191     }
192 }

 

分类:

技术点:

相关文章: