Java实现角色扮演游戏的示例代码
作者:小虚竹and掘金 时间:2023-03-31 19:41:45
前言
《模式策略的角色扮演游戏》游戏是自制的角色扮演游戏。选择两个角色,然后进行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();
}
来源:https://juejin.cn/post/7067521603612966942
标签:Java,角色,扮演,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实战项目 医院预约挂号系统
2023-09-18 06:56:44
![](https://img.aspxhome.com/file/2023/2/72632_0s.png)
使用Springboot对配置文件中的敏感信息加密
2021-05-24 10:50:25
![](https://img.aspxhome.com/file/2023/0/64020_0s.jpg)
使用spring框架实现数据库事务处理方式
2022-03-01 14:38:13
![](https://img.aspxhome.com/file/2023/8/62738_0s.png)
java抓包后对pcap文件解析示例
2022-11-23 20:21:53
java使用HashMap实现斗地主(有序版)
2021-08-30 10:24:10
android开机自启动APP及使用adb命令测试方法
2022-03-07 06:28:54
Android基于Intent实现Activity之间数据传递的方法
2022-08-01 16:34:04
C#字符集编码的使用及说明
2023-12-05 02:06:05
![](https://img.aspxhome.com/file/2023/3/78113_0s.png)
SpringBoot中的PUT和Delete请求使用
2022-01-22 19:33:32
![](https://img.aspxhome.com/file/2023/3/96053_0s.jpg)
Android实现打开各种文件的intent方法小结
2022-03-12 18:39:20
Java工具类之@RequestMapping注解
2023-11-16 03:00:54
![](https://img.aspxhome.com/file/2023/9/65789_0s.png)
SpringBoot2.0+阿里巴巴Sentinel动态限流实战(附源码)
2021-05-25 18:52:47
![](https://img.aspxhome.com/file/2023/1/83091_0s.jpg)
如何使用try-with-resource机制关闭连接
2022-04-25 01:36:54
![](https://img.aspxhome.com/file/2023/7/92547_0s.png)
基于Luhn算法的银行卡校验规则
2022-06-05 00:56:15
spring data jpa如何使用自定义repository实现类
2023-05-31 07:41:17
![](https://img.aspxhome.com/file/2023/0/94300_0s.png)
Java Socket 编程详解
2022-06-13 12:06:57
Android拍照上传功能示例代码
2022-08-22 07:51:39
Mybatis延迟加载的实现方式
2023-08-19 11:07:32
![](https://img.aspxhome.com/file/2023/8/67018_0s.png)
Javaweb中Request获取表单数据的四种方法详解
2023-10-07 03:07:02
![](https://img.aspxhome.com/file/2023/6/72606_0s.png)
SpringBoot 读取yml文件的多种方式汇总
2023-08-03 13:10:43