package snakeGame;
/*
* SnakeGame类来设计贪吃蛇小游戏的运行界面,运行界面是贪吃蛇游戏的主体部分, 界面主要包括两个方面的内容,
* 一方面是运行界面的内容,贪吃蛇长度显示,游戏说明,速度控制,游戏开始,暂停退出等按钮。
* 另一方面,主要包括贪吃蛇的形状和移动,贪吃蛇移动区域,随机点的定义
* 运行界面的过程是这样的:在开始姐爱你点击进入游戏按钮以后,程序运行到运行界面,开始播放背景音乐。
* 点击游戏说明按钮,弹出一个对话框,说明游戏运行的操作过程。点击开始按钮以后,
* 贪吃蛇开始向上移动,鼠标在向上区域点击,贪吃蛇向上,向左区域点击,贪吃蛇向左,依次赖推。
* 当贪吃蛇碰到草莓时,吃掉它,蛇身变长,并有背景音乐显示,长度显示加一,
* 点击暂停按钮游戏暂停,点击退出按钮后,退出游戏。
* 当贪吃蛇撞到自己或者墙体的时候,贪吃蛇会死亡,然后弹出一个界面,重启界面,用来决定游戏继续进行或者退出游戏。
* 贪吃蛇的形状和移动通过数组的形式实现,在界面中,定义一个x轴和y轴定义的坐标系,定义一个数组,数组的移动就是贪吃蛇的移动,
* 移动方式是贪吃蛇坐标的改变,可以通过鼠标控制或键盘控制来实现贪吃蛇的移动,
* 随机点产生是在坐标系中产生随机数来实现,
*/
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Time;
import java.util.Date;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.TimerTask;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JApplet;
public class SnakeGame extends JPanel implements ActionListener {
private final int length = 15;//定义活动范围
private final int width = 25;//定义活动范围
private final int unit = 45;//定义单位长度
private final int GameLOCX=40;
private final int GameLOCY=40;
private final int GameWidth=width*unit;
private final int GameLength=length*unit;
//随机点坐标
int newY1 =0 ;
int newX1 = 0 ;
int mousex=1;
int mousey=1;
//播放背景音乐
AudioClip christmas = loadSound ("F:\\MYJAVA\\Myprogram\\Snakeexample\\src\\Music\\backgroundMusic.wav");
int direction = 1;//定义一个按下按钮后要去的方向
private ArrayList<SnakeNode> snake = new ArrayList<>();//定义蛇身的数组集合
private int Direction;//定义蛇头的方向
private int Length ;//定义蛇身的长度
private SnakeNode newNode = new SnakeNode(1,1,Color.BLACK);//定义随机点
boolean startFlag =false;
//定义按钮,速度控制,开始暂停退出按钮等
RButton SspeedButton , TspeedButton,FspeedButton,THspeedButton ,ShowButton;
RButton startButton , stopButton , quitButton ,reStartButton,closeButton;
//定义标签,长度显示,方向显示,按钮提示等
JLabel snakeScore, label1, label3,label4;
//初始速度控制
private static int Difficult_Degree=1;
//蛇的移动控制,利用线程来实现用鼠标控制,利用计时器来实现用键盘控制。
Thread tr= new Thread(new ThingsListener());
Timer time = new Timer(1000, new ThingsListener1());//定义一个定时器对象,这里我们还要创建一个ThingsListener事件
public SnakeGame() {//初始化区域
//循环播放背景音乐
christmas.loop ();
// time.start();
tr.start();
//定义按键
//在容器中添加按钮标签等的时候,需要说明布局管理为空,不然的话,加进去的按钮会按照一定的布局来实现,
this.setLayout (null);
//定义按钮
startButton = new RButton("开始游戏");
stopButton =new RButton("暂停游戏");
quitButton =new RButton("退出游戏");
FspeedButton =new RButton("速度一");
SspeedButton =new RButton("速度二");
TspeedButton=new RButton("速度三");
THspeedButton=new RButton("速度四");
ShowButton =new RButton("游戏指南");
//定义标签
snakeScore =new JLabel("3");
label1 =new JLabel("当前长度");
label3 =new JLabel("速度设置");
label4 =new JLabel( );
//设置字体
startButton.setFont(new Font("华文行楷", Font.BOLD, 35));
stopButton.setFont(new Font("华文行楷", Font.BOLD, 35));
quitButton.setFont(new Font("华文行楷", Font.BOLD, 35));
FspeedButton.setFont(new Font("华文行楷", Font.BOLD, 15));
TspeedButton.setFont(new Font("华文行楷", Font.BOLD, 15));
SspeedButton.setFont(new Font("华文行楷", Font.BOLD, 15));
THspeedButton.setFont(new Font("华文行楷", Font.BOLD, 15));
ShowButton.setFont(new Font("华文行楷", Font.BOLD, 30));
label1.setFont(new Font("华文行楷", Font.BOLD, 35));
snakeScore.setFont(new Font("华文行楷", Font.BOLD, 50));
label3.setFont(new Font("华文行楷", Font.BOLD, 30));
label4.setFont(new Font("华文行楷", Font.BOLD, 35));
//定义按钮标签位置
startButton.setBounds (1390, 500 , 190, 90);
stopButton.setBounds (1390, 600 , 190, 90);
quitButton.setBounds (1390, 700 , 190, 90);
snakeScore.setBounds(1450, 70, 150, 90);
label1.setBounds(1390, 10, 190, 90);
ShowButton.setBounds(1390, 170, 190, 90);
label3.setBounds(1390, 270, 190, 90);
label4.setBounds(0, 0, 190, 90);
FspeedButton.setBounds (1390, 350 , 85, 60);
SspeedButton.setBounds (1500,350 , 85, 60);
TspeedButton.setBounds (1390, 420 , 85, 60);
THspeedButton.setBounds (1500, 420 , 85, 60);
THspeedButton.setBackground(Color.green);
SspeedButton.setBackground(Color.blue);
TspeedButton.setBackground(Color.red);
FspeedButton.setBackground(Color.red);
// 添加 按钮和标签,用this关键字指向当前容器
this.add(startButton);
this.add(stopButton);
this.add(quitButton);
this.add(FspeedButton);
this.add(SspeedButton);
this.add(TspeedButton);
this.add(THspeedButton);
this.add(label1);
this.add(snakeScore);
this.add( ShowButton);
this.add(label3);
this.add(label4);
// 添加三个按键的监听事件
startButton.addActionListener(this);
stopButton.addActionListener(this);
quitButton.addActionListener(this);
THspeedButton.addActionListener(this);
SspeedButton.addActionListener(this);
TspeedButton.addActionListener(this);
FspeedButton.addActionListener(this);
ShowButton.addActionListener(this);
snake.add(new SnakeNode(width/2,length/2 ,Color.red));
snake.add(new SnakeNode(width/2,length/2+1 ,Color.blue));
snake.add(new SnakeNode(width/2,length/2+2 ,Color.green));
Direction = 1;//定义初始方向为向上
Length = 3;//蛇身长度为3
CreateNode1();//产生随机点
// CreateNode2();
/*//采用键盘控制的控制模式,利用键盘的上下左右键,来实现让·direction的变化,从而使贪吃蛇能够按照键盘的控制来实现移动
this.addKeyListener(new KeyAdapter() {//捕捉键盘的按键事件 设置监听器
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
c