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

📄 贪吃蛇界面.java.bak

📁 一个用java编写的贪吃蛇升级版手机游戏
💻 BAK
📖 第 1 页 / 共 3 页
字号:
	   String timetotal;
	   DecimalFormat twoDigits = new DecimalFormat( "00" );
	   public timeThread(String names )
	   {
		   super(names);
	   }

	   public void run(){
		   while(true){
		   try
		   {Thread.sleep(1000);
					Second++;
					if ( Second%60 == 0)
						{
							Minute++;
						if (Minute%60 == 0 )
							{
							Hour++;
							}						
						}
					timetotal = twoDigits.format(Hour)+":"+
							 twoDigits.format(Minute%60)+":"+
							twoDigits.format(Second%60);

				setTitle("贪吃蛇 V1.1 —— 陈勇"+"             "+"游戏总时间 :"+timetotal+
					"              "+"该局游戏时间 :"+thread3.timethisgame);	
		  }
		   catch (InterruptedException interruptedException){}
	   } 
     }
  }
  //************************************************time线程*************************************************************
     public class time extends Thread
   {
	   int Hour = 0;
	   int Minute = 0 ;
	   int Second = 0 ;
	   String timethisgame = "00:00:00";
	   DecimalFormat twoDigits = new DecimalFormat( "00" );
	   public time(String names )
	   {
		   super(names);
	   }

	   public void run(){
		   while(true){
		   try
		   {Thread.sleep(1000);
		     if (!isEnd)
		     {
				if (!parse)
				{
					Second++;
					if ( Second%60 == 0)
						{
							Minute++;
						if (Minute%60 == 0 )
							{
							Hour++;
							}						
						}
					timethisgame = twoDigits.format(Hour)+":"+
							 twoDigits.format(Minute%60)+":"+
							twoDigits.format(Second%60);
				}
			 }
			 else{
				   Hour = 0;
				   Minute = 0 ;
				   Second = 0 ;
			     }
		  }

		   catch (InterruptedException interruptedException){}
	   } 
     }
  }
  //************************************************snakeThread线程*************************************************
  	private class snakeThread extends Thread
	{
	long sleepTime;
	public snakeThread(String name)
		{
			super(name);
			sleepTime = speedtime;
		}

	public void run()
	{
		while(true)
		{
			try{Thread.sleep(speedtime);}			
				catch(InterruptedException interruptedException){}
				catch(ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException)
				{}
			
	try
		{
		   if (!isEnd)
		   {
	
			if (!parse)
			{

				//去掉蛇尾
				playpanels[rows[snakelength]][columes[snakelength]].setBackground(colors[backBox.getSelectedIndex()]);

				//蛇身颜色
				for (int i = 0;i < snakelength ;i++ )
				{
					 playpanels[rows[i]][columes[i]].setBackground(Color.red);
				}
				//将蛇尾重新归为背景
				snake[rows[snakelength]][columes[snakelength]] = 0;				
				
				//利用递归使蛇前进
				for (int i = snakelength;i > 0 ;i-- )
				{
					rows[i] = rows[i-1];
					columes[i] = columes[i-1];
				}

				switch (direction)
				{
					case UP:
					{
						if (lastdirection == DOWN)
							rows[0] += 1;
						else
						{
							rows[0] -= 1;
							lastdirection = UP;
						}
							break;
						}
					case LEFT:
					{
						if (lastdirection == RIGHT)
							columes[0] += 1;
						else
						{
							columes[0] -= 1;
							lastdirection = LEFT;
						}
							break;
						}
					case DOWN:
					{
						if (lastdirection == UP)
							rows[0] -= 1;
						else
						{
							rows[0] += 1;
							lastdirection = DOWN;
						}
							break;
						}
					case RIGHT:
					{
						if (lastdirection == LEFT)
							columes[0] -= 1;
						else
						{
							columes[0] += 1;
							lastdirection = RIGHT;
						}
							break;
						}
				}



		//穿过边框的运算
		if (throughwall != 0)
		{
		
			if (rows[0] >= 25 )
			{
				throughwall--;
				textField3.setText(Integer.toString(throughwall));
				rows[0] = ROW -rows[0];
			}

			else if (rows[0] < 0 )
			{
				throughwall--;
				textField3.setText(Integer.toString(throughwall));
				rows[0] = (rows[0] + 25)% 25;
			}

			else if (columes[0] >= 50 )
			{
				throughwall--;
				textField3.setText(Integer.toString(throughwall));
				columes[0] = COLUMES -columes[0];
			}

			else if (columes[0] < 0 )
			{
				throughwall--;
				textField3.setText(Integer.toString(throughwall));
				columes[0] = (columes[0] + 50) % 50;
			}

		}

		if (rows[0] >= 25||rows[0] < 0 ||columes[0] >= 50||columes[0] < 0 && throughwall == 0)
		{
			cardManager.show(desk,"游戏结束界面");
			repaint();
			isEnd = true;
		}

		//蛇头遇到蛇身
		if (snake[rows[0]][columes[0]] == 1)
			{
			//有穿身宝物
			if (throughbody != 0)
			{
				throughbody--;
				textField2.setText(Integer.toString(throughbody));

			}

			//无宝物时游戏结束
			else{
					cardManager.show(desk,"游戏结束界面");
					repaint();
					isEnd = true;
					}

			}
		
		//蛇头遇到食物
		if (snake[rows[0]][columes[0]] == 2)
			{				
					
				//吃到食物
				snakelength++;
				score+=100;
				if (score%2000 == 0 && speedtime>25)
				{
					
					JOptionPane.showMessageDialog(panel2,
						"小猪们!升级啦!\n注意了!\nREADY? GO !   !   !");
					if (speedtime > 100)
					{
						speedtime -= 100 ;
						}

					if (speedtime <= 100 )
					{
						speedtime -= 25 ;
						}
					
				}
				textField1.setText(Integer.toString(score));
				int X,Y;
				X = (int)(Math.random()*25);
				Y = (int)(Math.random()*50);
				//当食物在蛇身上或墙上时重新随机设置食物位置
				while ( snake[X][Y] != 0 )
						{						
							X = (int)(Math.random()*25);
							Y = (int)(Math.random()*50);
						}
				snake[X][Y] = 2;
				playpanels[X][Y].setBackground(Color.yellow);
					    
			//宝物出现机率
			int random = (int)(Math.random()*3);
			//设置宝物
			if ( random == 1 )
				{
				int x,y;
				x = (int)(Math.random()*25);
				y = (int)(Math.random()*50);
				//当宝物在蛇身上或墙上时重新随机设置食物位置
				while ( snake[x][y] != 0 )
						{						
							x = (int)(Math.random()*25);
							y = (int)(Math.random()*50);
						}
				snake[x][y] = 3;
				playpanels[x][y].setBackground(Color.cyan);
						}
			if (random == 2)
			{
				int x,y;
				x = (int)(Math.random()*25);
				y = (int)(Math.random()*50);
				//当宝物在蛇身或在墙上时重新随机设置食物位置
				while ( snake[x][y] != 0 )
						{						
							x = (int)(Math.random()*25);
							y = (int)(Math.random()*50);
						}
				snake[x][y] = 4;
				playpanels[x][y].setBackground(Color.red);
			}
		}
		
		
		//吃到穿身宝物
		if (snake[rows[0]][columes[0]] == 3)
		{
			score += 100;
			throughbody++;
			if (score%2000 == 0 && speedtime>25)
				{
				JOptionPane.showMessageDialog(panel2,
						"小猪们!升级啦!\n注意了!\nREADY? GO !   !   !");
					if (speedtime > 100)
					{speedtime -= 100 ;}

					if (speedtime <= 100 )
					{speedtime -= 25 ;}
					
				}
			textField1.setText(Integer.toString(score));
			textField2.setText(Integer.toString(throughbody));

		}
		//吃到穿墙宝物
		if (snake[rows[0]][columes[0]] == 4)
		{
			score += 200;
			throughwall++;
			if (score%2000 == 0 && speedtime>25)
				{
				JOptionPane.showMessageDialog(panel2,
						"小猪们!升级啦!\n注意了!\nREADY? GO !   !   !");
					if (speedtime > 100)
					{speedtime -= 100 ;}

					if (speedtime <= 100 )
					{speedtime -= 25 ;}
					
				}
			textField1.setText(Integer.toString(score));
			textField3.setText(Integer.toString(throughwall));

		}
		//撞到墙壁
		if (snake[rows[0]][columes[0]] == 5)
		{			
			if (throughwall != 0)
			{
				throughwall--;
				textField3.setText(Integer.toString(throughwall));
			}
			else{
					cardManager.show(desk,"游戏结束界面");
					repaint();
					isEnd = true;
					}

			
		}
		//设置蛇头
		else
			{
				snake[rows[0]][columes[0]] = 1 ;
				playpanels[rows[0]][columes[0]].setBackground(Color.green);
				for ( int count = 0; count < map.length; count++ )
					{
						if ( map[ count ].isSelected() ) {
							changeMap( count );}
						 }
					}

				} 
			}	
		}
		catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException){}
																											
			}
		}
	}
						
}

⌨️ 快捷键说明

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