简易贪吃蛇

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Snake extends JFrame implements ActionListener {
	JPanel jp;
	JButton snake, jb, jb2, jb1, food;
	List list, list1;
	Thread t;
	String str;
	int node, sudu;
	JMenuBar start;
	JMenu jm, jm1;
	JMenuItem jmi, ks, jmi1, jmi2, jmi3, jmi4, jmi5, jmi6;
	ImageIcon imgleft, imgright, imgup, imgdown;

	public Snake() {
		final Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		this.setBounds(0, 0, d.width, d.height);
		System.out.print(d.height + "  " + d.width);
		this.setLayout(null);

		jp = new JPanel();
		jp.setLayout(null);
		jp.setBounds(0, 0, d.width, d.height);
		jp.setBackground(Color.green);
		this.add(jp);

		food = new JButton(); // 设置第一个食物
		food.setBounds(500, 400, 50, 50);
		jp.add(food);

		start = new JMenuBar(); // 设置重新开始按钮
		this.setJMenuBar(start);
		jm = new JMenu("aa");
		start.add(jm);

		jm1 = new JMenu("关卡");
		start.add(jm1);

		jmi1 = new JMenuItem("关卡1");
		jm1.add(jmi1);
		jmi1.addActionListener(this);
		jmi2 = new JMenuItem("关卡2");
		jm1.add(jmi2);
		jmi2.addActionListener(this);
		jmi3 = new JMenuItem("关卡3");
		jm1.add(jmi3);
		jmi3.addActionListener(this);
		jmi4 = new JMenuItem("关卡4");
		jm1.add(jmi4);
		jmi4.addActionListener(this);
		jmi5 = new JMenuItem("关卡5");
		jm1.add(jmi5);
		jmi5.addActionListener(this);
		jmi6 = new JMenuItem("关卡6");
		jm1.add(jmi6);
		jmi6.addActionListener(this);

		jmi = new JMenuItem("重新开始");
		jm.add(jmi);
		jmi.addActionListener(this);

		ks = new JMenuItem("开始");
		jm.add(ks);
		ks.addActionListener(this);

		imgleft = new ImageIcon("E:\\素材\\stleft.png");                   //头部图片
		imgright = new ImageIcon("E:\\素材\\stright.png");
		imgup = new ImageIcon("E:\\素材\\stup.png");
		imgdown = new ImageIcon("E:\\素材\\stdown.png");

		sudu = 500; // 初始化速度
		str="right"; //初始化方向

		list = new ArrayList(); // 创建蛇 节数 的集合

		for (int i = 0; i < 5; i++) { // 设置开始时 蛇的长度

			snake = new JButton();

			snake.setBounds(300 - i * 50, 200, 50, 50);
			jp.add(snake);
			list.add(snake);

		}

		t = new Thread(new Runnable() { // 游戏线程

					public void run() {

						int j = 0;
						int g = 0;
						while (true) {

							j++; 

							List list1 = new ArrayList(); // 获取蛇头的坐标,位移,再将坐标存入一集合
							jb1 = (JButton) list.get(0);
							
							jb1.setIcon(imgright);
							
							final int a = jb1.getX();
							final int b = jb1.getY();
							list1.add(a);
							list1.add(b);
							jb1.setLocation(a + 50, b);
							jb1.requestFocus();

							if (a > d.width-100 || b > d.height-150 || a < 0 || b < 0) { // 判断蛇
																					// 是否撞墙
								JOptionPane.showMessageDialog(null,
										"对不起,GAME OVER");
								t.stop();
							}

							int x = food.getX(); // 在地图内随机产生一个食物
							int y = food.getY();
							if (a == x && b == y) {

								list.add(food);
								food = new JButton();
								Random r = new Random();

								int q = r.nextInt(20);
								int w = r.nextInt(15);
								System.out.print(q + "  " + w);
								food.setBounds(q * 50, w * 50, 50, 50);
								jp.add(food);
								jp.repaint();

								g++; // 判断吃多少食物可以通关
								if (g == 10) {
									JOptionPane.showMessageDialog(null,
											"恭喜你,通关");
									t.stop();
								}

							}
                                  
							
							jb1.addKeyListener(new KeyListener() {

								@Override
								public void keyTyped(KeyEvent e) {
									//

								}

								@Override
								public void keyReleased(KeyEvent e) {

								}

								@Override
								public void keyPressed(KeyEvent e) { // 设定按键控制蛇的方向
									
									   
									if (e.getKeyCode() == KeyEvent.VK_DOWN && str!="up") {
										str = "down";

									}
									if (e.getKeyCode() == KeyEvent.VK_RIGHT &&  str!="left") {

										str = "right";

									}
									if (e.getKeyCode() == KeyEvent.VK_LEFT  &&  str!="right") {

										str = "left";

									}
									if (e.getKeyCode() == KeyEvent.VK_UP && str!="down") {

										str = "up";

									}
								}
							});

							if (str == "down") {
								jb1.setLocation(a, b + 50);
								jb1.setIcon(imgdown);
							}
							if (str == "up") {
								jb1.setLocation(a, b - 50);
								jb1.setIcon(imgup);
							}
							if (str == "right") {
								jb1.setLocation(a + 50, b);
								jb1.setIcon(imgright);
							}
							if (str == "left") {
								jb1.setLocation(a - 50, b);
								jb1.setIcon(imgleft);
							}

							for (int i = 1; i < list.size(); i++) { // 设置蛇
																	// 后面节数的坐标

								jb = (JButton) list.get(i);
								int m = jb.getX();
								int n = jb.getY();
								jb.setLocation(list1.get((i - 1) * 2),
										list1.get(2 * i - 1));
								list1.add(m);
								list1.add(n);
								if (jb.getX() == jb1.getX()
										&& jb.getY() == jb1.getY()) { // 判断蛇不能挨到自己的身体
									JOptionPane.showMessageDialog(null,
											"对不起,GAME OVER");
									t.stop();
								}
							}

							try {
								Thread.sleep(sudu); // 移动速度
							} catch (InterruptedException e) {

								e.printStackTrace();
							}
						}
					}

				});

		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public static void main(String[] agrs) {

		Snake s = new Snake();

	}

	public void actionPerformed(ActionEvent e) { // 重新开始游戏
		if (e.getSource() == jmi) {

			this.dispose();
			new Snake();
		} else if (e.getSource() == ks) {
			t.start();
		} else if (e.getSource() == jmi1) {
			sudu = 500;
		} else if (e.getSource() == jmi2) {
			sudu = 300;
		} else if (e.getSource() == jmi3) {
			sudu = 200;
		} else if (e.getSource() == jmi4) {
			sudu = 150;
		} else if (e.getSource() == jmi5) {
			sudu = 100;
		} else if (e.getSource() == jmi6) {
			sudu = 40;
		}

	}

}

你可能感兴趣的:(简易贪吃蛇)