贪吃蛇:看了传智博客的视频整理出来的代码
Snake类
package snake;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
public class Snake {
//定义方向的常量
public static final int UP=-1;
简单的java游戏代码 public static final int DOWN=1;
public static final int LEFT=2;
public static final int RIGHT=-2;
private int oldDirection,newDirection;
private Point oldTail;
private boolean life;
private LinkedList<Point> body=new LinkedList<Point>(); //蛇的坐标
private Set<SnakeListener> listener=new HashSet<SnakeListener>();
//蛇没身体 要初始化
public Snake(){
init();
}
public void init(){
//显示区最中间点
int x=Global.WIDTH/2;
int y=Global.HEIGHT/2;
//初始化身体节点
for(int i=0;i<3;i++){
//添加节点
body.addLast(new Point(x--,y));//蛇头在右边 默认方向为右
oldDirection=newDirection=RIGHT;
life=true;
}
}
public void move(){
System.out.println("Snake's move");
if(!((oldDirection+newDirection)==0)){
oldDirection=newDirection;
}
//1. 去尾
veLast();
int First().x; //原坐标
int First().y;
switch(oldDirection){
case UP:
y--;
if(y<0){
y=Global.HEIGHT-1;
}
break;
case DOWN:
y++;
if(y>=Global.HEIGHT){
y=0;
}
break;
case LEFT:
x--;
if(x<0){
x=Global.WIDTH-1;
}
break;
case RIGHT:
x++;
if(x>=Global.WIDTH){
x=0;
}
break;
}
Point newHead=new Point(x,y);
//2. 加头
body.addFirst(newHead);
}
public void changeDirection(int direction){
System.out.println("Snake's changeDirection");
/*if(!(direction+this.direction==0)){
this.direction=direction;
}*/
newDirection=direction;
}
public void die(){
life=false;
}
public void eatFood(){
System.out.println("Snake's eatFood");
body.addLast(oldTail);
}
public boolean isEatBody(){
System.out.println("Snake's isEatBody");
for(int i=1;i<body.size();i++){
(i).Head()))
return true;
}
return false;
}
//显示
public void drawMe(Graphics g){
System.out.println("Snake's drawMe");
g.setColor(Color.BLUE);//蛇身颜
//遍历
for(Point p: body)
g.fill3DRect(p.x*Global.CELL_SIZE, p.y*Global.CELL_SIZE,Global.CELL_SIZE , Global.CELL_SIZE,true);
}
//得到蛇头的方法
public Point getHead(){
First();
}
//内部类
private class SnakeDriver implements Runnable{
public void run(){
while(life){
move();
for(SnakeListener l:listener){
l.snakeMove(Snake.this);
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
//启动线程
public void start(){
new Thread(new SnakeDriver()).start();
}
public void addSnakeListener(SnakeListener l){
if(l!=null){
this.listener.add(l);
}
}
}
Food类
package snake;
import java.awt.Graphics;
import java.awt.Point;
public class Food extends Point {
public void newFood(Point p){
this.setLocation(p);
}
public boolean isSnakeEatFood(Snake snake){
System.out.println("Food's isAnakeEatFood");
return this.Head());
//return false;
}
public void drawMe(Graphics g){
System.out.println("Food's drawMe");
//g.fillOval(x*Global.WIDTH, y*Global.HEIGHT, Global.CELL_SIZE,Global.CELL_SIZE);
g.fill3DRect(x*Global.CELL_SIZE, y*Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
Ground类
package snake;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
public class Ground {
private int[][] rocks=new int[Global.WIDTH][Global.HEIGHT];
//生成墙
public Ground(){
for(int y=0;y<Global.WIDTH;y++){
rocks[0][y]=1;
rocks[Global.HEIGHT-1][y]=1;
}
for(int x=0;x<Global.WIDTH;x++){
rocks[x][0]=1;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论