Java贪吃蛇程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package Package1;
import java.awt.Graphics;
import java.awt.Point;
import PackageTool.Global;
public class Food extends Point{
public void newFood(Point p){
this.setLocation(p);
}
public boolean isSnakeEatFood(Snake snake){
System.out.println("Food's isSnakeEatFood");
return this.equals(snake.getHead());
}
public void drawMe(Graphics g){
System.out.println("Food's drawMe");
g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE,
Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
package Package1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
import PackageTool.Global;
public class Ground {
private int[][] rocks = new int[Global.WIDTH][Global.HEIGHT];
public Ground(){
for(int x = 0;x rocks[x][0]=1; rocks[x][Global.HEIGHT-1] = 1; } } public boolean isSnakeEatRock(Snake snake){ System.out.println("Ground's isSnakeEatRoce"); for(int x =0 ;x for(int y = 0;y if((rocks[x][y]==1)&&(x == snake.getHead().x &&y ==snake.getHead().y)){ return true; } } } return false; } public Point getPoint(){ Random random = new Random(); int x = 0,y = 0; do{ x = random.nextInt(Global.WIDTH); y = random.nextInt(Global.HEIGHT); } while(rocks[x][y]==1); return new Point(x,y); } public void drawMe(Graphics g){ System.out.println("Ground's drawMe"); for(int x = 0;x for(int y = 0;y if(rocks[x][y] ==1){ g.setColor(Color.gray); g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, false); } } } } } package Package1; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.HashSet; import java.util.LinkedList; import java.util.Set; import PackageListener.SnakeListener; import PackageTool.Global; public class Snake { private Set private LinkedList public int oldDirection,newDirection; public static final int UP = -1; public static final int DOWN = 1; public static final int LEFT = 2; public static final int RIGHT = -2; public static boolean gameover; private Point oldTail; public boolean life = true; public Snake(){ init(); } public void init(){ int x = Global.HEIGHT /2; int y = Global.HEIGHT/2; for(int i= 0;i<3;i++){ body.addFirst(new Point(x--,y)); } oldDirection=newDirection = LEFT;//新旧方向复制 } public void die(){ life = false; gameover = true;