Java实现角色扮演游戏的示例代码
作者:小虚竹and掘金 发布时间:2023-03-31 19:41:45
标签: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();
}
来源:https://juejin.cn/post/7067521603612966942


猜你喜欢
- 1. 前言Spring除了IOC和DI,还有另一个杀手锏功能——Spring AOP。AOP是一种面
- Android中获取资源 id 及资源 id 的动态获取我们平时获取资源是通过 findViewById 方法进行的,比如我们常
- 一,Maven 依赖 pom.xml配置1, 去掉默认日志,以便切换到log4j2的日志依赖2, 然后添加如下两个日志依赖二,在工程根目录下
- 如下所示:String beginDate="1328007600000";SimpleDateFormat sdf=n
- 本文实例为大家分享了Android实现百度地图两点画弧线的具体代码,供大家参考,具体内容如下import android.support.a
- 高斯模糊高斯模糊(英语:Gaussian Blur),也叫高斯平滑,是在Adobe Photoshop、GIMP以及Paint.NET等图像
- 异常处理增强错误恢复能力是提高代码健壮性的最有力的途径之一,C语言中采用的错误处理方法被认为是紧耦合的,函数的使用者必须在非常靠近函数调用的
- 最近有个需求 要求列表 滑动后第一条 需要和顶部对齐上网找了找 发现 官方支持 Recycle + LinearSna
- 大家真正在工作中开发 java 应用都会使用eclipse,myeclipse, IntelliJ等等不过依然值得花10分钟学习如何使用最原
- 前言前面我们已经分析Dubbo SPI相关的源码,看过的小伙伴相信已经知晓整个加载过程,我们也留下两个问题,今天我们先来处理下其中关于注解A
- 一、在drawable下面添加xml文件rounded_editview.xml<?xml version="1.0&quo
- List 是在开发中比较常用的集合,今天总结一下 Java 中初始化 List 的几种方式。1、常规方式List<String>
- 废话不多说了,直接给大家贴关键代码了。具体代码如下所示:using System;using System.Collections.Gene
- 一、 PorterDuffXfermodePorterDuffXfermode类似于数据集合里面的交集并集概念,只是数据里面取的是两个集合,
- 面试题1:说一下抽象类和接口有哪些区别?正经回答:抽象类和接口的主要区别:从设计层面来说,抽象类是对类的抽象,是一种模板设计;接口是行为的抽
- 本文实例为大家分享了Android自定义圆环倒计时控件的具体代码,供大家参考,具体内容如下先来一张最终效果图:主要思路: 在画渐变
- 过早提升(Premature Promotion)提升速率(promotion rate), 用于衡量单位时间内从年轻代提升到老年代的数据量
- c语言关闭socket的两种方式一、shutdown()#include<sys/socket.h>int shutdown(i
- 我们在使用ListView的时候,一般都会为ListView添加一个响应事件android.widget.AdapterView.OnIte
- 大部分Java开发者都在使用Map,特别是HashMap。HashMap是一种简单但强大的方式去存储和获取数据。但有多少开发者知道HashM