import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
/**
* ´ú±íÉß
* @author Administrator
* @download http://www.codefans.net
*/
public class Snake {
private Node head = null;
private Node tail = null;
private int size = 0;
private Node n = new Node(20, 30, Dir.L);
private Yard y;
public Snake(Yard y) {
head = n;
tail = n;
size = 1;
this.y = y;
}
public void addToTail() {
Node node = null;
switch(tail.dir) {
case L :
node = new Node(tail.row, tail.col + 1, tail.dir);
break;
case U :
node = new Node(tail.row + 1, tail.col, tail.dir);
break;
case R :
node = new Node(tail.row, tail.col - 1, tail.dir);
break;
case D :
node = new Node(tail.row - 1, tail.col, tail.dir);
break;
}
tail.next = node;
node.prev = tail;
tail = node;
size ++;
}
public void addToHead() {
Node node = null;
switch(head.dir) {
case L :
node = new Node(head.row, head.col - 1, head.dir);
break;
case U :
node = new Node(head.row - 1, head.col, head.dir);
break;
case R :
node = new Node(head.row, head.col + 1, head.dir);
break;
case D :
node = new Node(head.row + 1, head.col, head.dir);
break;
}
node.next = head;
head.prev = node;
head = node;
size ++;
}
public void draw(Graphics g) {
if(size <= 0) return;
move();
for(Node n = head; n != null; n = n.next) {
n.draw(g);
}
}
private void move() {
addToHead();
deleteFromTail();
checkDead();
}
private void checkDead() {
if(head.row < 2 || head.col < 0 || head.row > Yard.ROWS || head.col > Yard.COLS) {
y.stop();
}
for(Node n = head.next; n != null; n = n.next) {
if(head.row == n.row && head.col == n.col) {
y.stop();
}
}
}
private void deleteFromTail() {
if(size == 0) return;
tail = tail.prev;
tail.next = null;
}
private class Node {
int w = Yard.BLOCK_SIZE;
int h = Yard.BLOCK_SIZE;
int row , col;
Dir dir = Dir.L;
Node next = null;
Node prev = null;
Node(int row, int col, Dir dir) {
this.row = row;
this.col = col;
this.dir = dir;
}
void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillRect(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h);
g.setColor(c);
}
}
public void eat(Egg e) {
if(this.getRect().intersects(e.getRect())) {
e.reAppear();
this.addToHead();
y.setScore(y.getScore() + 5);
}
}
private Rectangle getRect() {
return new Rectangle(Yard.BLOCK_SIZE * head.col, Yard.BLOCK_SIZE * head.row, head.w, head.h);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_LEFT :
if(head.dir != Dir.R)
head.dir = Dir.L;
break;
case KeyEvent.VK_UP :
if(head.dir != Dir.D)
head.dir = Dir.U;
break;
case KeyEvent.VK_RIGHT :
if(head.dir != Dir.L)
head.dir = Dir.R;
break;
case KeyEvent.VK_DOWN :
if(head.dir != Dir.U)
head.dir = Dir.D;
break;
}
}
}
最近下载更多
微信网友_7556302635110400 LV1
6月16日
鲁一宇 LV5
2024年12月24日
mechanic LV1
2024年4月11日
雨梦岚 LV1
2024年3月3日
Hao Pan LV1
2024年1月5日
心比扎软 LV1
2023年12月21日
ClydeSon LV5
2023年12月18日
微信网友_6770780673069056 LV2
2023年12月9日
steven101 LV1
2023年8月4日
zwj2750514489 LV1
2023年6月29日
最近浏览更多
微信网友_7556302635110400 LV1
6月16日
鲁一宇 LV5
2024年12月24日
xiao peppa
2024年11月10日
暂无贡献等级
1661950467 LV2
2024年10月12日
3456788 LV1
2024年7月4日
188073301 LV1
2024年6月15日
qweasd669785
2024年6月14日
暂无贡献等级
liqizuiai
2024年5月20日
暂无贡献等级
微信网友_6979151039729664 LV2
2024年5月5日
Dominick LV14
2024年4月29日

