⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snakepanel.java

📁 java贪吃蛇游戏编程
💻 JAVA
字号:
package snake;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

import java.util.Random;
import java.util.Vector;

public class SnakePanel extends JPanel implements Runnable, KeyListener {
	private int gridNumx = 40;
	private int gridNumy = 40;
	private int length = 5; // 蛇身体的长度
	public static final int LEFT = 0;
	public static final int RIGHT = 1;
	public static final int TOP = 2;
	public static final int BUTTOM = 3;
	public static final int START = 0;
	public static final int PAUSE = 1;
	public static final int RESTART = 2;
	public static final int STOP=3;
	private int time = 200;
	private Point head = new Point(0, length - 1);
	private Point tail = new Point(0, 0);
	private Point food = new Point();
	private int direction = RIGHT;
	private int control = PAUSE;
	
	//存放蛇的节点,还没有想出有没有更好的方法,可能也就是这个方法吧
	private Vector<Point> nodes = new Vector<Point>();
	JButton[][] jb = new JButton[gridNumx][gridNumy];

	SnakePanel() {
		GridLayout gl = new GridLayout(gridNumx, gridNumy);
		setLayout(gl);
		
		//获取焦点,很关键的一步,因为它本人郁闷的一阵子
		setFocusable(true);
		this.addKeyListener(this);
		for (int i = 0; i < gridNumx; i++) {
			for (int j = 0; j < gridNumy; j++) {
				jb[i][j] = new JButton();
				jb[i][j].setVisible(false);
				add(jb[i][j]);
			}
		}
		for (int i = 0; i < length; i++) {
			jb[0][i].setVisible(true);
			nodes.add(new Point(0, i));
		}
	}
	
	public void start(){
		for (int i = 0; i < length; i++) {
			jb[0][i].setVisible(true);
			nodes.add(new Point(0, i));
		}
		if(getDirection()!=LEFT)
			right();
	}

	public void run() {
		/*
		 * 线程的利用,只要用一个线程既可以
		 */
		generaterFood();
		while (true) {
			try {
				Thread.sleep(time);
				if (getControl() == START) {
					switch (getDirection()) {
					case LEFT:
						left();
						break;
					case RIGHT:
						right();
						break;
					case TOP:
						up();
						break;
					case BUTTOM:
						down();
						break;
					}
				}else if(getControl()==PAUSE){
				}
				else if(getControl()==STOP){
					JOptionPane.showMessageDialog(this, "游戏结束");
					setControl(RESTART);
				}else if(getControl()==RESTART){
					restart();
					setControl(PAUSE);
				}
			} catch (InterruptedException e) {
				System.out.println(e.toString());
			}
		}
	}

	public void restart() {	
		if(getControl()==RESTART){
			clear();
			setDirection(RIGHT);
			nodes.removeAllElements();
			generaterFood();
			for (int i = 0; i < length; i++) {
				jb[0][i].setVisible(true);
				nodes.add(new Point(0, i));
			}
		}
	}

	public void clear() {
		for (int i = 0; i < gridNumx; i++) {
			for (int j = 0; j < gridNumy; j++) {
				jb[i][j].setVisible(false);
			}
		}
		setHead(new Point(0, length - 1));
		setTail(new Point(0, 0));
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public void left() {
		if (nodes.lastElement().y > 0) {
			tail = nodes.firstElement();
			head = nodes.lastElement();
			if (nodes.contains(new Point(head.x, head.y - 1))) {
				setControl(STOP);
			} else if (head.y - 1 == food.y && head.x == food.x) {
				jb[head.x][head.y - 1].setVisible(true);
				nodes.add(new Point(head.x, head.y - 1));
				generaterFood();
			} else {
				jb[head.x][head.y - 1].setVisible(true);
				jb[tail.x][tail.y].setVisible(false);
				nodes.removeElementAt(0);
				nodes.add(new Point(head.x, head.y - 1));
			}
		} else {
			setControl(STOP);
		}
	}

	public void right() {
		if (nodes.lastElement().y < gridNumy - 1) {
			tail = nodes.firstElement();
			head = nodes.lastElement();
			if (nodes.contains(new Point(head.x, head.y + 1))) {
				setControl(STOP);
			} else if (head.y + 1 == food.y && head.x == food.x) {
				jb[head.x][head.y + 1].setVisible(true);
				nodes.add(new Point(head.x, head.y + 1));
				generaterFood();
			} else {
				jb[tail.x][tail.y].setVisible(false);
				jb[head.x][head.y + 1].setVisible(true);
				nodes.removeElementAt(0);
				nodes.add(new Point(head.x, head.y + 1));
			}
		} else {
			setControl(STOP);
		}
	}

	public void up() {
		if (nodes.lastElement().x > 0) {
			tail = nodes.firstElement();
			head = nodes.lastElement();
			if (nodes.contains(new Point(head.x - 1, head.y))) {
				setControl(STOP);
			} else if (head.y == food.y && head.x - 1 == food.x) {
				jb[head.x - 1][head.y].setVisible(true);
				nodes.add(new Point(head.x - 1, head.y));
				generaterFood();
			} else {
				jb[tail.x][tail.y].setVisible(false);
				jb[head.x - 1][head.y].setVisible(true);
				nodes.removeElementAt(0);
				nodes.add(new Point(head.x - 1, head.y));
			}
		} else {
			setControl(STOP);
		}
	}

	public void down() {
		if (nodes.lastElement().x < gridNumx - 1) {
			tail = nodes.firstElement();
			head = nodes.lastElement();
			if (nodes.contains(new Point(head.x + 1, head.y))) {
				setControl(STOP);
			} else if (head.y == food.y && head.x + 1 == food.x) {
				jb[head.x + 1][head.y].setVisible(true);
				nodes.add(new Point(head.x + 1, head.y));
				generaterFood();
			} else {
				jb[tail.x][tail.y].setVisible(false);
				jb[head.x + 1][head.y].setVisible(true);
				nodes.removeElementAt(0);
				nodes.add(new Point(head.x + 1, head.y));
			}
		} else {
			setControl(STOP);
		}
	}

	public JButton[][] getButtons() {
		return this.jb;
	}

	public static void main(String[] args) {
		JFrame jf = new JFrame("测试");
		SnakePanel sp = new SnakePanel();
		jf.add(sp);
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public Point getHead() {
		return head;
	}

	public void setHead(Point head) {
		this.head = head;
	}

	public Point getTail() {
		return tail;
	}

	public void setTail(Point tail) {
		this.tail = tail;
	}

	// 判断蛇尾的位置,并赋值给蛇尾

	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			if (getDirection() != BUTTOM) {
				setDirection(TOP);
			}
		} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			if (getDirection() != TOP)
				setDirection(BUTTOM);
		} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			if (getDirection() != RIGHT)
				setDirection(LEFT);
		} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if (getDirection() != LEFT)
				setDirection(RIGHT);
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			setControl(START);
		} else {
		}
	}

	public void keyReleased(KeyEvent arg0) {
	}

	public void keyTyped(KeyEvent arg0) {
	}

	// 用递归的算法生成食物
	public void generaterFood() {
		Random random = new Random();
		food.x = random.nextInt(gridNumx);
		food.y = random.nextInt(gridNumy);
		if (jb[food.x][food.y].isVisible()) {
			generaterFood();
		} else {
			jb[food.x][food.y].setVisible(true);
		}
	}

	public int getControl() {
		return control;
	}

	public void setControl(int control) {
		this.control = control;
	}

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -