Java實現(xiàn)角色扮演游戲的示例代碼
前言
《模式策略的角色扮演游戲》游戲是自制的角色扮演游戲。選擇兩個角色,然后進行PK,可用來學(xué)習(xí)JAVA的接口,繼承和多態(tài)。
主要設(shè)計
1.事先設(shè)計好英雄,血量和相關(guān)技能。
2.為了讓玩家能與程序互動,使用下面這個命令可達效果
Scanner sc = new Scanner(System.in);
3.運行StartMain里的main方法
4.設(shè)計四個角色
1.Queen 2.King 3.Knight 4.Troll
5.角色可選擇使用魔法攻擊或者武器攻擊
6.血量為0,則結(jié)束戰(zhàn)斗
7.抽象出游戲角色類,然后不同的角色再繼承去實現(xiàn)自己的個性化。
8.魔法行為可用接口定義,不同的魔法效果,再去實現(xiàn)這個魔法行為接口。
9.開發(fā)環(huán)境使用JDK8+IDEA
功能截圖
游戲開始:
選擇角色

操作技能

代碼實現(xiàn)
游戲啟動類
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();
/**
* 第一個角色的創(chuàng)建
*/
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
* 第二個角色的創(chuàng)建
*/
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) {
/**
* 當(dāng)前玩家若被使用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魔法恢復(fù)魔法值
*/
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);
}
}
/**
* 一位玩家進攻或使用魔法結(jié)束后根據(jù)對方hitpoint是否小于等于零來判斷游戲是否結(jié)束
*/
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)
/**
* 當(dāng)?shù)诙煌婕业倪x擇為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)
/**
* 當(dāng)?shù)诙煌婕业倪x擇為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)
/**
* 當(dāng)?shù)诙煌婕业倪x擇為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)
/**
* 一號玩家創(chuàng)建角色的第二種選擇,以下操作與第一種選擇相似
*/
{
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)
/**
* 一號玩家創(chuàng)建角色的第三種選擇,以下操作與第一種選擇相似
*/
{
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)
/**
* 一號玩家創(chuàng)建角色的第四種選擇,以下操作與第一種選擇相似
*/
{
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();
/**
* 根據(jù)用戶鍵盤輸入動態(tài)設(shè)置角色武器
*/
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();
/**
* 根據(jù)用戶鍵盤輸入設(shè)置魔法以及作用對象
*/
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;
}/**動態(tài)設(shè)置角色武器*/
public void setMagicBehavior(MagicBehavior m)
{
this.magic=m;
}/**動態(tài)設(shè)計角色魔法*/
public String getName()
{
return this.name;
}
public void display()
{
System.out.println("It's a"+this.name);
}
}魔法行為接口
public interface MagicBehavior {
void useMagic();
}總結(jié)
通過此次的《模式策略的角色扮演游戲》實現(xiàn),讓我對JAVA的相關(guān)知識有了進一步的了解,對java這門語言也有了比以前更深刻的認識。
java的一些基本語法,比如數(shù)據(jù)類型、運算符、程序流程控制和數(shù)組等,理解更加透徹。java最核心的核心就是面向?qū)ο笏枷?,對于這一個概念,終于悟到了一些。
以上就是Java實現(xiàn)角色扮演游戲的示例代碼的詳細內(nèi)容,更多關(guān)于Java角色扮演游戲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解springboot集成websocket的兩種實現(xiàn)方式
這篇文章主要介紹了springboot集成websocket的兩種實現(xiàn)方式,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Spring Cloud Feign內(nèi)部實現(xiàn)代碼細節(jié)
Feign 的英文表意為“假裝,偽裝,變形”, 是一個http請求調(diào)用的輕量級框架,可以以Java接口注解的方式調(diào)用Http請求,而不用像Java中通過封裝HTTP請求報文的方式直接調(diào)用。接下來通過本文給大家分享Spring Cloud Feign內(nèi)部實現(xiàn)代碼細節(jié),感興趣的朋友一起看看吧2021-05-05
Maven中Could not find artifact XXXX的錯誤解決
本文主要介紹了Maven中Could not find artifact XXXX的錯誤解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Eclipse中查看android工程代碼出現(xiàn)"android.jar has no source attachment
這篇文章主要介紹了Eclipse中查看android工程代碼出現(xiàn)"android.jar has no source attachment"的解決方案,需要的朋友可以參考下2016-05-05

