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

功能截图

游戏开始:

选择角色

操作技能

代码实现

游戏启动类

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最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。

以上就是Java实现角色扮演游戏的示例代码的详细内容,更多关于Java角色扮演游戏的资料请关注我们其它相关文章!

(0)

相关推荐

  • Java实现经典大富翁游戏的示例详解

    目录 前言 主要设计 功能截图 代码实现 总结 前言 大富翁,又名地产大亨.是一种多人策略图版游戏.参与者分得游戏金钱,凭运气(掷骰子)及交易策略,买地.建楼以赚取租金.英文原名monopoly意为“垄断”,因为最后只得一个胜利者,其余均破产收场. <大富翁>游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 可多人参与的大富翁游戏,玩家有初始资金,通过掷骰子,玩家移动指定骰子点数步骤,根据对应格子上的交易策略,来决定是赚钱还是亏钱,其他玩家破

  • Java实现经典角色扮演侦探游戏游戏的示例代码

    目录 前言 游戏背景 主要需求 主要设计 功能截图 代码实现 游戏主界面 主卧 初始化 大厅 总结 前言 游戏背景 百变山庄坐落于太平洋的一座小岛上,山庄主人亦是小岛的主人.这个神秘主人细致周到,邀请函里不仅附着往返港口的机票,港口的邮船也是通往小岛的专线. 初登小岛,恢宏大气的山庄直入眼帘,通过门廊,金碧辉煌的大厅震人心魄. 受邀的侦探们陆续到齐,[侍者]彬彬有礼地站在一旁,他安排你们围坐在一个奇特十边形的桌子旁稍加等待.[侦探指尖]回忆着自己临行前调查的各位名侦探的资料,除了那个神秘的[电话

  • Java实现英文猜词游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 游戏启动类 处理 单词判断 总结 前言 <英文猜词游戏>代码行数没有超过200行,是之前为了背英语单词,特意研发的小游戏. 主要设计 1.事先准备单词文本. 2.为了让玩家能与程序互动,使用下面这个命令可达效果 Scanner sc = new Scanner(System.in); 3.运行WordleMaster里的main方法 4.在Wordle中输入第一个单词(默认第一个单词是abort,会显示在console中.可在代码中修改) 5.将Wor

  • Java实现经典捕鱼达人游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 游戏窗体 鱼 鱼池类继承自Jpanel 总结 前言 <捕鱼达人>是一款以深海狩猎为题材的休闲竞技游戏.这是一场海底世界的远征,享受捕获大鱼的乐趣,但不是所有的鱼都是友善的,它们会用自己的方式保护自己,保卫属于自己的海底世界.当然,这里也是冒险与机遇共存的地方,诸多埋藏于海底的宝藏等待着被探寻. 游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 在鱼池中有很多鱼,鱼各自游动. 有一张渔网,随鼠标移动,点

  • Java实现俄罗斯方块游戏简单版

    本文实例为大家分享了Java实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 游戏页面效果如下: 俄罗斯方块游戏本身的逻辑: 俄罗斯方块游戏的逻辑是比较简单的.它就类似于堆砌房子一样,各种各样的方地形状是不同的.但是,俄罗斯方块游戏的界面被等均的分为若干行和若干列,因此方块的本质就是占用了多少个单元. 首先来考虑一下数据的问题.对于界面来说,需要一个二维的 int 型数组,它保存着那些地方应该有着色,哪些没有:然后是方块本身,尽管它们的形状不统一,但是它们可以用一个4X4比例的方块所包围,

  • Java实现经典拳皇误闯冒险岛游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 游戏主界面 英雄 总结 前言 <拳皇误闯冒险岛>是拳皇和冒险岛素材的基于JavaSwing的动作类游戏,独创改编. 主要需求 拳皇迷迷糊糊醒来,发现自己在一间废弃的工厂里,地上爬满怪兽..这么可爱的怪兽,一拳下去,应该会哭很久吧~拳皇心里吐槽了下,向怪兽的怀抱冲了上去~~ 主要设计 1.游戏面板生成显示 2.背景选用冒险岛素材图 3.设计英雄,包含生命值,法术值,英雄的动作变化处理,英雄的技能特效 4.设计怪兽,包含怪物血量,攻击力,位置,步长等 5.

  • Java实现经典游戏2048的示例代码

    目录 前言 主要设计 功能截图 代码实现 界面布局类 业务逻辑类 总结 前言 2014年Gabriele Cirulli利用周末的时间写2048这个游戏的程序,仅仅只是好玩而已.他想用一种不同的视觉展现效果和更快速的动画来创造属于自己的游戏版本. 游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 每次控制所有方块向同一个方向运动,两个相同数字的方块撞在一起之后合并成为他们的和,每次操作之后会在空白的方格处随机生成一个2或者4,最终得到一个“20

  • Java实现角色扮演游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 游戏启动类 抽象类:游戏角色类 魔法行为接口 总结 前言 <模式策略的角色扮演游戏>游戏是自制的角色扮演游戏.选择两个角色,然后进行PK,可用来学习JAVA的接口,继承和多态. 主要设计 1.事先设计好英雄,血量和相关技能. 2.为了让玩家能与程序互动,使用下面这个命令可达效果 Scanner sc = new Scanner(System.in); 3.运行StartMain里的main方法 4.设计四个角色 1.Queen 2.King 3.Kni

  • Java实现萝卜勇者游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 启动类 键盘监听 核心算法 总结 前言 <萝卜勇者>是由国内玩家自制的一款独立游戏,玩家扮演萝卜勇士闯关,打败各种邪恶的敌人,获得最后的胜利. <萝卜勇者>游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 参考<萝卜勇者>的剧情,实现JAVA版本的单机游戏. 主要设计 1. 用Swing库做可视化界面 2.键盘监听,用WSAD可以控制光标移动,J是确定,K是取消,游戏中,WSA

  • JAVA实现经典扫雷游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 总结 前言 windows自带的游戏<扫雷>是陪伴了无数人的经典游戏,本程序参考<扫雷>的规则进行了简化,用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 1.要有难度等级,初级,中级,高级 2.由玩家逐个翻开方块,以找出所有地雷为最终游戏目标.如果玩家翻开的方块有地雷,则游戏结束 3.游戏主区域由很多个方格组成.使用鼠标左键随机点击一个方格,方格即被打开并显示出方格中的数字:方格中数字则表示其

  • Java+Swing实现五子棋游戏的示例代码

    目录 一.系统介绍 1.开发环境 2.技术选型 3.系统功能 二.系统展示 三.部分代码 AI.java Chess.java Gobang.java GobangListener.java 一.系统介绍 1.开发环境 开发工具:Eclipse2021 JDK版本:jdk1.8 Mysql版本:8.0.13 2.技术选型 Java+Swing 3.系统功能 实现五子棋游戏,开始游戏,悔棋,认输,退出功能. 二.系统展示 1.首页 2.黑棋走 3.白棋走 三.部分代码 AI.java packag

  • Java实现单机版五子棋游戏的示例代码

    目录 前言 主要需求 主要设计 功能截图 代码实现 总结 前言 五子棋是世界智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏,是世界智力运动会竞技项目之一,通常双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成5子连线者获胜. 棋具与围棋通用,起源于中国上古时代的传统黑白棋种之一.主要流行于华人和汉字文化圈的国家以及欧美一些地区,是世界上最古老的棋. 容易上手,老少皆宜,而且趣味横生,引人入胜:不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性. 用java语言实现

  • Java实现贪吃蛇游戏的示例代码

    目录 项目演示 项目实战 1. 游戏的主启动类 2. 游戏的面板 3. 数据中心 4. 绘制静态面板 5. 绘制静态小蛇 6. 绘制动态小蛇 7. 设置游戏状态 8. 让蛇动起来 9. 绘制食物布局 10. 游戏失败判定 11. 积分获取系统 12. 游戏优化 项目演示 项目演示地址 项目实战 1. 游戏的主启动类 作为贪吃蛇游戏的主启动类,构建了顶级窗口,可以容纳各种面板, package Snake; import javax.swing.*; /** * 游戏的主启动类 */ public

  • Java实现AI五子棋游戏的示例代码

    目录 前言 实现过程 抽象 实现AI接口 评估函数 前言 本文只是介绍五子棋AI的实现,最终的成品只是一个 AI 接口,并不包括 GUI,且不依赖 GUI. 五子棋 AI 的实现并不难,只需要解决一个问题就行: 怎么确定AI的最佳落子位置? 一般情况下,五子棋棋盘是由15条横线和15条纵线组合而成的,15x15 的棋盘共有 225 个交叉点,也就是说共有 225 个落子点. 假如说,AI 是黑棋,先行落子,所以 AI 总共有 225 个落子点可以选择,我们可以对每个落子点进行评估打分,哪个分高下

随机推荐