Java實現(xiàn)角色扮演游戲的示例代碼
前言
《模式策略的角色扮演游戲》游戲是自制的角色扮演游戲。選擇兩個角色,然后進行PK,可用來學習JAVA的接口,繼承和多態(tài)。
主要設計
1.事先設計好英雄,血量和相關技能。
2.為了讓玩家能與程序互動,使用下面這個命令可達效果
Scanner sc = new Scanner(System.in);
3.運行StartMain里的main方法
4.設計四個角色
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) {
/**
* 當前玩家若被使用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);
}
}
/**
* 一位玩家進攻或使用魔法結(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)
/**
* 當?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)
/**
* 當?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)
/**
* 當?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)設置角色武器
*/
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ù)用戶鍵盤輸入設置魔法以及作用對象
*/
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)設置角色武器*/
public void setMagicBehavior(MagicBehavior m)
{
this.magic=m;
}/**動態(tài)設計角色魔法*/
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的相關知識有了進一步的了解,對java這門語言也有了比以前更深刻的認識。
java的一些基本語法,比如數(shù)據(jù)類型、運算符、程序流程控制和數(shù)組等,理解更加透徹。java最核心的核心就是面向?qū)ο笏枷?,對于這一個概念,終于悟到了一些。
以上就是Java實現(xiàn)角色扮演游戲的示例代碼的詳細內(nèi)容,更多關于Java角色扮演游戲的資料請關注腳本之家其它相關文章!
相關文章
Java數(shù)據(jù)結(jié)構(gòu)之對象比較詳解
這篇文章主要為大家詳細介紹了Java中對象的比較、集合框架中PriorityQueue的比較方式以及PriorityQueue的模擬實現(xiàn),感興趣的可以了解一下2022-07-07
Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
利用Spring?boot+LogBack+MDC實現(xiàn)鏈路追蹤
這篇文章主要介紹了利用Spring?boot+LogBack+MDC實現(xiàn)鏈路追蹤,MDC?可以看成是一個與當前線程綁定的哈希表,可以往其中添加鍵值對,下文詳細介紹需要的小伙伴可以參考一下2022-04-04
Spring整合SpringMVC與Mybatis(SSM)實現(xiàn)完整登錄功能流程詳解
開學學校開始講servlet 后期要求做一個登錄功能,這個使用SSM先只做個簡單的只帶登錄功能的,且項目使用了MyBatis-Plus來簡化開發(fā)流程??辞闆r決定要不要升級功能或者換個寫法2022-09-09
MybatisPlus?構(gòu)造器wrapper的使用與原理解析
本次我們介紹了MybatisPlus?構(gòu)造器wrapper的使用方式及其易錯點,同時也針對其運行的原理進行了解釋,只有深刻理解了它的原理,我們才能更靈活的使用,并且更快的排查出問題,感興趣的朋友跟隨小編一起看看吧2024-05-05
java.lang.ArrayIndexOutOfBoundsException數(shù)組越界異常問題解決
這篇文章主要給大家介紹了關于java.lang.ArrayIndexOutOfBoundsException數(shù)組越界異常問題解決的相關資料,數(shù)組越界訪問是一個非常嚴重的問題,文中通過圖文將解決的辦法介紹的非常詳細,需要的朋友可以參考下2024-01-01

