Java實現角色扮演遊戲的示例代碼
前言
《模式策略的角色扮演遊戲》遊戲是自制的角色扮演遊戲。選擇兩個角色,然後進行PK,可用來學習JAVA的接口,繼承和多態。
主要設計
1.事先設計好英雄,血量和相關技能。
2.為瞭讓玩傢能與程序互動,使用下面這個命令可達效果
Scanner sc = new Scanner(System.in);
3.運行StartMain裡的main方法
4.設計四個角色
1.Queen 2.King 3.Knight 4.Troll
5.角色可選擇使用魔法攻擊或者武器攻擊
6.血量為0,則結束戰鬥
7.抽象出遊戲角色類,然後不同的角色再繼承去實現自己的個性化。
8.魔法行為可用接口定義,不同的魔法效果,再去實現這個魔法行為接口。
9.開發環境使用JDK8+IDEA
功能截圖
遊戲開始:
選擇角色
操作技能
代碼實現
遊戲啟動類
public class StartMain { public static void main(String[] args) { System.out.println("welcome to the game! please create two rolls."); System.out.println("please choose the character of the first roll: 1.Queen 2.King 3.Knight 4.Troll"); Scanner sc=new Scanner(System.in); int i=sc.nextInt(); /** * 第一個角色的創建 */ if(i==1) { Scanner sc1=new Scanner(System.in); String str=null; System.out.print("請輸入角色名字:"); str=sc1.nextLine(); Characters character1=new Queen(str); System.out.println("please choose the character of the second roll: 1.Queen 2.King 3.Knight 4.Troll"); /**hitpoint * 第二個角色的創建 */ Scanner sc2=new Scanner(System.in); int j=sc2.nextInt(); if(j==1) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Queen(str1); while(character1.hitpoint>0&&character2.hitpoint>0) { /** * 當前玩傢若被使用frozen魔法,失去一次進攻或使用魔法的機會 */ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); /** * 用戶鍵入選擇使用魔法還是進攻 */ Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); /** * 判斷魔法值是否大於80,若魔法值小於80則無法進攻,必須使用recover魔法恢復魔法值 */ if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { /** * 若對方上回合使用invisible魔法,則本回合攻擊無效 */ if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; /** * 跳出攻擊 */ } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } /** * 一位玩傢進攻或使用魔法結束後根據對方hitpoint是否小於等於零來判斷遊戲是否結束 */ if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } /** * 輪到第二位玩傢開始操作 * 以下情況與第一位玩傢操作階段一致 */ if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if(j==2) /** * 當第二位玩傢的選擇為2號角色時 * 以下操作同上 */ { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new King(str1); while(character1.hitpoint>0&&character2.hitpoint>0) { if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if(j==3) /** * 當第二位玩傢的選擇為3號角色時 * 以下操作同上 */ { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Knight(str1); while(character1.hitpoint>0&&character2.hitpoint>0) { if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if (j==4) /** * 當第二位玩傢的選擇為4號角色時 * 以下操作同上 */ { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Troll(str1); while(character1.hitpoint>0&&character2.hitpoint>0){ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } } else if(i==2) /** * 一號玩傢創建角色的第二種選擇,以下操作與第一種選擇相似 */ { Scanner sc1=new Scanner(System.in); String str=null; System.out.print("請輸入角色名字:"); str=sc1.nextLine(); Characters character1=new King(str); System.out.println("please choose the character of the second roll: 1.Queen 2.King 3.Knight 4.Troll"); Scanner sc2=new Scanner(System.in); int j=sc2.nextInt(); if(j==1) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Queen(str1); while(character1.hitpoint>0&&character2.hitpoint>0) { if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if(j==2) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new King(str1); while(character1.hitpoint>0&&character2.hitpoint>0) { if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if(j==3) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Knight(str1); while(character1.hitpoint>0&&character2.hitpoint>0) { if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if (j==4) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Troll(str1); while(character1.hitpoint>0&&character2.hitpoint>0){ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } } else if(i==3) /** * 一號玩傢創建角色的第三種選擇,以下操作與第一種選擇相似 */ { Scanner sc1=new Scanner(System.in); String str=null; System.out.print("請輸入角色名字:"); str=sc1.nextLine(); Characters character1=new Knight(str); System.out.println("please choose the character of the second roll: 1.Queen 2.King 3.Knight 4.Troll"); Scanner sc2=new Scanner(System.in); int j=sc2.nextInt(); if(j==1) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Queen(str1); while(character1.hitpoint>0&&character2.hitpoint>0);{ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if(j==2) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new King(str1); while(character1.hitpoint>0&&character2.hitpoint>0); { if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if(j==3) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Knight(str1); while(character1.hitpoint>0&&character2.hitpoint>0){ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if (j==4) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Troll(str1); while(character1.hitpoint>0&&character2.hitpoint>0){ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } } else if(i==4) /** * 一號玩傢創建角色的第四種選擇,以下操作與第一種選擇相似 */ { Scanner sc1=new Scanner(System.in); String str=null; System.out.print("請輸入角色名字:"); str=sc1.nextLine(); Characters character1=new Troll(str); System.out.println("please choose the character of the second roll: 1.Queen 2.King 3.Knight 4.Troll"); Scanner sc2=new Scanner(System.in); int j=sc2.nextInt(); if(j==1) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Queen(str1); while(character1.hitpoint>0&&character2.hitpoint>0);{ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if(j==2) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new King(str1); while(character1.hitpoint>0&&character2.hitpoint>0); { if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if(j==3) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Knight(str1); while(character1.hitpoint>0&&character2.hitpoint>0){ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character2.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } else if (j==4) { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Troll(str1); while(character1.hitpoint>0&&character2.hitpoint>0);{ if(character1.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character1.frozenchoice=0; } else { System.out.println("please choose the first player's operation:1.fight 2.do magic"); Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); if(character1.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); h=2; } ok: if(h==1) { if(character2.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character2.invisiblechoice=0; break ok; } character1.fight(character2); } if(h==2) { character1.performMagic(character2); } } if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } if(character2.frozenchoice==1) { System.out.println("the player has been frozen, skip to next player"); character2.frozenchoice=0; } else { System.out.println("please choose the second player's operation:1.fight 2.do magic"); Scanner sc5=new Scanner(System.in); int g=sc5.nextInt(); if(character2.magicpoint<80) { System.out.println("your magicpoint is too low, please do magic to recover"); g=2; } ok1: if(g==1) { if(character1.invisiblechoice==1) { System.out.println("the opponebt is invisible, skip this fight"); character1.invisiblechoice=0; break ok1; } character2.fight(character1); } if(g==2) { character2.performMagic(character1); } } if(character2.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character1.name+"win"); System.exit(0); } } } } } }
抽象類:遊戲角色類
public abstract class Characters { protected String name; protected WeaponBehavior weapon; protected int hitpoint=100; protected MagicBehavior magic; protected int magicpoint=100; protected int damage; protected int defense; protected int damagetotal; protected int invisiblechoice; protected int frozenchoice; public void fight(Characters C) { System.out.println("fight:"+C.name); System.out.println("please choose your weapon:1.Sword 2.Knife 3.Bow and Arrow 4.Axe"); Scanner sc=new Scanner(System.in); int i=sc.nextInt(); /** * 根據用戶鍵盤輸入動態設置角色武器 */ switch(i) { case 1:{ this.setWeaponBehavior(new SwordBehavior()); this.weapon.useWeapon(); this.magicpoint=this.magicpoint-6; this.damagetotal=this.damage+4; } break; case 2:{ this.setWeaponBehavior(new KnifeBehavior()); this.weapon.useWeapon(); this.magicpoint=this.magicpoint-15; this.damagetotal=this.damage+2; } break; case 3:{ this.setWeaponBehavior(new BowAndArrowBehavior()); this.weapon.useWeapon(); this.magicpoint=this.magicpoint-12; this.damagetotal=this.damage+7; break; } case 4:{ this.setWeaponBehavior(new AxeBehavior()); this.weapon.useWeapon(); this.magicpoint=this.magicpoint-4; this.damagetotal=this.damage+3; } break; } System.out.println(C.name+" : hitpoint-"+this.damagetotal); C.hitpoint=C.hitpoint-this.damagetotal; } public void performMagic(Characters C) { System.out.println("do magic to "+C.name); System.out.println("please choose the magic 1.invisible 2.heal 3.frozen"); Scanner sc=new Scanner(System.in); int i=sc.nextInt(); System.out.println("please choose the magic receiver: 1.yourself 2.opponent"); Scanner sc1=new Scanner(System.in); int a=sc1.nextInt(); /** * 根據用戶鍵盤輸入設置魔法以及作用對象 */ if(a==1) { switch(i) { case 1:{ this.setMagicBehavior(new InvisibleBehavior()); this.invisiblechoice =1; this.magic.useMagic(); } break; case 2:{ this.setMagicBehavior(new HealBehavior()); this.magic.useMagic(); System.out.println("hitpoint +5, magicpoint +10"); this.hitpoint=this.hitpoint+5; this.magicpoint=this.magicpoint+10; } break; case 3:{ this.frozenchoice=1; this.setMagicBehavior(new FrozenBehavior()); this.magic.useMagic(); } break; } } else if(a==2) { switch(i) { case 1:{ this.setMagicBehavior(new InvisibleBehavior()); C.invisiblechoice =1; this.magic.useMagic(); } break; case 2:{ this.setMagicBehavior(new HealBehavior()); this.magic.useMagic(); System.out.println("hitpoint +5, magicpoint +10"); C.hitpoint=this.hitpoint+5; C.magicpoint=this.magicpoint+10; } break; case 3:{ C.frozenchoice=1; this.setMagicBehavior(new FrozenBehavior()); this.magic.useMagic(); } break; } } else { System.out.println("please input correct choice!"); } } public void setWeaponBehavior(WeaponBehavior w) { this.weapon=w; }/**動態設置角色武器*/ public void setMagicBehavior(MagicBehavior m) { this.magic=m; }/**動態設計角色魔法*/ public String getName() { return this.name; } public void display() { System.out.println("It's a"+this.name); } }
魔法行為接口
public interface MagicBehavior { void useMagic(); }
總結
通過此次的《模式策略的角色扮演遊戲》實現,讓我對JAVA的相關知識有瞭進一步的瞭解,對java這門語言也有瞭比以前更深刻的認識。
java的一些基本語法,比如數據類型、運算符、程序流程控制和數組等,理解更加透徹。java最核心的核心就是面向對象思想,對於這一個概念,終於悟到瞭一些。
以上就是Java實現角色扮演遊戲的示例代碼的詳細內容,更多關於Java角色扮演遊戲的資料請關註WalkonNet其它相關文章!