iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java实现角色扮演游戏的示例代码
  • 487
分享到

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

2024-04-02 19:04:59 487人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录前言主要设计功能截图代码实现游戏启动类抽象类:游戏角色类魔法行为接口总结前言 《模式策略的角色扮演游戏》游戏是自制的角色扮演游戏。选择两个角色,然后进行PK,可用来学习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");
			  
				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==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角色扮演游戏的资料请关注编程网其它相关文章!

--结束END--

本文标题: Java实现角色扮演游戏的示例代码

本文链接: https://www.lsjlt.com/news/140126.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作