java贪吃蛇的问题,弄了半天蛇身体只有一个矩形,

独自存货 发布于 2014/06/08 10:07
阅读 818
收藏 1
捣鼓了半天也不知道怎么回事,可能是初始化的问题,求大神指点啊!
package snake.box;

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 snake.listener.SnakeListener;

public class Snake {
	//身体
	LinkedList<Point> body =new LinkedList<>();
	
	//方向
	private static int direction;
	//方向常量
	public static final int UP=1;
	public static final int DOWN=-1;
	public static final int LEFT=2;
	public static final int RIGHT=-2;
	
	

	//监听器
	Set<SnakeListener> listeners =new HashSet<>();
	
	/*
	 * 构造器
	 */
	public Snake(){
		init();
	}
	/*
	 * 初始化
	 */
	public void init(){
		int x=(Rec.Width*Rec.Cell_Size)/2;
		int y=(Rec.height*Rec.Cell_Size)/2;
		for(int i=0;i<3;i++){
			this.body.addFirst(new Point(x--,y));
			
		}
		this.direction=Snake.LEFT;
	}
	
	
	public void move(){
		body.removeLast();
		int x=body.getFirst().x;
		int y=body.getFirst().y;
		switch(this.direction){
		case Snake.UP:
			y--;
			break;
		case Snake.DOWN:
			y++;
			break;
		case Snake.LEFT:
			x--;
			break;
		case Snake.RIGHT:
			x++;
			break;
		}
		body.addFirst(new Point(x,y));
	}
	
	public void changeDirection(int direction){
			this.direction=direction;
	}
	
	public void eatFood(){
		
	}
	public void eatBody(){
		
	}
	public void draw(Graphics g){
		Color c=g.getColor();
		
		for(Point p :body){
			g.setColor(Color.black);
			g.fill3DRect(p.x, p.y, Rec.Cell_Size, Rec.Cell_Size,true);
		}
		g.setColor(c);
	}
	
	public void addListener(SnakeListener l){
		if(l!=null){
			listeners.add(l);
		}
	}
	
	public void begin(){
		new Thread(new SnakeDriver()).start();
	}
	
	class SnakeDriver implements Runnable{
	
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					try {
						move();
						for(SnakeListener listener :listeners){
							listener.SnakeMoved(Snake.this);
						}
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		}
}

package snake.control;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import snake.box.Food;
import snake.box.Rec;
import snake.box.Snake;
import snake.box.Stone;
import snake.listener.SnakeListener;
import snake.view.GamePanel;

public class Control extends KeyAdapter implements SnakeListener {
	Food food=null;
	Snake snake=null;
	Stone stone =null;
	GamePanel gamepanel= null;
	
	
	
	
	public Control(Food food, Snake snake, Stone stone, GamePanel gamepanel) {
		super();
		this.food = food;
		this.snake = snake;
		this.stone = stone;
		this.gamepanel = gamepanel;
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		int keycode=e.getKeyCode();
		switch(keycode){
		case KeyEvent.VK_UP:
			snake.changeDirection(Snake.UP);
			break;
		case KeyEvent.VK_DOWN:
			snake.changeDirection(Snake.DOWN);
			break;
		case KeyEvent.VK_LEFT:
			snake.changeDirection(Snake.LEFT);
			break;
		case KeyEvent.VK_RIGHT:
			snake.changeDirection(Snake.RIGHT);
			break;
		}
		
	}

	public void newGame(){
		snake.begin();
	}


	@Override
	public void SnakeMoved(Snake snake) {
		gamepanel.disPlay(food, snake, stone);
		
	}
	

}



package snake.view;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import snake.box.Food;
import snake.box.Rec;
import snake.box.Snake;
import snake.box.Stone;

public class GamePanel extends JPanel {
	Food food=null;
	Snake snake=null;
	Stone stone=null;
	
	public void disPlay(Food food,Snake snake,Stone stone){
		this.food=food;
		this.snake=snake;
		this.stone=stone;
		repaint();
	}
	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(Color.pink);
		g.fillRect(0, 0, Rec.Width*Rec.Cell_Size, Rec.height*Rec.Cell_Size);
		//this.food.draw();
		this.snake.draw(g);
		//this.stone.draw();
		setVisible(true);
	
	}
	
}



package snake.game;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import snake.box.Food;
import snake.box.Rec;
import snake.box.Snake;
import snake.box.Stone;
import snake.control.Control;
import snake.view.GamePanel;

public class gameTest {
	public static void main(String[] args) {
		Food food = new Food();
		Snake snake = new Snake();
		Stone stone = new Stone();
		GamePanel gamepanel=new GamePanel();
		Control control = new Control(food, snake, stone, gamepanel);
		
		JFrame f =new JFrame("贪吃蛇");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setLayout(new BorderLayout());
		f.add(gamepanel,BorderLayout.CENTER);
		f.addKeyListener(control);
		f.setVisible(true);
		f.setSize(Rec.Width*Rec.Cell_Size,
				Rec.height*Rec.Cell_Size);
		snake.addListener(control);
		control.newGame();
		
	}
}






加载中
返回顶部
顶部