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

📄 mbac.java

📁 一个贪吃蛇游戏
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
							}
						}
						else
						{
							if (mapPanel[x][y].getType() == 13)
							{
								tail = snakeAdapter.cutTail();
								mapPanel[tail / 100][tail % 100].setType(0);
								snakeAdapter.addHead(100 * x + y);
								leg();
								createFood(13);
								
								if (period < max)
								{
									timer.cancel();
									period += 25;
									time();
								}
							}
						}
					}
				}
			}
		}
	}
	
	//create another food after a kind of food is eaten
	private void createFood(int n)
	{
		random = new Random();
		randomX = random.nextInt(15);
		randomY = random.nextInt(15);
		
		while (mapPanel[randomX][randomY].getType() > 0 && 100 * randomX + randomY != snakeAdapter.getPosition())
		{
			randomX = random.nextInt(15);
			randomY = random.nextInt(15);
		}
		
		if (normal)
		{
			for (int i = 0;i < 4;i ++)
			{
				foods[i] --;
			}
			
			foods[n - 10] += 4;
			food = random.nextInt(100);
			
			if (food >= 0 && food < foods[0])
			{
				mapPanel[randomX][randomY].setType(10);
			}
			else
			{
				if (food >= foods[0] && food < foods[0] + foods[1])
				{
					mapPanel[randomX][randomY].setType(11);
				}
				else
				{
					if (food >= foods[0] + foods[1] && food < 100 - foods[3])
					{
						mapPanel[randomX][randomY].setType(12);
					}
					else
					{
						if (food >= 100 - foods[3] && food < 100)
						{
							mapPanel[randomX][randomY].setType(13);
						}
					}
				}
			}
			
			for (int i = 0;i < 4;i ++)
			{
				foods[i] ++;
			}
			
			foods[n - 10] -= 4;
		}
		else
		{
			mapPanel[randomX][randomY].setType(10);
		}
	}
	
	//input the player name after the game is over
	private void gameOver()
	{
		container.removeAll();
		container.setLayout(new BorderLayout(0,0));
		box = new Box[2];
		picturePanel = new PicturePanel[1];
		textPanel = new TextPanel[1];
		box[0] = Box.createVerticalBox();
		box[1] = Box.createHorizontalBox();
		
		if (normal)
		{
			score *= mbacAdapter.getMapDifficulty();
			score /= 100;
		}
		
		current = mbacAdapter.getTempRanking(normal,score);
		string = "";
		
		if (current == -1)
		{
			string += "    ";
		}
		else
		{
			if (current == 0)
			{
				string += " 1st";
			}
			else
			{
				if (current == 1)
				{
					string += " 2nd";
				}
				else
				{
					if (current == 2)
					{
						string += " 3rd";
					}
					else
					{
						if (current == 9)
						{
							string += "10th";
						}
						else
						{
							string += " " + current + "th";
						}
					}
				}
			}
		}
		
		string += "  ";
		
		for (int i = 9;i > new Integer(score).toString().length();i --)
		{
			string += " ";
		}
		
		string += score;
		textPanel[0] = new TextPanel(500,100,36,string);
		box[1].add(textPanel[0]);
		field = new JTextField();
		field.setBackground(Color.black);
		field.setBorder(new EmptyBorder(0,0,0,0));
		field.setDocument(new MyDocument(3));
		field.setFont(new Font("Monospaced",Font.BOLD,22));
		field.setForeground(Color.white);
		field.setText("MBP");
		box[1].add(field);
		box[0].add(box[1]);
		picturePanel[0] = new PicturePanel(800,500,"img\\gameover.jpg");
		box[0].add(picturePanel[0]);
		
		picturePanel[0].addMouseListener
		(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent event)
				{
					if (current >= 0)
					{
						mbacAdapter.update(normal,field.getText(),(normal)?mbacAdapter.getMapName():Integer.toString(stage),score);
					}
					
					timer.cancel();
					AudioPlayer.player.stop(as);
					music.cancel();
					music = new java.util.Timer();
					
					music.schedule(new TimerTask()
					{
						public void run()
						{
							try
							{
								is = new FileInputStream("bgm\\menu.wav");
								as = new AudioStream(is);
								AudioPlayer.player.start(as);
							}
							catch(Exception e)
							{
								e.printStackTrace();
							}
						}
					},500,72500);
					
					menu();
					setContentPane(container);
				}
			}
		);
		
		timer = new java.util.Timer();
		
		timer.schedule(new TimerTask()
		{
			public void run()
			{
				if (current >= 0)
				{
					mbacAdapter.update(normal,field.getText(),(normal)?mbacAdapter.getMapName():Integer.toString(stage),score);
				}
				
				timer.cancel();
				music.cancel();
				music = new java.util.Timer();
				
				music.schedule(new TimerTask()
				{
					public void run()
					{
						try
						{
							is = new FileInputStream("bgm\\menu.wav");
							as = new AudioStream(is);
							AudioPlayer.player.start(as);
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
					}
				},500,72500);
				
				menu();
				setContentPane(container);
			}
		},22500);
		
		container.add(box[0],BorderLayout.NORTH);
	}
	
	//save the map the player is editing
	private void save()
	{
		container.removeAll();
		container.setLayout(new BorderLayout(0,0));
		foods = mbacAdapter.getMapFood();
		box = new Box[8];
		button = new JButton[2];
		slider = new JSlider[4];
		blankPanel = new BlankPanel[2];
		picturePanel = new PicturePanel[3];
		textPanel = new TextPanel[6];
		box[0] = Box.createHorizontalBox();
		picturePanel[0] = new PicturePanel(400,567,"img\\side.jpg");
		box[0].add(picturePanel[0]);
		box[1] = Box.createVerticalBox();
		blankPanel[0] = new BlankPanel(400,100);
		box[1].add(blankPanel[0]);
		box[2] = Box.createHorizontalBox();
		textPanel[0] = new TextPanel(150,50,20,"NAME");
		box[2].add(textPanel[0]);
		field = new JTextField();
		field.setBackground(Color.black);
		field.setBorder(new EmptyBorder(0,0,0,0));
		field.setDocument(new MyDocument(9));
		field.setFont(new Font("Monospaced",Font.BOLD,25));
		field.setForeground(Color.white);
		field.setText("DEFAULT");
		box[2].add(field);
		box[1].add(box[2]);
		box[3] = Box.createHorizontalBox();
		textPanel[1] = new TextPanel(100,50,20,"好人猫");
		box[3].add(textPanel[1]);
		slider[0] = new JSlider(SwingConstants.HORIZONTAL,15,35,25);
		slider[0].setMajorTickSpacing(1);
		slider[0].setPaintTicks(true);
		slider[0].setSnapToTicks(true);
		slider[0].setValue(foods[0]);
		slide(slider[0],0);
		box[3].add(slider[0]);
		box[1].add(box[3]);
		box[4] = Box.createHorizontalBox();
		textPanel[2] = new TextPanel(100,50,20,"恶人猫");
		box[4].add(textPanel[2]);
		slider[1] = new JSlider(SwingConstants.HORIZONTAL,15,35,25);
		slider[1].setMajorTickSpacing(1);
		slider[1].setPaintTicks(true);
		slider[1].setSnapToTicks(true);
		slider[1].setValue(foods[1]);
		slide(slider[1],1);
		box[4].add(slider[1]);
		box[1].add(box[4]);
		box[5] = Box.createHorizontalBox();
		textPanel[3] = new TextPanel(100,50,20,"旋风猫");
		box[5].add(textPanel[3]);
		slider[2] = new JSlider(SwingConstants.HORIZONTAL,15,35,25);
		slider[2].setMajorTickSpacing(1);
		slider[2].setPaintTicks(true);
		slider[2].setSnapToTicks(true);
		slider[2].setValue(foods[2]);
		slide(slider[2],2);
		box[5].add(slider[2]);
		box[1].add(box[5]);
		box[6] = Box.createHorizontalBox();
		textPanel[4] = new TextPanel(100,50,20,"惊愕猫");
		box[6].add(textPanel[4]);
		slider[3] = new JSlider(SwingConstants.HORIZONTAL,15,35,25);
		slider[3].setMajorTickSpacing(1);
		slider[3].setPaintTicks(true);
		slider[3].setSnapToTicks(true);
		slider[3].setValue(foods[3]);
		slide(slider[3],3);
		box[6].add(slider[3]);
		box[1].add(box[6]);
		textPanel[5] = new TextPanel(400,100,32,"");
		box[1].add(textPanel[5]);
		box[7] = Box.createHorizontalBox();
		button[0] = new JButton(" SAVE ");
		button[0].setBackground(Color.black);
		button[0].setFont(new Font("Monospaced",Font.BOLD,32));
		button[0].setForeground(Color.white);
		box[7].add(button[0]);
		
		button[0].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if (mbacAdapter.hasMap(field.getText()))
					{
						textPanel[5].setText("Map existed!");
					}
					else
					{
						mbacAdapter.setMap(field.getText(),infos,foods);
						mbacAdapter.saveMap();
						textPanel[5].setText("Map saved!");
					}
				}
			}
		);
		
		button[1] = new JButton("RETURN");
		button[1].setBackground(Color.black);
		button[1].setFont(new Font("Monospaced",Font.BOLD,32));
		button[1].setForeground(Color.white);
		box[7].add(button[1]);
		
		button[1].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					mapEditor();
					setContentPane(container);
				}
			}
		);
		
		box[1].add(box[7]);
		blankPanel[1] = new BlankPanel(400,100);
		box[1].add(blankPanel[1]);
		box[0].add(box[1]);
		container.add(box[0],BorderLayout.NORTH);
	}
	
	//load a map already existing
	private void load()
	{
		container.removeAll();
		container.setLayout(new BorderLayout(0,0));
		box = new Box[3];
		button = new JButton[2];
		blankPanel = new BlankPanel[2];
		picturePanel = new PicturePanel[1];
		textPanel = new TextPanel[1];
		box[0] = Box.createHorizontalBox();
		picturePanel[0] = new PicturePanel(400,567,"img\\side.jpg");
		box[0].add(picturePanel[0]);
		box[1] = Box.createVerticalBox();
		blankPanel[0] = new BlankPanel(400,100);
		box[1].add(blankPanel[0]);
		combo = new JComboBox(mbacAdapter.getList());
		combo.setBackground(Color.black);
		combo.setFont(new Font("Monospaced",Font.BOLD,16));
		combo.setForeground(Color.white);
		combo.setMaximumRowCount(4);
		box[1].add(combo);
		textPanel[0] = new TextPanel(400,100,32,"");
		box[1].add(textPanel[0]);
		box[2] = Box.createHorizontalBox();
		button[0] = new JButton(" LOAD ");
		button[0].setBackground(Color.black);
		button[0].setFont(new Font("Monospaced",Font.BOLD,32));
		button[0].setForeground(Color.white);
		box[2].add(button[0]);
		
		button[0].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if (combo.getSelectedItem() == null)
					{
						textPanel[0].setText("Map not existed!");
					}
					else
					{
						mbacAdapter.loadMap(false,(String)combo.getSelectedItem());
						limitHead = 0;
						limitFood = 0;
						limitLand = 0;
						infos = mbacAdapter.getMapInfo();
						
						for (int i = 0;i < 15;i ++)
						{
							for (int j = 0;j < 15;j ++)
							{
								if (infos[i][j] > 0 && infos[i][j] < 9)
								{
									limitHead ++;
								}
								else
								{
									if (infos[i][j] > 9 && infos[i][j] < 14)
									{
										limitFood ++;
									}
									else
									{
										if (infos[i][j] > 13)
										{
											limitLand ++;
										}
									}
								}
							}
						}
						
						mapEditor();
						setContentPane(container);
					}
				}
			}
		);
		
		button[1] = new JButton("RETURN");
		button[1].setBackground(Color.black);
		button[1].setFont(new Font("Monospaced",Font.BOLD,32));
		button[1].setForeground(Color.white);
		box[2].add(button[1]);
		
		button[1].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					mapEditor();
					setContentPane(container);
				}
			}
		);
		
		box[1].add(box[2]);
		blankPanel[1] = new BlankPanel(400,100);
		box[1].add(blankPanel[1]);
		box[0].add(box[1]);
		container.add(box[0],BorderLayout.NORTH);
	}
	
	//create a random map
	private void random()
	{
		container.removeAll();
		container.setLayout(new BorderLayout(0,0));
		randomFoodKind = 2;
		randomLandKind = 2;
		box  = new Box[5];
		button = new JButton[10];
		blankPanel = new BlankPanel[3];
		picturePanel = new PicturePanel[1];
		textPanel = new TextPanel[2];
		box[0] = Box.createHorizontalBox();
		picturePanel[0] = new PicturePanel(400,567,"img\\side.jpg");
		box[0].add(picturePanel[0]);
		box[1] = Box.createVerticalBox();
		blankPanel[0] = new BlankPanel(400,100);
		box[1].add(blankPanel[0]);
		box[2] = Box.createHorizontalBox();
		textPanel[0] = new TextPanel(100,100,40,"FOOD");
		box[2].add(textPanel[0]);
		button[0] = new JButton("一");
		button[0].setBackground(Color.black);
		button[0].setFont(new Font("Monospaced",Font.BOLD,32));
		button[0].setForeground(Color.white);
		randomFood(button[0],0);
		box[2].add(button[0]);
		button[1] = new JButton("少");
		button[1].setBackground(Color.black);
		button[1].setFont(new Font("Monospaced",Font.BOLD,32));
		button[1].setForeground(Color.white);
		randomFood(button[1],1);
		box[2].add(button[1]);
		button[2] = new JButton("中");
		button[2].setBackground(Color.red);
		button[2].setFont(new Font("Monospaced",Font.BOLD,32));
		button[2].setForeground(Color.white);
		randomFood(button[2],2);
		box[2].add(button[2]);
		button[3] = new JButton("多");
		button[3].setBackground(Color.black);
		button[3].setFont(new Font("Monospaced",Font.BOLD,32));
		button[3].setForeground(Color.white);
		randomFood(button[3],3);
		box[2].add(button[3]);
		box[1].add(box[2]);
		box[3] = Box.createHorizontalBox();
		textPanel[1] = new TextPanel(100,100,40,"LAND");
		box[3].add(textPanel[1]);
		button[4] = new JButton("无");
		button[4].setBackground(Color.black);
		button[4].setFont(new Font("Monospaced",Font.BOLD,32));
		button[4].setForeground(Color.white);
		randomLand(button[4],0);
		box[3].add(button[4]);
		button[5] = new JButton("少");
		button[5].setBackground(Color.black);
		button[5].setFont(new Font("Monospaced",Font.BOLD,32));
		button[5].setForeground(Color.white);
		randomLand(button[5],1);
		box[3].add(button[5]);
		button[6] = new JButton("中");
		button[6].setBackground(Color.red);
		button[6].setFont(new Font("Monospaced",Font.BOLD,32));
		button[6].setForeground(Color.white);
		randomLand(button[6],2);
		box[3].add(button[6]);
		button[7] = new JButton("多");
		button[7].setBackground(Color.black);
		button[7].setFont(new Font("Monospaced",Font.BOLD,32));
		button[7].setForeground(Color.white);
		randomLand(button[7],3);
		box[3].add(button[7]);
		box[1].add(box[3]);
		blankPanel[1] = new BlankPanel(400,100);
		box[1].add(blankPanel[1]);
		box[4] = Box.createHorizontalBox();
		button[8] = new JButton("CREATE");
		button[8].setBackground(Color.black);
		button[8].setFont(new Font("Monospaced",Font.BOLD,32));
		button[8].setForeground(Color.white);
		box[4].add(button[8]);
		
		button[8].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					limitHead = 1;
					random = new Random();
					
					for (int i = 0;i < 15;i ++)
					{
						for (int j = 0;j < 15;j ++)
						{
							infos[i][j] = 0;
						}
					}
					
					if (randomFoodKind == 0)
					{
						randomFood = 1;
					}
					else
					{
						if (randomFoodKind == 1)
						{
							randomFood = random.nextInt(3) + 2;
						}
						else
						{
							if (randomFoodKind == 2)
							{
								randomFood = random.nextInt(3) + 5;
							}
							else
							{
								if (randomFoodKind == 3)

⌨️ 快捷键说明

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