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

📄 mbac.java

📁 一个贪吃蛇游戏
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
								{
									randomFood = random.nextInt(3) + 8;
								}
							}
						}
					}
					
					if (randomLandKind == 0)
					{
						randomLand = 0;
					}
					else
					{
						if (randomLandKind == 1)
						{
							randomLand = random.nextInt(9) + 2;
						}
						else
						{
							if (randomLandKind == 2)
							{
								randomLand = random.nextInt(20) + 11;
							}
							else
							{
								if (randomLandKind == 3)
								{
									randomLand = random.nextInt(20) + 31;
								}
							}
						}
					}
					
					for (int i = 0;i < randomFood;i ++)
					{
						randomX = random.nextInt(15);
						randomY = random.nextInt(15);
						
						while (infos[randomX][randomY] > 0)
						{
							randomX = random.nextInt(15);
							randomY = random.nextInt(15);
						}
						
						infos[randomX][randomY] = random.nextInt(4) + 10;
					}
					
					for (int i = 0;i < randomLand;i ++)
					{
						randomX = random.nextInt(15);
						randomY = random.nextInt(15);
						
						while (infos[randomX][randomY] > 0)
						{
							randomX = random.nextInt(15);
							randomY = random.nextInt(15);
						}
						
						infos[randomX][randomY] = random.nextInt(5) + 14;
					}
					
					direction = random.nextInt(4) + 1;
					randomX = random.nextInt(13) + 1;
					randomY = random.nextInt(13) + 1;
					
					while (infos[randomX][randomY] > 0 || (direction == 1 && infos[randomX][randomY - 1] > 0) || (direction == 2 && infos[randomX][randomY + 1] > 0) || (direction == 3 && infos[randomX - 1][randomY] > 0) ||(direction == 4 && infos[randomX + 1][randomY] > 0))
					{
						randomX = random.nextInt(13) + 1;
						randomY = random.nextInt(13) + 1;
					}
					
					infos[randomX][randomY] = 2 * direction - 1;
					limitFood = randomFood;
					limitLand = randomLand;
					mapEditor();
					setContentPane(container);
				}
			}
		);
		
		button[9] = new JButton("RETURN");
		button[9].setBackground(Color.black);
		button[9].setFont(new Font("Monospaced",Font.BOLD,32));
		button[9].setForeground(Color.white);
		box[4].add(button[9]);
		
		button[9].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					mapEditor();
					setContentPane(container);
				}
			}
		);
		
		box[1].add(box[4]);
		blankPanel[2] = new BlankPanel(400,100);
		box[1].add(blankPanel[2]);
		box[0].add(box[1]);
		container.add(box[0],BorderLayout.NORTH);
	}
	
	//draw the left half of the main menu
	private void mainMenu()
	{
		box[0] = Box.createHorizontalBox();
		box[1] = Box.createVerticalBox();
		picturePanel[0] = new PicturePanel(300,107,"img\\menu1.jpg");
		box[1].add(picturePanel[0]);
		box[2] = Box.createHorizontalBox();
		blankPanel[0] = new BlankPanel(44,34);
		box[2].add(blankPanel[0]);
		pictureTextPanel[0] = new PictureTextPanel(211,34,"img\\menuoff.jpg","New Game");
		chooseMenu(pictureTextPanel[0],1);
		box[2].add(pictureTextPanel[0]);
		blankPanel[1] = new BlankPanel(45,34);
		box[2].add(blankPanel[1]);
		box[1].add(box[2]);
		picturePanel[1] = new PicturePanel(300,53,"img\\menu2.jpg");
		box[1].add(picturePanel[1]);
		box[3] = Box.createHorizontalBox();
		blankPanel[2] = new BlankPanel(71,34);
		box[3].add(blankPanel[2]);
		pictureTextPanel[1] = new PictureTextPanel(211,34,"img\\menuoff.jpg","Map Editor");
		chooseMenu(pictureTextPanel[1],2);
		box[3].add(pictureTextPanel[1]);
		blankPanel[3] = new BlankPanel(18,34);
		box[3].add(blankPanel[3]);
		box[1].add(box[3]);
		picturePanel[2] = new PicturePanel(300,54,"img\\menu3.jpg");
		box[1].add(picturePanel[2]);
		box[4] = Box.createHorizontalBox();
		blankPanel[4] = new BlankPanel(89,34);
		box[4].add(blankPanel[4]);
		pictureTextPanel[2] = new PictureTextPanel(211,34,"img\\menuoff.jpg","Rankings");
		chooseMenu(pictureTextPanel[2],3);
		box[4].add(pictureTextPanel[2]);
		box[1].add(box[4]);
		picturePanel[3] = new PicturePanel(300,55,"img\\menu4.jpg");
		box[1].add(picturePanel[3]);
		box[5] = Box.createHorizontalBox();
		blankPanel[5] = new BlankPanel(71,34);
		box[5].add(blankPanel[5]);
		pictureTextPanel[3] = new PictureTextPanel(211,34,"img\\menuoff.jpg","System");
		chooseMenu(pictureTextPanel[3],4);
		box[5].add(pictureTextPanel[3]);
		blankPanel[6] = new BlankPanel(18,34);
		box[5].add(blankPanel[6]);
		box[1].add(box[5]);
		picturePanel[4] = new PicturePanel(300,55,"img\\menu5.jpg");
		box[1].add(picturePanel[4]);
		box[6] = Box.createHorizontalBox();
		blankPanel[7] = new BlankPanel(35,34);
		box[6].add(blankPanel[7]);
		pictureTextPanel[4] = new PictureTextPanel(211,34,"img\\menuoff.jpg","Exit Game");
		chooseMenu(pictureTextPanel[4],5);
		box[6].add(pictureTextPanel[4]);
		blankPanel[8] = new BlankPanel(54,34);
		box[6].add(blankPanel[8]);
		box[1].add(box[6]);
		picturePanel[5] = new PicturePanel(300,72,"img\\menu6.jpg");
		box[1].add(picturePanel[5]);
		box[0].add(box[1]);
	}
	
	//calculate the movement period of the snake
	private void time()
	{
		timer = new java.util.Timer();
		
		timer.schedule(new TimerTask()
		{
			public void run()
			{
				turn = true;
				mapPanel[x][y].setType(9);
				
				if (direction == 1)
				{
					if (y == 0)
					{
						tail = snakeAdapter.cutTail();
						mapPanel[tail / 100][tail % 100].setType(0);
						timer.cancel();
						reborn();
					}
					else
					{
						y --;
						eatFood();
					}
				}
				else
				{
					if (direction == 2)
					{
						if (y == 14)
						{
							tail = snakeAdapter.cutTail();
							mapPanel[tail / 100][tail % 100].setType(0);
							timer.cancel();
							reborn();
						}
						else
						{
							y ++;
							eatFood();
						}
					}
					else
					{
						if (direction == 3)
						{
							if (x == 0)
							{
								tail = snakeAdapter.cutTail();
								mapPanel[tail / 100][tail % 100].setType(0);
								timer.cancel();
								reborn();
							}
							else
							{
								x --;
								eatFood();
							}
						}
						else
						{
							if (direction == 4)
							{
								if (x == 14)
								{
									tail = snakeAdapter.cutTail();
									mapPanel[tail / 100][tail % 100].setType(0);
									timer.cancel();
									reborn();
								}
								else
								{
									x ++;
									eatFood();
								}
							}
						}
					}
				}
			}
		},period,period);
	}
	
	//act to the menu the player chooses
	private void chooseMenu(final PictureTextPanel ptp,final int n)
	{
		ptp.addMouseListener
		(
			new MouseAdapter()
			{
				public void mouseEntered(MouseEvent event)
				{
					ptp.setPicture("img\\menuon.jpg");
				}
				
				public void mouseExited(MouseEvent event)
				{
					ptp.setPicture("img\\menuoff.jpg");
				}
				
				public void mouseClicked(MouseEvent event)
				{
					timer.cancel();
					
					if (n == 1)
					{
						newGame();
						setContentPane(container);
					}
					else
					{
						if (n == 2)
						{
							infos = new int[15][15];
							
							for (int i = 0;i < 15;i ++)
							{
								for (int j = 0;j < 15;j ++)
								{
									infos[i][j] = 0;
								}
							}
							
							limitHead = 0;
							limitFood = 0;
							limitLand = 0;
							AudioPlayer.player.stop(as);
							music.cancel();
							music = new java.util.Timer();
							
							music.schedule(new TimerTask()
							{
								public void run()
								{
									try
									{
										is = new FileInputStream("bgm\\map.wav");
										as = new AudioStream(is);
										AudioPlayer.player.start(as);
									}
									catch(Exception e)
									{
										e.printStackTrace();
									}
								}
							},500,301500);
							
							mapEditor();
							setContentPane(container);
						}
						else
						{
							if (n == 3)
							{
								rankings();
								setContentPane(container);
							}
							else
							{
								if (n == 4)
								{
									system();
									setContentPane(container);
								}
								else
								{
									if (n == 5)
									{
										System.exit(1);
									}
								}
							}
						}
					}
				}
			}
		);
	}
	
	//act to the item the player chooses
	private void chooseItem(final MapPanel mp,final int n)
	{
		mp.addMouseListener
		(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent event)
				{
					if (mp.getType() < 100)
					{
						if (panel[current].getType() > 49)
						{
							panel[current].setType(panel[current].getType() - 50);
						}
						
						current = n;
						item = mp.getType();
						mp.setType(mp.getType() + 50);
					}
				}
			}
		);
	}
	
	//draw a thing on the map with choosed item
	private void drawMap(final MapPanel mp,final int a,final int b)
	{
		mp.addMouseListener
		(
			new MouseAdapter()
			{
				public void mouseEntered(MouseEvent event)
				{
					if (item >= 0)
					{
						icon = new ImageIcon("img\\" + item + ".jpg");
						cursor = getToolkit().createCustomCursor(icon.getImage(),new Point(16,16),Integer.toString(item));
						setCursor(cursor);
					}
				}
				
				public void mouseExited(MouseEvent event)
				{
					if ((a == 0 || a == 14 || b == 0 || b == 14) && item >= 0)
					{
						icon = new ImageIcon("img\\cursor.png");
						cursor = getToolkit().createCustomCursor(icon.getImage(),new Point(0,0),Integer.toString(item));
						setCursor(cursor);
					}
				}
				
				public void mouseClicked(MouseEvent event)
				{
					textPanel[0].setText("");
					
					if (item == 0)
					{
						if (mp.getType() > 0 && mp.getType() < 9)
						{
							limitHead --;
						}
						else
						{
							if (mp.getType() > 9 && mp.getType() < 14)
							{
								limitFood --;
							}
							else
							{
								if (mp.getType() > 13)
								{
									limitLand --;
								}
							}
						}
						
						mp.setType(0);
					}
					else
					{
						if (mp.getType() == 0)
						{
							if (item > 0 && item < 9)
							{
								if (limitHead == 1)
								{
									textPanel[0].setText("At most 1 head!");
								}
								else
								{
									limitHead ++;
									mp.setType(item);
								}
							}
							else
							{
								if (item > 9 && item < 14)
								{
									if (limitFood == 10)
									{
										textPanel[0].setText("At most 10 food!");
									}
									else
									{
										limitFood ++;
										mp.setType(item);
									}
								}
								else
								{
									if (item > 13)
									{
										if (limitLand == 100)
										{
											textPanel[0].setText("At most 100 land!");
										}
										else
										{
											limitLand ++;
											mp.setType(item);
										}
									}
								}
							}
						}
					}
				}
			}
		);
	}
	
	//calculate the limit of the food
	private void randomFood(final JButton jb,final int n)
	{
		jb.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					for (int i = 0;i < 4;i ++)
					{
						button[i].setBackground(Color.black);
					}
					
					randomFoodKind = n;
					jb.setBackground(Color.red);
				}
			}
		);
	}
	
	//calculate the limit of the land
	private void randomLand(final JButton jb,final int n)
	{
		jb.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					for (int i = 4;i < 8;i ++)
					{
						button[i].setBackground(Color.black);
					}
					
					randomLandKind = n;
					jb.setBackground(Color.red);
				}
			}
		);
	}
	
	//balance the proportions of four kinds of food
	private void slide(final JSlider js,final int n)
	{
		js.addChangeListener
		(
			new ChangeListener()
			{
				public void stateChanged(ChangeEvent event)
				{
					temp = 0;
					
					if (!js.getValueIsAdjusting())
					{
						if (js.getValue() < foods[n])
						{
							for (int i = 0;i < foods[n] - js.getValue();i ++)
							{
								while (temp == n || foods[temp] == 35)
								{
									temp = (temp + 1) % 4;
								}
								
								foods[temp] ++;
								temp = (temp + 1) % 4;
							}
						}
						else
						{
							if (js.getValue() > foods[n])
							{
								for (int i = 0;i < js.getValue() - foods[n];i ++)
								{
									while (temp == n || foods[temp] == 15)
									{
										temp = (temp + 1) % 4;
									}
									
									foods[temp] --;
									temp = (temp + 1) % 4;
								}
							}
						}
						
						foods[n] = js.getValue();
						
						for (int i = 0;i < 4;i ++)
						{
							slider[i].setValue(foods[i]);
						}
					}
				}
			}
		);
	}
	
	//add a component in GridBagLayout
	private void addComponent(Component component,int row,int column,int width,int height)
	{
		constraints.gridx = row;
		constraints.gridy = column;
		constraints.gridwidth = width;
		constraints.gridheight = height;
		layout.setConstraints(component,constraints);
		add(component);
	}
	
	//execute application
	public static void main(String args[])
	{
		MBAC mbac = new MBAC();
		mbac.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}	//end class MBAC

⌨️ 快捷键说明

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