package gameTanChiShe;
import com.sun.scenario.effect.ImageData;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
public class Main extends JFrame implements Runnable{
private JPanel jpCenter=new JPanel();
private JFrame jf= new JFrame();
private SnackHead snackHead=new SnackHead();
private SnackNode snackNodeChi=new SnackNode();
private List<SnackNode> snackNodeList=new ArrayList<>();
static String ydFangXiang="n";//s,x,z,y-上下左右
private Thread t;
public void Main(){
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
jf=new JFrame();
jf.setLocation((d.width - d.width / 3) / 2, (d.height - d.height / 3) / 2);
jf.setSize(600, 400);
//设置窗口标题
jf.setTitle("测试贪吃蛇");
;
// jf.setIconImage(tk.getImage("/gameTanChiShe/images/nanchong.png"));//窗口图标哦
// URL url=ImageData.class.getResource("/gameTanChiShe/images/nanchong.png");
// Image img=ImageData.class.getResource("/gameTanChiShe/images/nanchong.png");
jf.setIconImage(tk.getImage("/gameTanChiShe/images/nanchong.png"));//窗口图标哦
jf.setResizable(false);//是否可调窗口尺寸
jf.setLayout(new BorderLayout());
JButton bStart = new JButton("开始"), bEnd = new JButton("退出"), bS = new JButton("上")
, bX = new JButton("下"), bZ = new JButton("左"), bY = new JButton("右");
bStart.addActionListener(new StartHandeler());
bEnd.addActionListener(new ExitHandeler());
//设置按钮上下左右事件
bS.addActionListener(new sL());
bX.addActionListener(new xL());
bZ.addActionListener(new zL());
bY.addActionListener(new yL());
bStart.addKeyListener(new SxzyKeyListener());
bEnd.addKeyListener(new SxzyKeyListener());
bS.addKeyListener(new SxzyKeyListener());
// bX.addKeyListener(new SxzyKeyListener());
// bZ.addKeyListener(new SxzyKeyListener());
// bY.addKeyListener(new SxzyKeyListener());
JTextArea jta = new JTextArea("这是贪吃蛇游戏");
jta.setEnabled(false);
JPanel jpSouth = new JPanel();
jpSouth.add(bStart);
jpSouth.add(bEnd);
JPanel jpEast=new JPanel();
jpEast.setLayout(new GridLayout(5,1));
jpEast.setSize(20,400);
jpEast.add(bS);
jpEast.add(bX);
jpEast.add(bZ);
jpEast.add(bY);
jpEast.add(jta);
jpEast.setBackground(Color.MAGENTA);
GridLayout glCenter = new GridLayout(20, 20);
jpCenter.setLayout(glCenter);
JLabel jl[][] = new JLabel[20][20];
Border blackline = BorderFactory.createLineBorder(Color.red);
Random ran = new Random();
int xshe = ran.nextInt(19);//蛇头节点
int yshe = ran.nextInt(19);
int xsheChi = ran.nextInt(19);//被吃的节点
int ysheChi = ran.nextInt(19);
while(xsheChi==xshe){
xsheChi=ran.nextInt(19);
}
while(ysheChi==yshe){
ysheChi=ran.nextInt(19);
}
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
jl[i][j] = new JLabel();
jl[i][j].setOpaque(true);//设置不透明,默认是透明的
jl[i][j].setBorder(blackline);
if(i==xshe&&j==yshe){
jl[xshe][yshe].setBackground(Color.RED);
snackHead.setX(i);
snackHead.setY(j);
}
if(i==xsheChi&&j==ysheChi){
jl[xsheChi][ysheChi].setBackground(Color.YELLOW);
snackNodeChi.setX(i);
snackNodeChi.setY(j);
}
jpCenter.add(jl[i][j]);
}
}
jf.add(jpCenter, BorderLayout.CENTER);
jf.add(jpSouth, BorderLayout.SOUTH);
jf.add(jpEast,BorderLayout.EAST);
// jf.addKeyListener(new KeyPress());
jf.setVisible(true);//可见
}
@Override
public void run() {
try {
while (true){
if(!ydFangXiang.equals("n")){
shauxinShe(ydFangXiang);
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//按钮退出事件
class ExitHandeler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"按确认按钮后关闭窗口");
System.exit(0);
}
}
public void shauxinShe(String sxzy){
ydFangXiang=sxzy;
Random ran = new Random();
int flag=0;//没吃
if(sxzy=="s"){//上
snackHead.setX(snackHead.getX()-1);
if(snackHead.getX()==snackNodeChi.getX()&&snackHead.getY()==snackNodeChi.getY()){
flag=1;//吃掉节点
snackNodeList.add(new SnackNode(snackHead.getX()+1,snackHead.getY()));//新增最后一个节点
}else{
if(snackNodeList.size()>=1){
snackNodeList.remove(0);//移除第一个节点
snackNodeList.add(new SnackNode(snackHead.getX()+1,snackHead.getY()));//新增最后一个节点
}
}
}
if(sxzy=="x"){//下
snackHead.setX(snackHead.getX()+1);
if(snackHead.getX()==snackNodeChi.getX()&&snackHead.getY()==snackNodeChi.getY()){
flag=1;//吃掉节点
snackNodeList.add(new SnackNode(snackHead.getX()-1,snackHead.getY()));//新增最后一个节点
}else{
if(snackNodeList.size()>=1){
snackNodeList.remove(0);//移除第一个节点
snackNodeList.add(new SnackNode(snackHead.getX()-1,snackHead.getY()));//新增最后一个节点
}
}
}
if(sxzy=="z"){//左
snackHead.setY(snackHead.getY()-1);
if(snackHead.getX()==snackNodeChi.getX()&&snackHead.getY()==snackNodeChi.getY()){
flag=1;//吃掉节点
snackNodeList.add(new SnackNode(snackHead.getX(),snackHead.getY()+1));//新增最后一个节点
}else{
if(snackNodeList.size()>=1){
snackNodeList.remove(0);//移除第一个节点
snackNodeList.add(new SnackNode(snackHead.getX(),snackHead.getY()+1));//新增最后一个节点
}
}
}
if(sxzy=="y"){//右
snackHead.setY(snackHead.getY()+1);
if(snackHead.getX()==snackNodeChi.getX()&&snackHead.getY()==snackNodeChi.getY()){
flag=1;//吃掉节点
snackNodeList.add(new SnackNode(snackHead.getX(),snackHead.getY()-1));//新增最后一个节点
}else{
if(snackNodeList.size()>=1){
snackNodeList.remove(0);//移除第一个节点
snackNodeList.add(new SnackNode(snackHead.getX(),snackHead.getY()-1));//新增最后一个节点
}
}
}
if(sxzy=="n"){//new新开始
snackNodeList=new ArrayList<>();
snackHead.setX(ran.nextInt(19));
snackHead.setY(ran.nextInt(19));
}
if(snackHead.getX()>=20||
评论0