📄 snake.java
字号:
package worm;import java.util.*;import java.util.Random;import java.awt.*;import java.applet.*;import java.net.*;/** * Title: Snake.java * Description: class Snake * Copyright: Copyright (c) 2004 * Company: * @author * @version 1.0 */public class Snake extends Thread{ int colorSize = 6; /* the body of snake*/ ArrayList list; /* the length of snake*/ int length; /* color of snake*/ Color color; /* radius of snake's body (radius of point)*/ int radius = 4; /*the direction of snake's head left ---- 0 right ---- 1 up ---- 2 down ---- 3 */ int direction =0; public Snake(Runnable target) { super(target); init(); } public Snake() { init(); } /* snake dead play some sound */ public void dead() { try { URL codeBase = new URL("file:" + System.getProperty("user.dir") + "/fail.wav"); AudioClip audioClip = Applet.newAudioClip(codeBase); audioClip.play(); } catch(MalformedURLException ex) { } } /* paint the whole snake, by paint all the point int the snake's body*/ public void paint(Graphics g) { int index; for(index=0;index<this.length; index++) { CPoint p = (CPoint)list.get(index); p.show(); p.paint(g); } } /* set the direction of snake's head*/ public void setDirecotion(int direct) { this.direction = direct; } /* the snake's head will be add in the direction of head if parameter grow is true the original tail will not be cut else the original tail will be cut parameter grow will be get from the function eatPoint(CPoint pt), which is called by ScreenPanel Instance(panel, declared in WormFrame.java) */ public boolean move(boolean grow) { boolean stoped = false; this.addHeadPoint(); CPoint pt = (CPoint)this.list.get(0); /* if the snake hit something, it snake will be dead and the snake thread will be stop */ if(this.hit()) { this.dead() ; stoped = true; //this.stop() ; } if(!grow) { this.removeTailPoint() ; } return stoped; } /* testing wheather snake can eat food point (parameter-pt)*/ public boolean eatPoint(CPoint foodPoint) { boolean eat = false; CPoint ptHead = (CPoint) list.get(0); int distance2 = (ptHead.getX()-foodPoint.getX())*(ptHead.getX()-foodPoint.getX())+ (ptHead.getY()-foodPoint.getY())*(ptHead.getY()-foodPoint.getY()); if(distance2< (ptHead.getRadius()+foodPoint.getRadius())*(ptHead.getRadius()+foodPoint.getRadius())) { eat = true; foodPoint.hide(); color = foodPoint.getColor(); } return eat; } /* get the radius of snake's body*/ public int getRadius() { return this.radius ; } void init() { if (list==null); list = new ArrayList(); list.clear(); snakeRandomColor(); /* add the first point to list*/ CPoint p = new CPoint(200,200,radius,color); list.add(length,p); length++; /* add another point to list */ this.addTailPoint(1); this.addTailPoint(1); this.addTailPoint(1); this.addTailPoint(1); } /*the color of snake can be changed in unsame time*/ private void snakeRandomColor() { int index = (int)(Math.random()*this.colorSize ); switch(index) { case 0: color = Color.red ; break; case 1: color = Color.yellow ; break; case 2: color = Color.blue ; break; case 3: color = Color.green ; break; case 4: color = Color.pink; break; case 5: color = Color.white ; break; } } /* add head point follow by head direction - direction property in the class */ private void addHeadPoint() { //list 可能为null CPoint p = (CPoint)list.get(0); int newX =p.getX(); int newY = p.getY() ; switch(direction) { case 1: newX = newX+2*this.radius; break; //right add tail case 2: newY = newY-2*this.radius; break; //up add tail case 3: newY = newY+2*this.radius ; break; //down add tail default: newX = newX-2*this.radius ; break; //left add tail } CPoint headPoint = new CPoint(newX,newY,this.radius,this.color); list.add(headPoint); for(int index=this.length; index>0; index--) { list.set(index,(CPoint)list.get(index-1)); } list.set(0,headPoint); length++; } /* add tail point follow by tail direction */ private void addTailPoint(int tailDirection) { CPoint p = (CPoint)list.get(this.length-1); int newX =p.getX(); int newY = p.getY() ; switch(tailDirection) { case 1: newX = newX+2*this.radius; break; //right add tail case 2: newY = newY-2*this.radius; break; //up add tail case 3: newY = newY+2*this.radius ; break; //down add tail default: newX = newX-2*this.radius ; break; //left add tail } //System.out.println("X:"+String.valueOf(newX)+" Y:"+String.valueOf(newY)); CPoint tailPoint = new CPoint(newX,newY,this.radius,this.color); list.add(length,tailPoint); length++; } private void removeTailPoint() { list.remove(length-1); length--; } private boolean hit() { return (this.hitBorder()||this.hitItself() ); } /* testing if the snake's head hit the border of the screen */ private boolean hitBorder() { CPoint pt = (CPoint) this.list .get(0); int x = pt.getX() ; int y = pt.getY(); if (x-this.radius<=0 || x+this.radius>600|| y-this.radius<=0 || y+this.radius>400) return true; return false; } /* testing if the snake's head hit itself */ private boolean hitItself() { CPoint ptHead = (CPoint) this.list.get(0); int headX = ptHead.getX() ; int headY = ptHead.getY() ; for(int index = 1; index<this.length ; index++) { CPoint pt = (CPoint)this.list.get(index); if(pt.getX() == headX && pt.getY() ==headY) { return true; } } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -