Java实现的简单的贪吃蛇,没用面向对象思想。后边有用面向对象实现的

Java实现的简单的贪吃蛇,没用面向对象思想。后边有用面向对象实现的

Java实现的简单的贪吃蛇,没用面向对象思想。后边有用面向对象实现的

package snack;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * 贪吃蛇
 * 
 * @author anduo
 * 
 */
public class Snack extends JFrame implements KeyListener, Runnable
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int WIDTH = 20;
	private static final int HEIGHT = 20;
	private static final int SIZE = 20;
	/**
	 * UP和DOWN是偶数,RIGHT和LEFT是奇数
	 */
	private static final int UP = 2;
	private static final int DOWN = 4;
	private static final int LEFT = 1;
	private static final int RIGHT = 3;
	private int direction = 1;

	// 线程的休眠时间
	private int time = 300;

	// 一个linkedlist用于储存蛇节点
	private LinkedList<Point> snack = new LinkedList<Point>();
	// 食物节点
	private static Point food = new Point();

	Random rand = new Random();

	public Snack()
	{
		// 初始化蛇身
		int x = WIDTH / 2;
		int y = HEIGHT / 2;
		for (int i = 0; i < 3; i++)
		{
			Point p = new Point();
			p.x = x++;
			p.y = y;
			snack.add(p);
		}

		// 初始化食物
		food.x = rand.nextInt(18) + 1;
		food.y = rand.nextInt(17) + 2;

		setTitle("贪吃蛇");
		this.addKeyListener(this);
		setDefaultCloseOperation(3);
		setBounds(getToolkit().getScreenSize().width / 2 - 150, getToolkit()
				.getScreenSize().height / 2 - 150, 450, 460);
		this.repaint();
		setVisible(true);
	}

	/**
	 * 重写print方法
	 */

	public void paint(Graphics g)
	{
		// 绘制界面
		// g.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true);
		g.setColor(Color.green);
		for (int i = 0; i < WIDTH; i++)
		{// 绘制了400个方格,背景
			for (int j = 0; j < HEIGHT; j++)
			{
				g.fill3DRect(i * SIZE + 20, j * SIZE + 40, SIZE, SIZE, true);
			}
		}

		// 绘制 蛇
		g.setColor(Color.red);
		for (Point p : snack)
		{
			g.fillRect(p.x * SIZE, p.y * SIZE, SIZE, SIZE);
		}

		// 绘制食物
		g.setColor(Color.black);
		g.fill3DRect(food.x * SIZE, food.y * SIZE, SIZE, SIZE, true);

	}

	/**
	 * 把蛇头跟蛇身的其他所有节点比较,如果相同,就死掉,否则不死
	 * 
	 * @return
	 */
	public boolean isDead()
	{
		boolean dead = false;
		Point head = snack.getFirst();
		for (int i = 1; i < snack.size(); i++)
		{
			if (head.equals(snack.get(i)))
			{
				dead = true;
			}
		}
		return dead;
	}

	/**
	 * 构造一个新的蛇 添加一个蛇头,然后删除蛇尾
	 * 
	 * @param direction
	 */
	private void move(int direction)
	{
		// if ((newDirection + this.direction) == 0)
		// {

		// }
		Point p = snack.getFirst();
		int x = p.x;
		int y = p.y;
		switch (direction)
		{
		// 到达顶端时,从最下方出来;
		// 到达底端时,从最上方出来;
		// 到达左端时,从最右方出来;
		// 到达右端时,从最左方出来;

		case UP:
			y--;
			if (y == 1)
				y = 21;
			break;
		case DOWN:
			y++;
			if (y == 22)
				y = 2;
			break;
		case LEFT:
			x--;
			if (x == 0)
				x = 20;
			break;
		case RIGHT:
			x++;
			if (x == 21)
				x = 1;
			break;
		}

		if (this.isDead())
		{
			JOptionPane.showConfirmDialog(this, "GAME OVER!");
			System.exit(0);
		}

		// 如果蛇不出界,构造新蛇
		if (x < WIDTH + 1 && y < HEIGHT + 2 && x >= 1 && y >= 2)
		{
			p = new Point(x, y);
			if (p.equals(food))
			{// 吃食物,然后把食物点加为头结点

				while (true)
				{// 让食物不出现在蛇的身上
					food.x = rand.nextInt(18) + 1;
					food.y = rand.nextInt(17) + 2;
					if (!snack.contains(food))
						break;
				}
				snack.add(food);
				time -= 10;

			}
			snack.addFirst(p);
			snack.removeLast();

			this.repaint();
		}
	}

	/**
	 * 改变蛇的行进方向
	 * 
	 * @param newDirection
	 */
	public void changeDirection(int newDirection)
	{
		if (this.direction % 2 != newDirection % 2)// 避免冲突
		{
			this.direction = newDirection;
		}
	}

	/**
	 * 从键盘获取UP、DOWN、LEFT、RIGHT 调用move方法处理蛇的移动
	 */
	public void keyPressed(KeyEvent e)
	{
		int code = e.getKeyCode();

		switch (code)
		{
		case KeyEvent.VK_UP:
			this.changeDirection(UP);
			break;
		case KeyEvent.VK_DOWN:
			this.changeDirection(DOWN);
			break;
		case KeyEvent.VK_LEFT:
			this.changeDirection(LEFT);
			break;
		case KeyEvent.VK_RIGHT:
			this.changeDirection(RIGHT);
			break;

		}
	}

	/**
	 * 线程方法,用一个while循环线程时间,让蛇可以按指定方向自动行走
	 */
	public void run()
	{
		while (true)
		{
			try
			{
				Thread.sleep(time);
				this.move(this.direction);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args)
	{
		new Thread(new Snack()).start();
	}

	public void keyTyped(KeyEvent e)
	{
	}

	public void keyReleased(KeyEvent e)
	{
	}

}

你可能感兴趣的:(java,swing,ITeye,J#,UP)