佰落暑期java自学记录-11

佰落暑期java自学记录-11
记录时间:2019-8-3
记录内容摘要:
用图形对话框来实现皇帝选妃

今天借助图形对话框来代替文本对话;实现皇帝选妃的图形对话框。
合理调试音乐,图片,对话框内容。

public class Kingpro 
{
	public static void main(String[] args) throws MalformedURLException, InterruptedException 
	{
//		Scanner input = new Scanner(System.in);
		
		AudioClip sound = Applet.newAudioClip(new File("皇帝选妃音效\\背景音乐.wav").toURL());
		sound.play();
		//开始界面

		JOptionPane.showMessageDialog(null, "欢迎来到皇帝选妃游戏", "游戏开始",0, new ImageIcon("images\\title.jpg"));

		int count = 5; 		//存放当前娘娘的总数
		int currday = 1;	//游戏进行到了第几天
		final int DAY_COUNT = 10;	//游戏一共进行10天
		int choice;			//存放用户选择
		int tempCount;		//临时变量,存放好感度低于60的个数
		int index = -1;		//用来存放删除,查找时的索引
		String name = null ;		//存放用户输入的姓名
		Object op ;
		//姓名数组,最多容纳六个字符串,每个字符串的最大长度位8个字符(英文)
		String[] names = {"西施","貂蝉","王昭君","杨玉环","赵飞燕",""};
		//级别数组,最多容纳五个字符串,每个字符串的最大长度位8个字符(英文)
		String[] levelNames = {"贵人","嫔妃","贵妃","皇贵妃","皇后",""};
		//用来存放妃子的等级,与leaveName联用,-1表示未启用
		int level[] = { 0, 0, 0, 0, 0, -1};
		//用来存放每个妃子的好感度,-1表示未启用
		int loves[] = {100, 100, 100, 100, 100, -1};
		
		
		do
		{
			//主界面
			String menu = "1、皇帝下旨选妃\n2、翻牌宠幸\n3、打入冷宫!\n4、朕的爱妃呢?\n陛下请选择:";
			op = JOptionPane.showInputDialog(null, menu, "游戏进行到第" + currday +"天", 0, new ImageIcon("images\\宝座.jpg"), 
					new String[] {"1","2","3","4"}, 1);
			
			if(op == null)
			{
				currday ++;
				continue;
			}
			else
				choice = Integer.parseInt(op.toString());

			sound.stop();
			
			
			switch(choice)
			{
			case 1://新增妃子,其他人好感度-10
				sound = Applet.newAudioClip(new File("皇帝选妃音效/选妃.wav").toURL());
				sound.play();
				
				//首先判断数组长度是否可以增加元素
				if(count >= level.length)
				{
					JOptionPane.showMessageDialog(null, "后宫人满为患,皇帝还是在等等吧", "后宫满员",0,new ImageIcon("images/人满.jpg"));
					break;
				}
				
				op = JOptionPane.showInputDialog(null, "请皇上下旨选妃", "下旨选妃", 0, new ImageIcon("images/待选妃子.jpg"),
						new String[] {"褒姒","钟无艳","陈圆圆","苏小小"}, 1);

				name = op.toString();
				//将新增元素放在count处;
				names[count] = name;
				level[count] = 0;
				loves[count] = 100;
				
				JOptionPane.showMessageDialog(null, "皇帝又选妃了,众爱妃好感度-10", "", 0, new ImageIcon("images/打入冷宫.jpg"));
				for(int i = 0; i < count; i ++)
				{
					loves[i] -= 10;
				}
				count ++;
				break;
			case 2://翻拍宠幸,宠幸的妃子好感度+20,其他人-10;
				sound.stop();
				sound = Applet.newAudioClip(new File("皇帝选妃音效/翻牌.wav").toURL());
				sound.play();
				
				op = JOptionPane.showInputDialog(null, "请选择宠幸的妃子", "翻牌宠幸", 0 , new ImageIcon("images/选妃翻牌.jpg"),names,1);
				name = op.toString();
				for(int i = 0; i < count; i ++)
				{
					if(name.equals(names[i]))
					{
						index = i;
						continue;
					}
					loves[i] -= 10;
				}
				if(index == -1)//没有找到
					JOptionPane.showMessageDialog(null, "皇帝不要做梦了,你没有这个妃子!", "找不到" + name +"妃子!", 0, new ImageIcon("images/做梦.jpg"));
				else
				{
					loves[index] += 20;
					//每临幸一次,级别提升1级,最高为4级,皇后;
					if(level[index] == 4)
						level[index] = 4;
					else
						level[index] ++;
					JOptionPane.showMessageDialog(null, "Duang~"+ names[index] +"等级提升为:"+ levelNames[level[index]] +"\n憔悴容华怯对春,寂寥宫殿锁闭门。\n此身却羡宫中树\n其她娘娘好感-10!", "翻牌结算", 0, new ImageIcon("images/嬉戏.jpg"));
	
				}
				index = -1;
				break;
			case 3://打入冷宫,执行删除操作,其她妃子好感度+10;
				sound.stop();
				sound = Applet.newAudioClip(new File("皇帝选妃音效/冷宫.wav").toURL());
				sound.play();
				op = JOptionPane.showInputDialog(null,"皇上要将哪位娘娘打入冷宫呢?", "打入冷宫", 0, new ImageIcon("images/打入冷宫.jpg"), names, null);
				name = op.toString();
				for(int i = 0; i < count;i ++)
				{
					if(name.equals(names[i]))
					{
						index = i;
						break;
					}
				}
				if(index == -1)//未找到要删除的姓名
				{
					JOptionPane.showMessageDialog(null, "虚惊一场,无人打入冷宫,该吃吃该喝喝~", "", 0, new ImageIcon("image/嬉戏.jpg"));
					break;
				}
				//执行删除操作
				for(int i = index; i < count - 1; i ++)
				{
					names[i] = names[i + 1];
					level[i] = level[i + 1];
					loves[i] = loves[i + 1];
				}
				count--;//总人数-1
				index = -1;//变量用完重置为初始值,方便下次使用
				JOptionPane.showMessageDialog(null, name + "已被打入冷宫!\n人情冷暖,所有娘娘好感度+10", "成功打入冷宫!", 0, new ImageIcon("images/成功打入冷宫.jpg"));
				for(int i = 0; i < count; i++)
				{
					loves[i] += 10;
				}
				index = -1;
				break;
			case 4://姓名查找,好感+10
				sound.stop();
				sound = Applet.newAudioClip(new File("皇帝选妃音效/一笑倾城.wav").toURL());
				sound.play();
				
				op = JOptionPane.showInputDialog(null, "请选择搜寻的妃子!", "搜寻妃子", 0,new ImageIcon("images/朕的爱妃呢.jpg"), names	, null);
				name =op.toString();
				for(int i = 0; i < count; i ++)
				{
					if(name.equals(names[i]))
					{
						JOptionPane.showMessageDialog(null, "微微一笑很倾城,"+ name +"娘娘的好感度+10",  name +"娘娘在此",0,new ImageIcon("images/"+ name +".jpg"));
						loves[i] += 10;
						break;
					}
				}
				break;
				default: JOptionPane.showMessageDialog(null, "皇帝不要做梦了,你没有这个妃子!", "找不到" + name +"妃子!", 0, new ImageIcon("images/做梦.jpg"));
			}//end of switch
			
			
			sound.stop();
			sound = Applet.newAudioClip(new File("皇帝选妃音效\\背景音乐.wav").toURL());
			sound.play();

			
			String daymessage = "";//输出每日结算的娘娘信息
			for(int i = 0; i < count; i ++)
				{
					daymessage += String.format("%s    %s    %d\n", names[i],levelNames[level[i]],loves[i]);
				}
			JOptionPane.showMessageDialog(null, daymessage, "每日结算", 0, new ImageIcon("images/选妃.jpg"));
			currday ++;
			
			
			//每天结算好感度,一旦有三个或以上的嫔妃好感度低于60,则发生暴乱,游戏结束
			tempCount = 0;//存放好感度低于60的个数
			for(int i = 0; i < count; i++)
			{
				if(loves[i] < 60)
					tempCount ++;
			}
			
			if(tempCount >= 3)
			{
				//借用时间函数来输出相应时间
				Calendar cal = Calendar.getInstance();
				JOptionPane.showMessageDialog(null, "皇上荒淫无度,后宫叛乱!\n"+"公元"+
				cal.get(Calendar.YEAR)+"年"+
				(cal.get(Calendar.MONTH) + 1)+"月" +
				cal.get(Calendar.DATE)+"日皇帝驾崩!共执政"+currday+"天", "皇帝驾崩",0, new ImageIcon("images/皇上被害.jpg"));
			
				System.exit(0);
		
			}
		}while(currday < DAY_COUNT);//当天数达到设定天数时结束
		
		sound.stop();
		sound = Applet.newAudioClip(new File("皇帝选妃音效\\背景音乐.wav").toURL());
		sound.play();
		

		String end = "";//输出游戏结束的结算信息
		for(int i = 0; i < count; i ++)
		{
			end += String.format("%s    %s    %d\n", names[i],levelNames[level[i]],loves[i]);
		}
		JOptionPane.showMessageDialog(null, end, "游戏结束", 0, new ImageIcon("images/选妃.jpg"));
		
		System.exit(0);
	}

你可能感兴趣的:(自学记录)