JAVA 实现《模式策略的角色扮演游戏》游戏

前言

《模式策略的角色扮演游戏》游戏是自制的角色扮演游戏。选择两个角色,然后进行PK,可用来学习JAVA的接口,继承和多态。

主要设计

  1. 事先设计好英雄,血量和相关技能。

  2. 为了让玩家能与程序互动,使用下面这个命令可达效果

    Scanner sc = new Scanner(System.in);
    
  3. 运行StartMain里的main方法

  4. 设计四个角色

    1.Queen 2.King 3.Knight 4.Troll

  5. 角色可选择使用魔法攻击或者武器攻击

  6. 血量为0,则结束战斗

  7. 抽象出游戏角色类,然后不同的角色再继承去实现自己的个性化。

  8. 魔法行为可用接口定义,不同的魔法效果,再去实现这个魔法行为接口。

  9. 开发环境使用JDK8+IDEA

功能截图

游戏开始:

选择角色

JAVA 实现《模式策略的角色扮演游戏》游戏_第1张图片

操作技能

JAVA 实现《模式策略的角色扮演游戏》游戏_第2张图片

代码实现

游戏启动类


public class StartMain {
public static void main(String[] args)
{

	System.out.println("welcome to the game! please create two rolls.");
	System.out.println("please choose the character of the first roll: 1.Queen 2.King 3.Knight 4.Troll");
	Scanner sc=new Scanner(System.in);
	int i=sc.nextInt();
	/**
     * 第一个角色的创建
     */
	if(i==1) 
	{
		      
		      Scanner sc1=new Scanner(System.in);
              String str=null;
              System.out.print("请输入角色名字:");
			  str=sc1.nextLine();
			  Characters character1=new Queen(str);
			  System.out.println("please choose the character of the second roll: 1.Queen 2.King 3.Knight 4.Troll");
			  /**hitpoint
			   * 第二个角色的创建
			   */
				Scanner sc2=new Scanner(System.in);
				int j=sc2.nextInt();
				if(j==1) 
				{
					      Scanner sc3=new Scanner(System.in);
			              String str1=null;
			              System.out.print("请输入角色名字:");
						  str1=sc3.nextLine();
					      Characters character2= new Queen(str1);
					      while(character1.hitpoint>0&&character2.hitpoint>0) {
					    	  /**
					    	   * 当前玩家若被使用frozen魔法,失去一次进攻或使用魔法的机会
					    	   */
					    	  if(character1.frozenchoice==1)
					    	  {
					    		  System.out.println("the player has been frozen, skip to next player");
					    		  character1.frozenchoice=0;
					    	  }
					    	  else {
					    		  
					    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
					    		  /**
					    		   * 用户键入选择使用魔法还是进攻
					    		   */
					    		  Scanner sc4=new Scanner(System.in);
						    		int h=sc4.nextInt();
						    		/**
						    		 * 判断魔法值是否大于80,若魔法值小于80则无法进攻,必须使用recover魔法恢复魔法值
						    		 */
					    		  if(character1.magicpoint<80)
					    			{
					    				System.out.println("your magicpoint is too low, please do magic to recover");
					    				h=2;
					    			}
					    		ok: if(h==1)
					    		{
					    			/**
					    			 * 若对方上回合使用invisible魔法,则本回合攻击无效
					    			 */
					    			if(character2.invisiblechoice==1)
					    			{
					    				System.out.println("the opponebt is invisible, skip this fight");
					    				character2.invisiblechoice=0;
					    				break ok;
					    				/**
					    				 * 跳出攻击
					    				 */
					    			}
					    			character1.fight(character2);
					    		}
					    		if(h==2)
					    		{
					    		  character1.performMagic(character2);
					    		}
					    		}
					    	  /**
					    	   * 一位玩家进攻或使用魔法结束后根据对方hitpoint是否小于等于零来判断游戏是否结束
					    	   */
					    	  if(character1.hitpoint<=0)
					    	  {
					    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
					    		  System.exit(0);
					    	  }
					    	  /**
					    	   * 轮到第二位玩家开始操作
					    	   * 以下情况与第一位玩家操作阶段一致
					    	   */
					    		if(character2.frozenchoice==1)
						    	  {
						    		  System.out.println("the player has been frozen, skip to next player");
						    		  character2.frozenchoice=0;
						    	  }
					    		else {
					    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
					    	    Scanner sc5=new Scanner(System.in);
					    		int g=sc5.nextInt();
					    		if(character2.magicpoint<80)
				    			{
				    				System.out.println("your magicpoint is too low, please do magic to recover");
				    				g=2;
				    			}
					    		ok1: if(g==1)
					    		{
					    			if(character1.invisiblechoice==1)
					    			{
					    				System.out.println("the opponebt is invisible, skip this fight");
					    				character1.invisiblechoice=0;
					    				break ok1;
					    			}
					    			character2.fight(character1);
					    		}
					    		if(g==2)
					    		{
					    		  character2.performMagic(character1);
					    		}
					    		}
					    		if(character2.hitpoint<=0)
						    	  {
						    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
						    		  System.exit(0);
						    	  }
					      }
					      
				}
				else if(j==2)
					/**
					 * 当第二位玩家的选择为2号角色时
					 * 以下操作同上
					 */
				{
					Scanner sc3=new Scanner(System.in);
		              String str1=null;
		              System.out.print("请输入角色名字:");
					  str1=sc3.nextLine();
				      Characters character2= new King(str1);
				      while(character1.hitpoint>0&&character2.hitpoint>0) {
				    	  if(character1.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character1.frozenchoice=0;
				    	  }
				    	  else {
				    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
				    	    Scanner sc4=new Scanner(System.in);
				    		int h=sc4.nextInt();
				    		if(character1.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				h=2;
			    			}
				    		ok: if(h==1)
				    		{
				    			if(character2.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character2.invisiblechoice=0;
				    				break ok;
				    			}
				    			character1.fight(character2);
				    		}
				    		if(h==2)
				    		{
				    		  character1.performMagic(character2);
				    		}
				    		}
				    	  if(character1.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
				    		  System.exit(0);
				    	  }
				    		if(character2.frozenchoice==1)
					    	  {
					    		  System.out.println("the player has been frozen, skip to next player");
					    		  character2.frozenchoice=0;
					    	  }
				    		else {
				    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
				    	    Scanner sc5=new Scanner(System.in);
				    		int g=sc5.nextInt();
				    		if(character2.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				g=2;
			    			}
				    		ok1: if(g==1)
				    		{
				    			if(character1.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character1.invisiblechoice=0;
				    				break ok1;
				    			}
				    			character2.fight(character1);
				    		}
				    		if(g==2)
				    		{
				    		  character2.performMagic(character1);
				    		}
				    		}
				    		if(character2.hitpoint<=0)
					    	  {
					    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
					    		  System.exit(0);
					    	  }
				      }
				}
				else if(j==3)
					/**
					 * 当第二位玩家的选择为3号角色时
					 * 以下操作同上
					 */
				{
					Scanner sc3=new Scanner(System.in);
		              String str1=null;
		              System.out.print("请输入角色名字:");
					  str1=sc3.nextLine();
				      Characters character2= new Knight(str1);
				      while(character1.hitpoint>0&&character2.hitpoint>0) {
				    	  if(character1.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character1.frozenchoice=0;
				    	  }
				    	  else {
				    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
				    	    Scanner sc4=new Scanner(System.in);
				    		int h=sc4.nextInt();
				    		if(character1.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				h=2;
			    			}
				    		ok: if(h==1)
				    		{
				    			if(character2.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character2.invisiblechoice=0;
				    				break ok;
				    			}
				    			character1.fight(character2);
				    		}
				    		if(h==2)
				    		{
				    		  character1.performMagic(character2);
				    		}
				    		}
				    	  if(character1.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
				    		  System.exit(0);
				    	  }
				    		if(character2.frozenchoice==1)
					    	  {
					    		  System.out.println("the player has been frozen, skip to next player");
					    		  character2.frozenchoice=0;
					    	  }
				    		else {
				    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
				    	    Scanner sc5=new Scanner(System.in);
				    		int g=sc5.nextInt();
				    		if(character2.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				g=2;
			    			}
				    		ok1: if(g==1)
				    		{
				    			if(character1.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character1.invisiblechoice=0;
				    				break ok1;
				    			}
				    			character2.fight(character1);
				    		}
				    		if(g==2)
				    		{
				    		  character2.performMagic(character1);
				    		}
				    		}
				    		if(character2.hitpoint<=0)
					    	  {
					    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
					    		  System.exit(0);
					    	  }
				      }
				}
				else if (j==4)
					/**
					 * 当第二位玩家的选择为4号角色时
					 * 以下操作同上
					 */
				{

					Scanner sc3=new Scanner(System.in);
		              String str1=null;
		              System.out.print("请输入角色名字:");
					  str1=sc3.nextLine();
				      Characters character2= new Troll(str1);
				      while(character1.hitpoint>0&&character2.hitpoint>0){
				    	  if(character1.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character1.frozenchoice=0;
				    	  }
				    	  else {
				    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
				    	    Scanner sc4=new Scanner(System.in);
				    		int h=sc4.nextInt();
				    		if(character1.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				h=2;
			    			}
				    		ok: if(h==1)
				    		{
				    			if(character2.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character2.invisiblechoice=0;
				    				break ok;
				    			}
				    			character1.fight(character2);
				    		}
				    		if(h==2)
				    		{
				    		  character1.performMagic(character2);
				    		}
				    		}
				    	  if(character1.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
				    		  System.exit(0);
				    	  }
				    		if(character2.frozenchoice==1)
					    	  {
					    		  System.out.println("the player has been frozen, skip to next player");
					    		  character2.frozenchoice=0;
					    	  }
				    		else {
				    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
				    	    Scanner sc5=new Scanner(System.in);
				    		int g=sc5.nextInt();
				    		if(character2.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				g=2;
			    			}
				    		ok1: if(g==1)
				    		{
				    			if(character1.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character1.invisiblechoice=0;
				    				break ok1;
				    			}
				    			character2.fight(character1);
				    		}
				    		if(g==2)
				    		{
				    		  character2.performMagic(character1);
				    		}
				    		}
				    		if(character2.hitpoint<=0)
					    	  {
					    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
					    		  System.exit(0);
					    	  }
				      }
				}
				
	}
	else if(i==2)
		/**
		 * 一号玩家创建角色的第二种选择,以下操作与第一种选择相似
		 */
	{
		Scanner sc1=new Scanner(System.in);
        String str=null;
        System.out.print("请输入角色名字:");
		  str=sc1.nextLine();
		  Characters character1=new King(str);
		  System.out.println("please choose the character of the second roll: 1.Queen 2.King 3.Knight 4.Troll");
			Scanner sc2=new Scanner(System.in);
			int j=sc2.nextInt();
			if(j==1) 
			{
				      Scanner sc3=new Scanner(System.in);
		              String str1=null;
		              System.out.print("请输入角色名字:");
					  str1=sc3.nextLine();
				      Characters character2= new Queen(str1);
				      while(character1.hitpoint>0&&character2.hitpoint>0) {
				    	  if(character1.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character1.frozenchoice=0;
				    	  }
				    	  else {
				    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
				    	    Scanner sc4=new Scanner(System.in);
				    		int h=sc4.nextInt();
				    		if(character1.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				h=2;
			    			}
				    		ok: if(h==1)
				    		{
				    			if(character2.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character2.invisiblechoice=0;
				    				break ok;
				    			}
				    			character1.fight(character2);
				    		}
				    		if(h==2)
				    		{
				    		  character1.performMagic(character2);
				    		}
				    		}
				    	  if(character1.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
				    		  System.exit(0);
				    	  }
				    		if(character2.frozenchoice==1)
					    	  {
					    		  System.out.println("the player has been frozen, skip to next player");
					    		  character2.frozenchoice=0;
					    	  }
				    		else {
				    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
				    	    Scanner sc5=new Scanner(System.in);
				    		int g=sc5.nextInt();
				    		if(character2.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				g=2;
			    			}
				    		ok1: if(g==1)
				    		{
				    			if(character1.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character1.invisiblechoice=0;
				    				break ok1;
				    			}
				    			character2.fight(character1);
				    		}
				    		if(g==2)
				    		{
				    		  character2.performMagic(character1);
				    		}
				    		}
				    		if(character2.hitpoint<=0)
					    	  {
					    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
					    		  System.exit(0);
					    	  }
				      }
				      
			}
			else if(j==2)
			{
				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new King(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0) {
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			else if(j==3)
			{
				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new Knight(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0) {
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			else if (j==4)
			{

				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new Troll(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0){
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			
	}
	else if(i==3)
		/**
		 * 一号玩家创建角色的第三种选择,以下操作与第一种选择相似
		 */
	{
		Scanner sc1=new Scanner(System.in);
        String str=null;
        System.out.print("请输入角色名字:");
		  str=sc1.nextLine();
		  Characters character1=new Knight(str);
		  System.out.println("please choose the character of the second roll: 1.Queen 2.King 3.Knight 4.Troll");
			Scanner sc2=new Scanner(System.in);
			int j=sc2.nextInt();
			if(j==1) 
			{
				      Scanner sc3=new Scanner(System.in);
		              String str1=null;
		              System.out.print("请输入角色名字:");
					  str1=sc3.nextLine();
				      Characters character2= new Queen(str1);
				      while(character1.hitpoint>0&&character2.hitpoint>0);{
				    	  if(character1.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character1.frozenchoice=0;
				    	  }
				    	  else {
				    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
				    	    Scanner sc4=new Scanner(System.in);
				    		int h=sc4.nextInt();
				    		if(character1.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				h=2;
			    			}
				    		ok: if(h==1)
				    		{
				    			if(character2.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character2.invisiblechoice=0;
				    				break ok;
				    			}
				    			character1.fight(character2);
				    		}
				    		if(h==2)
				    		{
				    		  character1.performMagic(character2);
				    		}
				    		}
				    	  if(character1.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
				    		  System.exit(0);
				    	  }
				    		if(character2.frozenchoice==1)
					    	  {
					    		  System.out.println("the player has been frozen, skip to next player");
					    		  character2.frozenchoice=0;
					    	  }
				    		else {
				    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
				    	    Scanner sc5=new Scanner(System.in);
				    		int g=sc5.nextInt();
				    		if(character2.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				g=2;
			    			}
				    		ok1: if(g==1)
				    		{
				    			if(character1.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character1.invisiblechoice=0;
				    				break ok1;
				    			}
				    			character2.fight(character1);
				    		}
				    		if(g==2)
				    		{
				    		  character2.performMagic(character1);
				    		}
				    		}
				    		if(character2.hitpoint<=0)
					    	  {
					    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
					    		  System.exit(0);
					    	  }
				      }
				      
			}
			else if(j==2)
			{
				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new King(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0); {
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			else if(j==3)
			{
				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new Knight(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0){
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			else if (j==4)
			{

				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new Troll(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0){
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			
	}
	else if(i==4)
		/**
		 * 一号玩家创建角色的第四种选择,以下操作与第一种选择相似
		 */
	{
		Scanner sc1=new Scanner(System.in);
        String str=null;
        System.out.print("请输入角色名字:");
		  str=sc1.nextLine();
		  Characters character1=new Troll(str);
		  System.out.println("please choose the character of the second roll: 1.Queen 2.King 3.Knight 4.Troll");
			Scanner sc2=new Scanner(System.in);
			int j=sc2.nextInt();
			if(j==1) 
			{
				      Scanner sc3=new Scanner(System.in);
		              String str1=null;
		              System.out.print("请输入角色名字:");
					  str1=sc3.nextLine();
				      Characters character2= new Queen(str1);
				      while(character1.hitpoint>0&&character2.hitpoint>0);{
				    	  if(character1.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character1.frozenchoice=0;
				    	  }
				    	  else {
				    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
				    	    Scanner sc4=new Scanner(System.in);
				    		int h=sc4.nextInt();
				    		if(character1.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				h=2;
			    			}
				    		ok: if(h==1)
				    		{
				    			if(character2.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character2.invisiblechoice=0;
				    				break ok;
				    			}
				    			character1.fight(character2);
				    		}
				    		if(h==2)
				    		{
				    		  character1.performMagic(character2);
				    		}
				    		}
				    	  if(character1.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
				    		  System.exit(0);
				    	  }
				    		if(character2.frozenchoice==1)
					    	  {
					    		  System.out.println("the player has been frozen, skip to next player");
					    		  character2.frozenchoice=0;
					    	  }
				    		else {
				    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
				    	    Scanner sc5=new Scanner(System.in);
				    		int g=sc5.nextInt();
				    		if(character2.magicpoint<80)
			    			{
			    				System.out.println("your magicpoint is too low, please do magic to recover");
			    				g=2;
			    			}
				    		ok1: if(g==1)
				    		{
				    			if(character1.invisiblechoice==1)
				    			{
				    				System.out.println("the opponebt is invisible, skip this fight");
				    				character1.invisiblechoice=0;
				    				break ok1;
				    			}
				    			character2.fight(character1);
				    		}
				    		if(g==2)
				    		{
				    		  character2.performMagic(character1);
				    		}
				    		}
				    		if(character2.hitpoint<=0)
					    	  {
					    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
					    		  System.exit(0);
					    	  }
				      }
				      
			}
			else if(j==2)
			{
				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new King(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0); {
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			else if(j==3)
			{
				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new Knight(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0){
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character2.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			else if (j==4)
			{

				Scanner sc3=new Scanner(System.in);
	              String str1=null;
	              System.out.print("请输入角色名字:");
				  str1=sc3.nextLine();
			      Characters character2= new Troll(str1);
			      while(character1.hitpoint>0&&character2.hitpoint>0);{
			    	  if(character1.frozenchoice==1)
			    	  {
			    		  System.out.println("the player has been frozen, skip to next player");
			    		  character1.frozenchoice=0;
			    	  }
			    	  else {
			    		  System.out.println("please choose the first player's operation:1.fight 2.do magic");
			    	    Scanner sc4=new Scanner(System.in);
			    		int h=sc4.nextInt();
			    		if(character1.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				h=2;
		    			}
			    		ok: if(h==1)
			    		{
			    			if(character2.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character2.invisiblechoice=0;
			    				break ok;
			    			}
			    			character1.fight(character2);
			    		}
			    		if(h==2)
			    		{
			    		  character1.performMagic(character2);
			    		}
			    		}
			    	  if(character1.hitpoint<=0)
			    	  {
			    		  System.out.println("the"+character1.name+"is dead, the"+character2.name+"win");
			    		  System.exit(0);
			    	  }
			    		if(character2.frozenchoice==1)
				    	  {
				    		  System.out.println("the player has been frozen, skip to next player");
				    		  character2.frozenchoice=0;
				    	  }
			    		else {
			    	    System.out.println("please choose the second player's operation:1.fight 2.do magic");
			    	    Scanner sc5=new Scanner(System.in);
			    		int g=sc5.nextInt();
			    		if(character2.magicpoint<80)
		    			{
		    				System.out.println("your magicpoint is too low, please do magic to recover");
		    				g=2;
		    			}
			    		ok1: if(g==1)
			    		{
			    			if(character1.invisiblechoice==1)
			    			{
			    				System.out.println("the opponebt is invisible, skip this fight");
			    				character1.invisiblechoice=0;
			    				break ok1;
			    			}
			    			character2.fight(character1);
			    		}
			    		if(g==2)
			    		{
			    		  character2.performMagic(character1);
			    		}
			    		}
			    		if(character2.hitpoint<=0)
				    	  {
				    		  System.out.println("the"+character1.name+"is dead, the"+character1.name+"win");
				    		  System.exit(0);
				    	  }
			      }
			}
			
	}
}
}


抽象类:游戏角色类


public abstract class Characters {
protected String name;
protected WeaponBehavior weapon;
protected int hitpoint=100;
protected MagicBehavior magic;
protected int magicpoint=100;
protected int damage;
protected int defense;
protected int damagetotal;
protected int invisiblechoice;
protected int frozenchoice;
public void fight(Characters C)
{
	System.out.println("fight:"+C.name);
	System.out.println("please choose your weapon:1.Sword 2.Knife 3.Bow and Arrow 4.Axe");
	Scanner sc=new Scanner(System.in);
	int i=sc.nextInt();
	/**
	 * 根据用户键盘输入动态设置角色武器
	 */
	switch(i) {
	case 1:{
		this.setWeaponBehavior(new SwordBehavior());
		this.weapon.useWeapon();
		this.magicpoint=this.magicpoint-6;
		this.damagetotal=this.damage+4;
	}
	break;
	case 2:{
		this.setWeaponBehavior(new KnifeBehavior());
		this.weapon.useWeapon();
		this.magicpoint=this.magicpoint-15;
	    this.damagetotal=this.damage+2;
	}
	break;
	case 3:{
		this.setWeaponBehavior(new BowAndArrowBehavior());
		this.weapon.useWeapon();
		this.magicpoint=this.magicpoint-12;
		this.damagetotal=this.damage+7;
		break;
	}
	case 4:{
		this.setWeaponBehavior(new AxeBehavior());
		this.weapon.useWeapon();
		this.magicpoint=this.magicpoint-4;
		this.damagetotal=this.damage+3;
	}
	break;
	
	}
	System.out.println(C.name+" : hitpoint-"+this.damagetotal);
	C.hitpoint=C.hitpoint-this.damagetotal;
	
}
public void performMagic(Characters C)
{
	System.out.println("do magic to "+C.name);
	System.out.println("please choose the magic 1.invisible 2.heal 3.frozen");
	Scanner sc=new Scanner(System.in);
	int i=sc.nextInt();
	System.out.println("please choose the magic receiver: 1.yourself 2.opponent");
	Scanner sc1=new Scanner(System.in);
	int a=sc1.nextInt();
	/**
	 * 根据用户键盘输入设置魔法以及作用对象
	 */
	if(a==1) {
	switch(i) {
	case 1:{
		this.setMagicBehavior(new InvisibleBehavior());
		this.invisiblechoice =1;
		this.magic.useMagic();
		}
	break;
	case 2:{
		this.setMagicBehavior(new HealBehavior());
		this.magic.useMagic();
		System.out.println("hitpoint +5, magicpoint +10");
		this.hitpoint=this.hitpoint+5;
		this.magicpoint=this.magicpoint+10;
	}
	break;
	case 3:{
		this.frozenchoice=1;
		this.setMagicBehavior(new FrozenBehavior());
		this.magic.useMagic();
	}
	break;
	}
	}
	else if(a==2) {
		switch(i) {
		case 1:{
			this.setMagicBehavior(new InvisibleBehavior());
			C.invisiblechoice =1;
			this.magic.useMagic();
			}
		break;
		case 2:{
			this.setMagicBehavior(new HealBehavior());
			this.magic.useMagic();
			System.out.println("hitpoint +5, magicpoint +10");
			C.hitpoint=this.hitpoint+5;
			C.magicpoint=this.magicpoint+10;
		}
		break;
		case 3:{
			C.frozenchoice=1;
			this.setMagicBehavior(new FrozenBehavior());
			this.magic.useMagic();
		}
		break;
		}
		
	}
	else {
		System.out.println("please input correct choice!");
	}
	
}
public void setWeaponBehavior(WeaponBehavior w)
{
	this.weapon=w;
}/**动态设置角色武器*/
public void setMagicBehavior(MagicBehavior m)
{
	this.magic=m;
}/**动态设计角色魔法*/
public String getName()
{
	return this.name;
}
public void display()
{
	System.out.println("It's a"+this.name);
}
}

魔法行为接口

public interface MagicBehavior {
void useMagic();
}

总结

通过此次的《模式策略的角色扮演游戏》实现,让我对JAVA的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。

java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。

源码获取

源码下载地址:传送门------->

点赞,关注博主后,私聊博主免费获取
需要技术指导,写项目程序,等更多服务请联系博主

今天是持续写作的第 20 / 100 天。
可以关注我,点赞我、评论我、收藏我啦。

你可能感兴趣的:(JAVA,游戏库,java,游戏,intellij-idea)