⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tuanchishi.java

📁 一个用java编写的贪吃蛇升级版手机游戏
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class tuanchishi extends JFrame implements KeyListener
{
	   //************************************************界面组件***********************************************
	   public  JPanel desk;
	   public  JPanel panel2;
	   private CardLayout cardManager;
	   private JPanel panel1,panel11,panel12 ;
	   private JButton btStart,btParse,btExit,btHelp;
	   private JLabel gameover;
	   private JLabel picture1,picture2,picture3;
	   private JLabel label1,label2,label3,label4,label5;
	   private JTextField textField1,textField2,textField3;
	   private JRadioButton RadioButton1,RadioButton2,RadioButton3,RadioButton4,RadioButton5,RadioButton6,RadioButton7;
	   private ButtonGroup radioGroup;
	   public JRadioButtonMenuItem map[];
	   private String mapnames[] = {"畅通无阻","一刀两断","两肋插刀","四面楚歌","双剑合璧","一失足成千古恨","月光宝盒","天女散花","九死一生"};
	   private ButtonGroup mapGroup;
	   private JComboBox backBox;
	   private String speedLevelNumber[] = {"1","2","3","4","5"};
       private String colorNames[] = { "蓝色","黑色",   "深灰色", "灰色",
											 "绿色", "浅灰色", "紫红色",
											 "橙色", "粉红色", "红色" };
	   private String names[] = {"开始界面","游戏界面","暂停界面","游戏结束界面"};
	   private Color colors[] = {  Color.blue, Color.black, 
							Color.darkGray, Color.gray, Color.green,
							Color.lightGray, Color.magenta, Color.orange, Color.pink,
							Color.red};
	   private JPanel[][] playpanels;
	   private GameOverPanel GameOver;
	   private HelpFrame helpArea;
	   private ResetFrame panelset;
	   private ParsePanel Parse;
	   private StartPanel Start;
	   //*********************************************风格********************************************************
	   private String strings[] = { "Metal", "Motif", "Windows" };
	   private UIManager.LookAndFeelInfo looks[];
	   private JRadioButtonMenuItem radio[];

	  
	   //********************************************游戏参数*****************************************************
	   private final int UP = 1,LEFT = 2,DOWN = 3,RIGHT = 4;
	   private int lastdirection = RIGHT;												//记录上次的运动方向
	   private int ROW = 25;															//游戏界面行大小
	   private int COLUMES = 50;														//游戏界面列大小	  
	   private boolean parse = false;													//判断暂停键是否第一次按下
	   private boolean isEnd ;															//判断游戏是否结束
	   private int snake[][] = new int [25][50];										//0位背景,1为蛇身,2为食物,3为穿身,4为穿墙,5为墙壁
	   private int snakelength = 3;														//蛇身长度
	   private int rows[];																//蛇身行位置
	   private int columes[];															//蛇身列位置
	   private int direction = RIGHT;													//初始运动方向,1向上,2向左,3向下,4向右
	   private long speedtime = 200;													//游戏速度
	   private int score = 0;															//分数
	   private int throughbody = 0;														//穿过身体宝物数量
	   private int throughwall = 0;														//穿墙宝物数量
	   
	   snakeThread thread = new snakeThread("贪吃蛇");
	   timeThread thread2 = new timeThread("贪吃蛇");
	   time thread3 = new time("贪吃蛇");
	贪吃蛇界面 ()
	{	
		//************************************************设置容器*************************************************
		Container container = getContentPane();
		container.setLayout( new BorderLayout(5,0));
		desk = new JPanel();
		cardManager = new CardLayout();
		desk.setLayout(cardManager);

		//************************************************HelpFrame***********************************************
		helpArea = new HelpFrame();
		helpArea.setVisible(false);
		//************************************************RestFrame***********************************************
		panelset = new ResetFrame();
		panelset.setVisible(false);
		//***********************************************Start界面******************************************************
		Start = new StartPanel();
		desk.add(Start,"开始界面");
		//*************************************************游戏界面************************************************
		panel2=new JPanel();
		panel2.addKeyListener(this);
		playpanels=new JPanel[ROW][COLUMES];
		panel2.setLayout(new GridLayout(ROW,COLUMES,1,1));
		for(int i=0;i<ROW;i++){
			for(int j=0;j<COLUMES;j++){
				playpanels[i][j]=new JPanel();
				playpanels[i][j].setBackground(Color.blue);
				panel2.add(playpanels[i][j]);
			}
			addKeyListener(this);
		}
		desk.add(panel2,"游戏界面");
		//************************************************Parse界面************************************************
		Parse = new ParsePanel();
		desk.add(Parse,"暂停界面");
		//************************************************GameOver************************************************
		GameOver = new GameOverPanel();
		desk.add(GameOver,"游戏结束界面");

					
		//*******************************************字体*********************************************************
		Font font1=new Font("",Font.ITALIC+Font.BOLD,15);
		Font font2=new Font("",Font.ITALIC+Font.BOLD,20);
		Font font3=new Font("",Font.ITALIC+Font.BOLD,25);
		Font font4=new Font("",Font.ITALIC+Font.BOLD,15);
		//******************************************菜单栏********************************************************
		JMenuBar menubar=new JMenuBar();
		setJMenuBar(menubar);
		//******************************************主菜单********************************************************
		JMenu fileMenu=new JMenu("选项");
		fileMenu.setFont(font2);fileMenu.setForeground(Color.blue);
		//****************************************风格选项********************************************************
	    JMenu menu = new JMenu("风格");
	    radio = new JRadioButtonMenuItem[ strings.length ];
        ButtonGroup group = new ButtonGroup();
	    ItemHandler handler = new ItemHandler();

	   for ( int count = 0; count < radio.length; count++ ) {
         radio[ count ] = new JRadioButtonMenuItem( strings[ count ] );
         radio[ count ].addItemListener( handler );
         group.add( radio[ count ] );
         menu.add( radio[ count ] );
      }
	  looks = UIManager.getInstalledLookAndFeels();
	  radio[ 2 ].setSelected( true );
	  fileMenu.add(menu);
	  fileMenu.addSeparator();	
		//****************************************按键设置菜单*****************************************************
		fileMenu.add(new AbstractAction("按键设置"){
					public void actionPerformed(ActionEvent event){
			panelset.setVisible(true);				
					}	
				}			
				);	//结束监听菜单
		fileMenu.addSeparator();	

		//*************************************** 网格设置 ***************************************
		JMenu netmenu = new JMenu("网格设置");
		netmenu.add(new AbstractAction("显示网格"){
					public void actionPerformed(ActionEvent event){
		try{panel2.setBackground(colors[backBox.getSelectedIndex()+1]);	}
		catch(ArrayIndexOutOfBoundsException e)
			{panel2.setBackground(Color.blue);}
					}	
				}			
				);	//结束监听菜单
		netmenu.add(new AbstractAction("隐藏网格"){
			public void actionPerformed(ActionEvent event){
				panel2.setBackground(colors[backBox.getSelectedIndex()]);					
					}	
				}			
				);	//结束监听菜单
	  fileMenu.add(netmenu);

      fileMenu.addSeparator();
	  //*********************************** 地图选择  ***********************************************
	  JMenu mapmenu = new JMenu("地图选择");
	  map = new JRadioButtonMenuItem[mapnames.length];
	  ItemMapHandler maphandler = new ItemMapHandler();
	  mapGroup = new ButtonGroup();

	  for ( int count = 0 ; count < map.length ; count ++ )
	  {
		  map[ count ] = new JRadioButtonMenuItem( mapnames[ count ] );
		  map[ count ].addItemListener(maphandler);
		  mapGroup.add(map[ count ]);
		  mapmenu.add(map[ count ]);
	  }
	  
	  // *************************************************************************************************
	  fileMenu.add(mapmenu);
	  menubar.add(fileMenu);


		

		
		//**********************             panel1——按钮之类部分        **************************
		panel1 = new JPanel();
		panel1.setLayout(new BorderLayout());
		panel11 = new JPanel();
		panel11.setLayout(new GridLayout(2,8));
		panel12 = new JPanel();
		panel12.setLayout(new FlowLayout());
		//#####################              等级的多选框               ###########################
		label1 = new JLabel("等级",SwingConstants.CENTER); 
		label1.setFont(font1);
		panel11.add(label1);
		RadioButton1 = new JRadioButton("菜鸟",false);
		RadioButton1.setBackground(Color.cyan);
		RadioButton1.setFont(font1);
		RadioButton1.addKeyListener(this);
		RadioButton2 = new JRadioButton("入门",true);
		RadioButton2.setBackground(Color.cyan);
		RadioButton2.setFont(font1);
		RadioButton2.addKeyListener(this);
		RadioButton3 = new JRadioButton("高手",false);
		RadioButton3.setBackground(Color.cyan);
		RadioButton3.setFont(font1);
		RadioButton3.addKeyListener(this);
		RadioButton4 = new JRadioButton("专家",false);
		RadioButton4.setBackground(Color.cyan);
		RadioButton4.setFont(font1);
		RadioButton4.addKeyListener(this);
		RadioButton5 = new JRadioButton("精英",false);
		RadioButton5.setBackground(Color.cyan);
		RadioButton5.setFont(font1);
		RadioButton5.addKeyListener(this);
		RadioButton6 = new JRadioButton("大师",false);
		RadioButton6.setBackground(Color.cyan);
		RadioButton6.setFont(font1);
		RadioButton6.addKeyListener(this);
		RadioButton7 = new JRadioButton("超人",false);
		RadioButton7.setBackground(Color.cyan);
		RadioButton7.setFont(font1);
		RadioButton7.addKeyListener(this);
		radioGroup = new ButtonGroup();
		radioGroup.add(RadioButton1);
		radioGroup.add(RadioButton2);
		radioGroup.add(RadioButton3);
		radioGroup.add(RadioButton4);
		radioGroup.add(RadioButton5);
		radioGroup.add(RadioButton6);
		radioGroup.add(RadioButton7);
		RadioButton1.addActionListener(new listener(400));
		RadioButton2.addActionListener(new listener(300));
		RadioButton3.addActionListener(new listener(200));
		RadioButton4.addActionListener(new listener(100));
		RadioButton5.addActionListener(new listener(75));
		RadioButton6.addActionListener(new listener(50));
		RadioButton7.addActionListener(new listener(25));

		panel11.add(RadioButton1);
		panel11.add(RadioButton2);
		panel11.add(RadioButton3);
		panel11.add(RadioButton4);
		panel11.add(RadioButton5);
		panel11.add(RadioButton6);
		panel11.add(RadioButton7);
//#####################              显示得分区               ####################################
		label2 = new JLabel("得分",SwingConstants.CENTER); 
		label2.setFont(font1);
		panel11.add(label2);
		textField1 = new JTextField("0",4);
		textField1.setHorizontalAlignment(textField1.CENTER);
		textField1.setFont(font1);
		textField1.setBackground(Color.white);
		textField1.setEditable(false);
		panel11.add(textField1);
//#####################               显示穿身宝物区           ####################################
		label3 = new JLabel("穿身",SwingConstants.CENTER); 
		label3.setFont(font1);
		panel11.add(label3);
		textField2 = new JTextField("0",4);
		textField2.setHorizontalAlignment(textField2.CENTER);
		textField2.setFont(font1);
		textField2.setBackground(Color.white);
		textField2.setEditable(false);
		panel11.add(textField2);
//#######################              显示穿墙宝物区           ##############################################
		label4 = new JLabel("穿墙",SwingConstants.CENTER); 
		label4.setFont(font1);
		panel11.add(label4);
		textField3 = new JTextField("0",4);
		textField3.setHorizontalAlignment(textField2.CENTER);
		textField3.setFont(font1);
		textField3.setBackground(Color.white);
		textField3.setEditable(false);
		panel11.add(textField3);

//######################               下拉框换背景色              ###############################
		label5 = new JLabel("背景",SwingConstants.CENTER); 
		label5.setFont(font1);
		panel11.add(label5);
		backBox = new JComboBox( colorNames); 
		backBox.setFont(font2) ;
		backBox.setBackground(Color.white);
		backBox.setMaximumRowCount( 5 );
		//开始监听
        backBox.addItemListener(
         new ItemListener() {
            public void itemStateChanged( ItemEvent event )
            {
			   if ( event.getStateChange() == ItemEvent.SELECTED )
                  panel2.setBackground(colors[backBox.getSelectedIndex()]);
			      for (int i = 0 ;i <ROW ;i++ )
			      {
					  for (int j = 0;j<COLUMES ;j++ )
					  {playpanels[i][j].setBackground(colors[backBox.getSelectedIndex()]);
					  }
			      }
            }

         }  

      ); // 结束监听
		panel11.add(backBox);

		//button组件
//#############################3          开始按钮             #################################
		Icon btStartPicture = new ImageIcon( "开始.gif" );
		btStart = new JButton("开始",btStartPicture);
		btStart.setToolTipText("开始");
		btStart.setFont(font3) ;
		btStart.setHorizontalTextPosition( SwingConstants.CENTER );
        btStart.setVerticalTextPosition( SwingConstants.BOTTOM );
		btStart.setBackground(Color.green);
		btStart.setForeground(Color.red);
		//监听开始按钮
		btStart.addKeyListener(this);
		btStart.addActionListener(new 
			ActionListener(){
				public void actionPerformed(ActionEvent e)
				{	
				newGame(speedtime);	
				try
		           {thread3.start();}			
		           catch (IllegalThreadStateException illegalThreadStateException){}		
				}				
			  }			
			);			

//###########################3         暂停按钮          ##########################################3
		Icon btParsePicture = new ImageIcon( "暂停.gif" );	
		btParse = new JButton("暂停",btParsePicture);
		btParse.setToolTipText("暂停");
		btParse.setFont(font3) ;
		btParse.setHorizontalTextPosition( SwingConstants.CENTER );
        btParse.setVerticalTextPosition( SwingConstants.BOTTOM );
		btParse.setBackground(Color.green);
		btParse.setForeground(Color.red);
		//监听暂停按钮
		btParse.addKeyListener(this);
		btParse.addActionListener(new 
			ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					if (parse == true )
					{btParse.setText("暂停");cardManager.show(desk,"游戏界面");}
					if (parse == false)
					{btParse.setText("继续");cardManager.show(desk,"暂停界面");}
					parse = !parse;

				}
				
			}			
			);
		//结束监听
		
//##########################3          退出按钮	     ##########################################3
	    Icon btExitPicture = new ImageIcon( "讨厌.gif" );	
		btExit = new JButton("退出",btExitPicture);
		btExit.setToolTipText("退出");
		btExit.setFont(font3) ;
		btExit.setHorizontalTextPosition( SwingConstants.CENTER );
        btExit.setVerticalTextPosition( SwingConstants.BOTTOM );
		btExit.setBackground(Color.green);
		btExit.setForeground(Color.red);

		
		//开始监听退出按钮
		btStart.addKeyListener(this);
		btExit.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
						System.exit(0);
				}
		}
		);//结束监听退出按钮

//################################         帮助按钮    ########################################3
		Icon btHelpPicture = new ImageIcon( "关于.gif" );	
		btHelp = new JButton("帮助",btHelpPicture);
		btHelp.setToolTipText("帮助");
		btHelp.setHorizontalTextPosition( SwingConstants.CENTER );
        btHelp.setVerticalTextPosition( SwingConstants.BOTTOM );
		btHelp.setFont(font3) ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -