Java實(shí)現(xiàn)角色扮演游戲的示例代碼
前言
《模式策略的角色扮演游戲》游戲是自制的角色扮演游戲。選擇兩個(gè)角色,然后進(jìn)行PK,可用來(lái)學(xué)習(xí)JAVA的接口,繼承和多態(tài)。
主要設(shè)計(jì)
1.事先設(shè)計(jì)好英雄,血量和相關(guān)技能。
2.為了讓玩家能與程序互動(dòng),使用下面這個(gè)命令可達(dá)效果
Scanner sc = new Scanner(System.in);
3.運(yùn)行StartMain里的main方法
4.設(shè)計(jì)四個(gè)角色
1.Queen 2.King 3.Knight 4.Troll
5.角色可選擇使用魔法攻擊或者武器攻擊
6.血量為0,則結(jié)束戰(zhàn)斗
7.抽象出游戲角色類,然后不同的角色再繼承去實(shí)現(xiàn)自己的個(gè)性化。
8.魔法行為可用接口定義,不同的魔法效果,再去實(shí)現(xiàn)這個(gè)魔法行為接口。
9.開(kāi)發(fā)環(huán)境使用JDK8+IDEA
功能截圖
游戲開(kāi)始:
選擇角色
操作技能
代碼實(shí)現(xiàn)
游戲啟動(dòng)類
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(); /** * 第一個(gè)角色的創(chuàng)建 */ if(i==1) { Scanner sc1=new Scanner(System.in); String str=null; System.out.print("請(qǐng)輸入角色名字:"); 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 * 第二個(gè)角色的創(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("請(qǐng)輸入角色名字:"); str1=sc3.nextLine(); Characters character2= new Queen(str1); while(character1.hitpoint>0&&character2.hitpoint>0) { /** * 當(dāng)前玩家若被使用frozen魔法,失去一次進(jìn)攻或使用魔法的機(jī)會(huì) */ 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"); /** * 用戶鍵入選擇使用魔法還是進(jìn)攻 */ Scanner sc4=new Scanner(System.in); int h=sc4.nextInt(); /** * 判斷魔法值是否大于80,若魔法值小于80則無(wú)法進(jìn)攻,必須使用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) { /** * 若對(duì)方上回合使用invisible魔法,則本回合攻擊無(wú)效 */ 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); } } /** * 一位玩家進(jìn)攻或使用魔法結(jié)束后根據(jù)對(duì)方hitpoint是否小于等于零來(lái)判斷游戲是否結(jié)束 */ if(character1.hitpoint<=0) { System.out.println("the"+character1.name+"is dead, the"+character2.name+"win"); System.exit(0); } /** * 輪到第二位玩家開(kāi)始操作 * 以下情況與第一位玩家操作階段一致 */ 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號(hào)角色時(shí) * 以下操作同上 */ { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請(qǐng)輸入角色名字:"); 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號(hào)角色時(shí) * 以下操作同上 */ { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請(qǐng)輸入角色名字:"); 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號(hào)角色時(shí) * 以下操作同上 */ { Scanner sc3=new Scanner(System.in); String str1=null; System.out.print("請(qǐng)輸入角色名字:"); 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) /** * 一號(hào)玩家創(chuàng)建角色的第二種選擇,以下操作與第一種選擇相似 */ { Scanner sc1=new Scanner(System.in); String str=null; System.out.print("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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) /** * 一號(hào)玩家創(chuàng)建角色的第三種選擇,以下操作與第一種選擇相似 */ { Scanner sc1=new Scanner(System.in); String str=null; System.out.print("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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) /** * 一號(hào)玩家創(chuàng)建角色的第四種選擇,以下操作與第一種選擇相似 */ { Scanner sc1=new Scanner(System.in); String str=null; System.out.print("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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("請(qǐng)輸入角色名字:"); 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ù)用戶鍵盤(pán)輸入動(dòng)態(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ù)用戶鍵盤(pán)輸入設(shè)置魔法以及作用對(duì)象 */ 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; }/**動(dòng)態(tài)設(shè)置角色武器*/ public void setMagicBehavior(MagicBehavior m) { this.magic=m; }/**動(dòng)態(tài)設(shè)計(jì)角色魔法*/ public String getName() { return this.name; } public void display() { System.out.println("It's a"+this.name); } }
魔法行為接口
public interface MagicBehavior { void useMagic(); }
總結(jié)
通過(guò)此次的《模式策略的角色扮演游戲》實(shí)現(xiàn),讓我對(duì)JAVA的相關(guān)知識(shí)有了進(jìn)一步的了解,對(duì)java這門(mén)語(yǔ)言也有了比以前更深刻的認(rèn)識(shí)。
java的一些基本語(yǔ)法,比如數(shù)據(jù)類型、運(yùn)算符、程序流程控制和數(shù)組等,理解更加透徹。java最核心的核心就是面向?qū)ο笏枷?,?duì)于這一個(gè)概念,終于悟到了一些。
以上就是Java實(shí)現(xiàn)角色扮演游戲的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java角色扮演游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解springboot集成websocket的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了springboot集成websocket的兩種實(shí)現(xiàn)方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Spring Cloud Feign內(nèi)部實(shí)現(xiàn)代碼細(xì)節(jié)
Feign 的英文表意為“假裝,偽裝,變形”, 是一個(gè)http請(qǐng)求調(diào)用的輕量級(jí)框架,可以以Java接口注解的方式調(diào)用Http請(qǐng)求,而不用像Java中通過(guò)封裝HTTP請(qǐng)求報(bào)文的方式直接調(diào)用。接下來(lái)通過(guò)本文給大家分享Spring Cloud Feign內(nèi)部實(shí)現(xiàn)代碼細(xì)節(jié),感興趣的朋友一起看看吧2021-05-05簡(jiǎn)單了解JAVA內(nèi)存區(qū)域效果知識(shí)
這篇文章主要介紹了簡(jiǎn)單了解JAVA內(nèi)存區(qū)域效果知識(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10jpa?EntityManager?復(fù)雜查詢實(shí)例
這篇文章主要介紹了jpa?EntityManager?復(fù)雜查詢實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Maven中Could not find artifact XXXX的錯(cuò)誤解決
本文主要介紹了Maven中Could not find artifact XXXX的錯(cuò)誤解決,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03IDEA卡在”正在解析Maven依賴項(xiàng)“的解決方法
在創(chuàng)建新的SpringBoot項(xiàng)目時(shí),始終卡在"正在解析Maven依賴項(xiàng)…",本文小編給大家介紹了幾種相關(guān)的解決方案,具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11springboot如何通過(guò)URL方式訪問(wèn)外部資源
這篇文章主要介紹了springboot如何通過(guò)URL方式訪問(wèn)外部資源,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Eclipse中查看android工程代碼出現(xiàn)"android.jar has no source attachment
這篇文章主要介紹了Eclipse中查看android工程代碼出現(xiàn)"android.jar has no source attachment"的解決方案,需要的朋友可以參考下2016-05-05