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

📄 mbac.java

📁 一个贪吃蛇游戏
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/**
 * @(#)MBAC.java
 * @The main class.
 *
 * @Link Scholes
 * @version 1.00 2008/7/21
 */

package GUI;

//Java core packages
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

//Java extension packages
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import sun.audio.*;
import Adapter.*;

public class MBAC extends JFrame
{
	private boolean leg;
	private boolean normal;
	private boolean turn;
	private int current;
	private int direction;
	private int food;
	private int item;
	private int limitFood;
	private int limitHead;
	private int limitLand;
	private int max;
	private int min;
	private int period;
	private int randomFood;
	private int randomFoodKind;
	private int randomLand;
	private int randomLandKind;
	private int randomX;
	private int randomY;
	private int score;
	private int stage;
	private int tail;
	private int temp;
	private int x;
	private int y;
	private int foods[];
	private int points[];
	private int infos[][];
	private Random random;
	private String string;
	private String strings[];
	private java.util.Timer music;
	private java.util.Timer timer;
	private Cursor cursor;
	private ImageIcon icon;
	private InputStream is;
	private AudioStream as;
	private MBACAdapter mbacAdapter;
	private SnakeAdapter snakeAdapter;
	private Container container;
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	private JComboBox combo;
	private JTextField field;
	private Box box[];
	private JButton button[];
	private JSlider slider[];
	private BlankPanel blankPanel[];
	private MapPanel panel[];
	private PicturePanel picturePanel[];
	private PictureTextPanel pictureTextPanel[];
	private TextPanel textPanel[];
	private MapPanel mapPanel[][];
	private String survival_maps[] = {"absurd","Abyss","agony","amethyst","being","BLAZE","capriccio","CAVE","Chercher","DEFAULT","flow","GAY","HALLUCINO","loose","Lupe","oblivion","prime","repeat","RETRIEVE","room","sensitive","siren"};
	
	//construct the main frame
	public MBAC()
	{
		super("MELTY BLOOD Act Cadenza");
		container = getContentPane();
		container.setBackground(Color.black);
		mbacAdapter = new MBACAdapter();
		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();
		icon = new ImageIcon("img\\cursor.png");
		cursor = getToolkit().createCustomCursor(icon.getImage(),new Point(0,0),Integer.toString(item));
		setCursor(cursor);
		icon = new ImageIcon("img\\icon.jpg");
		setFocusable(true);
		setIconImage(icon.getImage());
		setResizable(false);
		setSize(800,600);
		setVisible(true);
	}
	
	//display the main menu
	private void menu()
	{
		container.removeAll();
		container.setLayout(new BorderLayout(0,0));
		normal = false;
		box = new Box[7];
		blankPanel = new BlankPanel[9];
		picturePanel = new PicturePanel[7];
		pictureTextPanel = new PictureTextPanel[5];
		mainMenu();
		picturePanel[6] = new PicturePanel(500,567,"img\\logo1.jpg");
		box[0].add(picturePanel[6]);
		timer = new java.util.Timer();
		
		timer.schedule(new TimerTask()
		{
			public void run()
			{
				if (normal)
				{
					normal = false;
					picturePanel[6].setPicture("img\\logo1.jpg");
				}
				else
				{
					normal = true;
					picturePanel[6].setPicture("img\\logo2.jpg");
				}
			}
		},500,500);
		
		container.add(box[0],BorderLayout.NORTH);
	}
	
	//display the new game menu
	private void newGame()
	{
		container.removeAll();
		box = new Box[12];
		blankPanel = new BlankPanel[18];
		picturePanel = new PicturePanel[11];
		pictureTextPanel = new PictureTextPanel[5];
		mainMenu();
		box[7] = Box.createVerticalBox();
		picturePanel[6] = new PicturePanel(500,64,"img\\newgamelogo.jpg");
		box[7].add(picturePanel[6]);
		box[8] = Box.createHorizontalBox();
		blankPanel[9] = new BlankPanel(50,200);
		box[8].add(blankPanel[9]);
		picturePanel[7] = new PicturePanel(200,200,"img\\moon.jpg");
		box[8].add(picturePanel[7]);
		
		picturePanel[7].addMouseListener
		(
			new MouseAdapter()
			{
				public void mouseEntered(MouseEvent event)
				{
					picturePanel[8].setPicture("img\\normalmodeon.jpg");
				}
				
				public void mouseExited(MouseEvent event)
				{
					picturePanel[8].setPicture("img\\normalmodeoff.jpg");
				}
				
				public void mouseClicked(MouseEvent event)
				{
					chooseMap();
					setContentPane(container);
				}
			}
		);
		
		blankPanel[10] = new BlankPanel(250,200);
		box[8].add(blankPanel[10]);
		box[7].add(box[8]);
		box[9] = Box.createHorizontalBox();
		blankPanel[11] = new BlankPanel(200,50);
		box[9].add(blankPanel[11]);
		picturePanel[8] = new PicturePanel(250,50,"img\\normalmodeoff.jpg");
		box[9].add(picturePanel[8]);
		blankPanel[12] = new BlankPanel(50,50);
		box[9].add(blankPanel[12]);
		box[7].add(box[9]);
		blankPanel[13] = new BlankPanel(500,3);
		box[7].add(blankPanel[13]);
		box[10] = Box.createHorizontalBox();
		blankPanel[14] = new BlankPanel(250,200);
		box[10].add(blankPanel[14]);
		picturePanel[9] = new PicturePanel(200,200,"img\\sun.jpg");
		box[10].add(picturePanel[9]);
		
		picturePanel[9].addMouseListener
		(
			new MouseAdapter()
			{
				public void mouseEntered(MouseEvent event)
				{
					picturePanel[10].setPicture("img\\survivalmodeon.jpg");
				}
				
				public void mouseExited(MouseEvent event)
				{
					picturePanel[10].setPicture("img\\survivalmodeoff.jpg");
				}
				
				public void mouseClicked(MouseEvent event)
				{
					normal = false;
					stage = 0;
					snakeAdapter = new SnakeAdapter();
					AudioPlayer.player.stop(as);
					music.cancel();
					music = new java.util.Timer();
					
					music.schedule(new TimerTask()
					{
						public void run()
						{
							try
							{
								is = new FileInputStream("bgm\\survival.wav");
								as = new AudioStream(is);
								AudioPlayer.player.start(as);
							}
							catch(Exception e)
							{
								e.printStackTrace();
							}
						}
					},500,405500);
					
					stage();
					setContentPane(container);
				}
			}
		);
		
		blankPanel[15] = new BlankPanel(50,200);
		box[10].add(blankPanel[15]);
		box[7].add(box[10]);
		box[11] = Box.createHorizontalBox();
		blankPanel[16] = new BlankPanel(50,50);
		box[11].add(blankPanel[16]);
		picturePanel[10] = new PicturePanel(250,50,"img\\survivalmodeoff.jpg");
		box[11].add(picturePanel[10]);
		blankPanel[17] = new BlankPanel(200,50);
		box[11].add(blankPanel[17]);
		box[7].add(box[11]);
		box[0].add(box[7]);
		container.add(box[0],BorderLayout.NORTH);
	}
	
	//display the map editor
	private void mapEditor()
	{
		container.removeAll();
		layout = new GridBagLayout();
		constraints = new GridBagConstraints();
		container.setLayout(layout);
		current = 0;
		item = -1;
		button = new JButton[8];
		panel = new MapPanel[6];
		textPanel = new TextPanel[1];
		mapPanel = new MapPanel[15][15];
		
		for (int i = 0;i < 15;i ++)
		{
			for (int j = 0;j < 15;j ++)
			{
				mapPanel[i][j] = new MapPanel(infos[i][j]);
				addComponent(mapPanel[i][j],i,j,1,1);
				drawMap(mapPanel[i][j],i,j);
			}
		}
		
		constraints.insets = new Insets(4,4,4,4);
		button[0] = new JButton("HEAD");
		button[0].setBackground(Color.red);
		button[0].setFont(new Font("Monospaced",Font.BOLD,16));
		button[0].setForeground(Color.white);
		addComponent(button[0],20,0,3,2);
		
		button[0].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					current = 0;
					item = -1;
					
					for (int i = 0;i < 3;i ++)
					{
						button[i].setBackground(Color.black);
					}
					
					button[0].setBackground(Color.red);
					
					for (int i = 0;i < 4;i ++)
					{
						panel[i].setType(2 * i + 1);
					}
					
					for (int i = 4;i < 6;i ++)
					{
						panel[i].setType(100);
					}
				}
			}
		);
		
		button[1] = new JButton("FOOD");
		button[1].setBackground(Color.black);
		button[1].setFont(new Font("Monospaced",Font.BOLD,16));
		button[1].setForeground(Color.white);
		addComponent(button[1],23,0,3,2);
		
		button[1].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					current = 0;
					item = -1;
					
					for (int i = 0;i < 3;i ++)
					{
						button[i].setBackground(Color.black);
					}
					
					button[1].setBackground(Color.red);
					
					for (int i = 0;i < 4;i ++)
					{
						panel[i].setType(i + 10);
					}
					
					for (int i = 4;i < 6;i ++)
					{
						panel[i].setType(100);
					}
				}
			}
		);
		
		button[2] = new JButton("LAND");
		button[2].setBackground(Color.black);
		button[2].setFont(new Font("Monospaced",Font.BOLD,16));
		button[2].setForeground(Color.white);
		addComponent(button[2],26,0,3,2);
		
		button[2].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					current = 0;
					item = -1;
					
					for (int i = 0;i < 3;i ++)
					{
						button[i].setBackground(Color.black);
					}
					
					button[2].setBackground(Color.red);
					panel[0].setType(0);
					
					for (int i = 1;i < 6;i ++)
					{
						panel[i].setType(i + 13);
					}
				}
			}
		);
		
		for (int i = 0;i < 3;i ++)
		{
			panel[i] = new MapPanel(100);
			addComponent(panel[i],21 + 3 * i,2,3,2);
			chooseItem(panel[i],i);
		}
		
		for (int i = 3;i < 6;i ++)
		{
			panel[i] = new MapPanel(100);
			addComponent(panel[i],12 + 3 * i,4,3,2);
			chooseItem(panel[i],i);
		}
		
		for (int i = 0;i < 4;i ++)
		{
			panel[i].setType(2 * i + 1);
		}
		
		textPanel[0] = new TextPanel(150,64,16,"");
		addComponent(textPanel[0],20,6,9,3);
		button[3] = new JButton(" SAVE ");
		button[3].setBackground(Color.black);
		button[3].setFont(new Font("Monospaced",Font.BOLD,16));
		button[3].setForeground(Color.white);
		addComponent(button[3],20,9,3,2);
		
		button[3].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if (limitHead == 0)
					{
						textPanel[0].setText("No head!");
					}
					else
					{
						if (limitFood == 0)
						{
							textPanel[0].setText("No food!");
						}
						else
						{
							for (int i = 0;i < 15;i ++)
							{
								for (int j = 0;j < 15;j ++)
								{
									infos[i][j] = mapPanel[i][j].getType();
								}
							}
							
							save();
							setContentPane(container);
						}
					}
				}
			}
		);
		
		button[4] = new JButton(" LOAD ");
		button[4].setBackground(Color.black);
		button[4].setFont(new Font("Monospaced",Font.BOLD,16));
		button[4].setForeground(Color.white);
		addComponent(button[4],26,9,3,2);
		
		button[4].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					for (int i = 0;i < 15;i ++)
					{
						for (int j = 0;j < 15;j ++)
						{
							infos[i][j] = mapPanel[i][j].getType();
						}
					}
					
					load();
					setContentPane(container);
				}
			}
		);
		
		button[5] = new JButton("CLEAR");
		button[5].setBackground(Color.black);
		button[5].setFont(new Font("Monospaced",Font.BOLD,19));
		button[5].setForeground(Color.white);
		addComponent(button[5],20,11,3,2);
		
		button[5].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					for (int i = 0;i < 15;i ++)
					{
						limitFood = 0;
						limitHead = 0;
						limitLand = 0;
						
						for (int j = 0;j < 15;j ++)
						{
							mapPanel[i][j].setType(0);
						}
					}
				}
			}
		);
		
		button[6] = new JButton("RANDOM");
		button[6].setBackground(Color.black);
		button[6].setFont(new Font("Monospaced",Font.BOLD,16));
		button[6].setForeground(Color.white);
		addComponent(button[6],26,11,3,2);
		
		button[6].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					for (int i = 0;i < 15;i ++)
					{
						for (int j = 0;j < 15;j ++)
						{
							infos[i][j] = mapPanel[i][j].getType();
						}
					}
					
					random();
					setContentPane(container);
				}
			}
		);
		
		button[7] = new JButton("RETURN");
		button[7].setBackground(Color.black);
		button[7].setFont(new Font("Monospaced",Font.BOLD,16));
		button[7].setForeground(Color.white);
		addComponent(button[7],26,13,3,2);
		
		button[7].addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					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);
				}
			}
		);
	}
	
	//display the rankings menu
	private void rankings()
	{
		container.removeAll();
		strings = mbacAdapter.getRankings(true);
		box = new Box[12];
		blankPanel = new BlankPanel[11];
		picturePanel = new PicturePanel[10];
		pictureTextPanel = new PictureTextPanel[5];
		textPanel = new TextPanel[10];
		mainMenu();
		box[7] = Box.createVerticalBox();
		picturePanel[6] = new PicturePanel(500,64,"img\\rankingslogo.jpg");
		box[7].add(picturePanel[6]);
		box[8] = Box.createHorizontalBox();
		box[9] = Box.createVerticalBox();
		picturePanel[7] = new PicturePanel(100,64,"img\\normalon.jpg");
		box[9].add(picturePanel[7]);
		
		picturePanel[7].addMouseListener

⌨️ 快捷键说明

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