Java 实现简单的贪吃蛇

  • 源码

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class SnakePanel extends JPanel implements KeyListener, Runnable{
	private static final long serialVersionUID = 1L;
	
	public static Image background;
	public static Image cellImage;
	public static Image foolImage;
	ArrayList snakeList = new ArrayList();
	private Cell food = new Cell();
	//方向
	public char direction = Direction.RIGHT;
	public boolean isLive = true;
	//速度
	public int sleep = 100;
	
	//横向35个
	//纵向35个
	public static final int MIN_X = 50 + 2;
	public static final int MAX_X = MIN_X + 10 * 35 - 10;//350 - 10 + 2
	public static final int MIN_Y = 50 - 2;
	public static final int MAX_Y = MIN_Y + 10 * 35 - 10;//350 - 10 - 2
//	public static final int edge = 10;
	
	public SnakePanel() {
		background = Toolkit.getDefaultToolkit().createImage("bg.png");
		cellImage = Toolkit.getDefaultToolkit().createImage("cell.png");
		foolImage = Toolkit.getDefaultToolkit().createImage("food.png");
		
		//初始化蛇
		int x = MIN_X; 
		int y = MIN_Y;
		for (int i = 0; i < 10; i++) {
			Cell cell = new Cell(x, y);
			snakeList.add(cell);
			x += 10;
		}
		
		//初始化食物
		getFood();
		
	}
	
	//创建食物
	public void getFood() {
		boolean flag;
		int x, y;
		do{
			flag = false;
			x = (int)(Math.random() * 35) * 10 + 52;
			y = (int)(Math.random() * 35) * 10 + 48;
			for (Cell cell : snakeList) {
				if (cell.getX() == x && cell.getY() == y) {
					flag = true;
					break;
				}
			}
		}while(flag);
		food.setX(x);
		food.setY(y);
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		
		g.drawImage(background, 0, 0, this);
		//g.draw3DRect(50, 50 - 2, 350, 350 - 2, true);
		//g.draw3DRect(50, 50 - 2, 10, 10, false);
		
		for (Cell cell : snakeList) {
			g.drawImage(cellImage, cell.x , cell.y, this);
		}
		g.drawImage(foolImage, food.x , food.y, this);
		//g.translate(x, y):将坐标原点移动到(x,y)
		if (!isLive){
			int x = (MIN_X + MAX_X) / 2;
			int y = (MIN_Y + MAX_Y) / 2;
			g.setColor(Color.red);
			g.drawString("Game over !!!", x, y);
			//死亡信息
		}
		
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setTitle("小破蛇");
		frame.setSize(470, 480);
		frame.setVisible(true);
		//设置窗口居中显示
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SnakePanel sp = new SnakePanel();
		frame.add(sp);
		//frame.setContentPane(new SnakePanel());
		
		//注册事件监听
		frame.addKeyListener(sp);
		
		//线程
		new Thread(sp).start();
		
	}
	
	public class Direction {
		public static final char UP = 'w';
		public static final char DOWN = 's';
		public static final char LEFT = 'a';
		public static final char RIGHT = 'd';
	}
	
	public class Cell {
		private int x;
		private int y;
		
		public Cell() {
		}
		
		public Cell(int x, int y) {
			this.x = x;
			this.y = y;
		}
		
		public int getX() {
			return x;
		}
		public void setX(int x) {
			this.x = x;
		}
		public int getY() {
			return y;
		}
		public void setY(int y) {
			this.y = y;
		}
	}

	//键盘控制方向
	//按下
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP) {
			if(direction != Direction.DOWN)direction = Direction.UP;
		} else if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT) {
			if(direction != Direction.RIGHT)direction = Direction.LEFT;
		} else if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(direction != Direction.UP)direction = Direction.DOWN;
		} else if(e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(direction != Direction.LEFT)direction = Direction.RIGHT;
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		
	}

	//按下并释放(敲击)
	@Override
	public void keyTyped(KeyEvent e) {
		
	}

	//线程:重画
	@Override
	public void run() {
		while(true) {
			try {
				Thread.sleep(sleep);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			int x = snakeList.get(snakeList.size() - 1).x;
			int y = snakeList.get(snakeList.size() - 1).y;
			switch(direction) {
				case Direction.DOWN:
					y += 10;
					break;
				case Direction.LEFT:
					x -= 10;
					break;
				case Direction.RIGHT:
					x += 10;
					break;
				case Direction.UP:
					y -= 10;
					break;
			}
			//撞墙
			if (x < MIN_X || x > MAX_X || y < MIN_Y || y > MAX_Y) {
				//提示处理
				isLive = false;
				break;
			}
			
			boolean flag = false;
			for(Cell cell : snakeList) {
				if (cell.getX() == x && cell.getY() == y) {
					flag = true;
					break;
				}
			}
			//撞自己
			if (flag) {
				//提示处理
				isLive = false;
				break;
			}
			
			//吃食物
			if (x == food.x && y == food.y) {
				//添加长度
				Cell cell = new Cell(x, y);
				snakeList.add(cell);
				//重新生成食物
				getFood();
			}
			else {
				//蛇头前进
				Cell cell = new Cell(x, y);
				snakeList.add(cell);
				//蛇尾前进
				snakeList.remove(0);
			}
			this.repaint();
		}
		this.repaint();
		
	}
	
}

  • 效果

Java 实现简单的贪吃蛇_第1张图片

  • 图片
    bg.png:背景
    cell:png:一节蛇
    food.png:食物

你可能感兴趣的:(java基础篇)