package worm1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import javax.swing.JPanel;
public class WormPanel extends JPanel {
private Worm worm;
private MoveListener listener = new MoveListener();
private Set<Node> foods = new HashSet<Node>();
public int n = 5;
public int t = 1000;
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public int getT() {
return t;
}
public void setT(int t) {
this.t = t;
}
public void setFoods(Set<Node> foods) {
this.foods = foods;
}
private Service s = new Service();
public WormPanel() {
setWorm(new Worm());
foods = initFoods(n);
addKeyListener(listener);
s.start();
}
public Worm getWorm() {
return worm;
}
public Set<Node> initFoods(int n) {
Set<Node> foods = new HashSet<Node>();
Random r = new Random();
for (;;) {
int x = r.nextInt(480) / 10 * 10 + 10;
int y = r.nextInt(360) / 10 * 10 + 10;
if (getWorm().getLinkListWorm().contains(new Node(x, y))) {
continue;
}
foods.add(new Node(x, y));
if (foods.size() == n) {
break;
}
}
return foods;
}
// 运行界面
public void paint(Graphics g) {
super.paint(g);
this.requestFocus();
this.setBackground(Color.black);
g.setColor(Color.gray);
g.fill3DRect(0, 0, 550, 10, false);
g.fill3DRect(500, 0, 10, 410, false);
g.fill3DRect(0, 370, 550, 10, false);
g.fill3DRect(0, 0, 10, 410, false);
for (int i = 0; i < getWorm().getLinkListWorm().size(); i++) {
g.setColor(Color.cyan);
g.fill3DRect(getWorm().getLinkListWorm().get(i).getX(), getWorm()
.getLinkListWorm().get(i).getY(), 10, 10, false);
}
if (foods.size() == 0) {
foods = initFoods(++n);
t = t - 200;
}
Iterator<Node> food = foods.iterator();
g.setColor(Color.GREEN);
while (food.hasNext()) {
Node f = food.next();
g.fill3DRect(f.getX(), f.getY(), 10, 10, false);
}
}
// 结束界面
public void paintEnd(Graphics g) {
g.setColor(Color.red);
g.setFont(new Font(TOOL_TIP_TEXT_KEY, ERROR, 30));
g.clearRect(0, 150, 550, 100);
g.setColor(Color.BLUE);
g.fillRect(0, 150, 550, 100);
g.clearRect(150, 180, 200, 40);
g.setColor(Color.red);
g.drawString(" Game Over!", 150, 210);
Service.interrupted();
// removeKeyListener(listener);
}
// 键盘监听器
class MoveListener extends KeyAdapter {
int dir;
public void keyPressed(KeyEvent e) {
System.out.println("您按下的是 " + e.getKeyChar());
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
dir = Worm.UP;
break;
case KeyEvent.VK_DOWN:
dir = Worm.DOWN;
break;
case KeyEvent.VK_LEFT:
dir = Worm.LEFT;
break;
case KeyEvent.VK_RIGHT:
dir = Worm.RIGHT;
break;
}
System.out.println(getWorm());
System.out.println("dir=" + dir + ",worm.dir=" + getWorm().dir);
step(dir, getWorm());
}
}
// 走一步
public void step(int dir, Worm worm) {
if (dir + worm.dir == 0) {
paintEnd(WormPanel.this.getGraphics());
worm.dir = 100;
return;
} else {
if (worm.dir == 100) {
paintEnd(WormPanel.this.getGraphics());
return;
}
if (worm.dir == -100) {
return;
}
Node head = worm.getLinkListWorm().get(0);
int x = head.getX() + dir / 10 * 10;
int y = head.getY() + dir % 10 * 10;
Node n = new Node(x, y);
if (foods.contains(n) && !worm.getLinkListWorm().contains(n)) {
foods.remove(n);
worm.getLinkListWorm().addFirst(n);
worm.dir = dir;
} else {
try {
worm.step(dir);
} catch (Exception e) {
paintEnd(WormPanel.this.getGraphics());
return;
}
}
WormPanel.this.repaint();
}
}
public void setWorm(Worm worm) {
this.worm = worm;
}
class Service extends Thread {
public void run() {
try {
for (;;) {
Thread.sleep(t);
step(getWorm().dir, getWorm());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}