這是一個非常簡單的畫出移動及按鍵的控制..
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Snake extends JFrame implements Runnable {
private PaintPanel paintPanel = new PaintPanel();
private boolean isRunning = true;
private long[] sleepTime = {500, 400, 300, 200, 100};
private int dir = KeyEvent.VK_RIGHT;
static Snake s = null;
private LinkedList<Point> snakeList = new LinkedList<Point>();
private int resolution = 20;
public Snake() {
super("Java Snake");
setSize(500, 500);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(((int)dim.getWidth() - 500) / 2, ((int)dim.getHeight() - 500) / 2);
initComponent();
initSnake();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initSnake() {
snakeList.add(new Point(7,0));
snakeList.add(new Point(6,0));
snakeList.add(new Point(5,0));
snakeList.add(new Point(4,0));
snakeList.add(new Point(3,0));
snakeList.add(new Point(2,0));
snakeList.add(new Point(1,0));
snakeList.add(new Point(0,0));
}
private void initComponent() {
setLayout(new BorderLayout());
add(paintPanel);
addKeyListener(new SnakeKeyListener());
addComponentListener(new ComponentListener() {
public void componentHidden(ComponentEvent arg0) {
}
public void componentMoved(ComponentEvent arg0) {
}
public void componentResized(ComponentEvent arg0) {
Dimension d = s.getSize();
if(d.width <= 500) s.setSize(500, 500);
if(d.height <= 500) s.setSize(500, 500);
}
public void componentShown(ComponentEvent arg0) {
}
});
}
public void run() {
while(isRunning) {
try {
Thread.sleep(sleepTime[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
checkBoundary();
moveSnake();
paintPanel.repaint();
}
}
private void moveSnake() {
Point p = snakeList.get(0);
Point newPoint = null;
switch(dir) {
case KeyEvent.VK_RIGHT:
newPoint = new Point(p.x + 1, p.y);
snakeList.addFirst(newPoint);
break;
case KeyEvent.VK_LEFT:
newPoint = new Point(p.x - 1, p.y);
snakeList.addFirst(newPoint);
break;
case KeyEvent.VK_UP:
newPoint = new Point(p.x, p.y - 1);
snakeList.addFirst(newPoint);
break;
case KeyEvent.VK_DOWN:
newPoint = new Point(p.x, p.y + 1);
snakeList.addFirst(newPoint);
break;
}
snakeList.removeLast();
}
private void checkBoundary() {
}
public static void main(String[] args) {
s = new Snake();
Thread d = new Thread(s);
d.start();
}
private final class SnakeKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent arg0) {
int keyDir = arg0.getKeyCode();
if(keyDir != KeyEvent.VK_UP && keyDir != KeyEvent.VK_DOWN &&
keyDir != KeyEvent.VK_RIGHT && keyDir != KeyEvent.VK_LEFT) {
return;
}
if((dir == KeyEvent.VK_UP && keyDir == KeyEvent.VK_DOWN) ||
(dir == KeyEvent.VK_DOWN && keyDir == KeyEvent.VK_UP) ||
(dir == KeyEvent.VK_RIGHT && keyDir == KeyEvent.VK_LEFT) ||
(dir == KeyEvent.VK_LEFT && keyDir == KeyEvent.VK_RIGHT)) {
System.out.println("return");
return;
}
dir = keyDir;
}
}
private final class PaintPanel extends JPanel {
public PaintPanel() {
setBackground(Color.DARK_GRAY);
}
public void paint(Graphics g) {
CleanBackground(g);
PaintSnake(g);
}
private void PaintSnake(Graphics g) {
Dimension d = null;
d = getSize(d);
for(int i = 0; i < snakeList.size(); ++i) {
g.setColor(new Color(0, 255 - i*(255 / (snakeList.size())), 0));
Point p = snakeList.get(i);
g.fill3DRect(
p.x*(int)(d.width / resolution),
p.y*(int)(d.height / resolution),
d.width / resolution, d.height / resolution,
true
);
}
}
private void CleanBackground(Graphics g) {
Dimension d = null;
d = getSize(d);
g.setColor(Color.RED);
g.fillRect(0, 0, d.width, d.height);
g.setColor(Color.DARK_GRAY);
for(int i = 0; i < d.width / resolution; ++i) {
for(int j = 0; j < d.height / resolution; ++j){
g.fill3DRect(
i*(int)(d.width / resolution),
j*(int)(d.height / resolution),
d.width / resolution,
d.height / resolution,
true);
}
}
}
}
}