文字格斗游戏
import java.util.Random;
public class Role {
private String name;
private int blood;
public Role() {
}
public Role(String name, int blood) {
this.name = name;
this.blood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
//定义一个方法用于攻击别人
//谁攻击谁?
//Role r1 = new Role();
//Role r2 = new Role();
//r1.攻击(r2);
//方法的调用者.攻击(参数);
public void attack(Role role){
//计算造成的伤害
Random r = new Random();
int hurt = r.nextInt(20)+1;
//剩余血量 (使用就要用get(),有赋值这个操作就用set() 。)
int remainblood = role.getBlood() - hurt;
//对剩余血量做验证,为负数,就修改为0
remainblood = remainblood < 0?0:remainblood;
//修改挨揍人的血量
role.setBlood(remainblood);
//this代表方法的调用者
System.out.println(this.getName() + "举起拳头打了" + role.getName() + "一下" + "造成了" + hurt +
"点伤害" + role.getName() + "还剩下" + remainblood +"点血。");
}
}
public class TextGame {
public static void main(String[] args) {
Role r1 = new Role("乔峰",100);
Role r2 = new Role("鸠摩智",100);
//开始格斗
while(true){
r1.attack(r2);
if(r2.getBlood() == 0){
System.out.println(r1.getName()+"OK了"+r2.getName());
break;
}
r2.attack(r1);
if(r1.getBlood() == 0){
System.out.println(r2.getName()+"OK了"+r1.getName());
break;
}
}
}
}
%S 与 souf
%s的作用是占位
souf分为两部分参数:
- 要输出的内容%s
- 要填充的数据
System.out.printf("你好%s","张三");
ctrl + shift + / 快速
import java.util.Random;
public class FightingGameOptimization {
private String name;
private int blood;
private char gender;
private String face;
String[] boyfaces={"面目狰狞","玉树临风","相貌平平"};
String[] grilfaces={"惨不忍睹","沉鱼落雁","亭亭玉立"};
//攻击描述
String[] attack_desc={"%s使用了[电光一闪]向%s的胸口打去。","%s使用[十万伏特]释放了无数的雷电朝向%s"};
//伤害描述
String[] injured_desc={"%s成功使用了[快躲开],毫发无损。","%s似乎受了很严重的伤害,但他撑住了!!!发动了[站起来]。"};
public FightingGameOptimization() {
}
public FightingGameOptimization(String name, int blood,char gender) {
this.name = name;
this.blood = blood;
this.gender = gender;
//长相是我们输入性别后,随机给的,不需要我们给信息。
setFace(gender);
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getFace() {
return face;
}
public void setFace(char gender) {
Random r =new Random();
if (gender == '男'){
int index=r.nextInt(boyfaces.length);
this.face=boyfaces[index];
} else if (gender == '女') {
int index=r.nextInt(grilfaces.length);
this.face=grilfaces[index];
}else {
System.out.println("此人面目狰狞!");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
//定义一个方法用于攻击别人
//r1.攻击(r2);
//方法的调用者.攻击(参数);
public void attack(FightingGameOptimization role){
Random r = new Random();
int index = r.nextInt(attack_desc.length);
String kongFu = attack_desc[index];
//输出一个攻击效果
System.out.printf(kongFu,this.getName(),role.getName());
System.out.println();
//计算造成的伤害
int hurt = r.nextInt(20)+1;
//剩余血量 (使用就要用get(),有赋值这个操作就用set() 。)
int remainblood = role.getBlood() - hurt;
//对剩余血量做验证,为负数,就修改为0
remainblood = remainblood < 0?0:remainblood;
//修改挨揍人的血量
role.setBlood(remainblood);
//受伤的描述
/*\
如果血量大于90,0索引的描述。
以此类推
*/
if (remainblood > 50){
System.out.printf(injured_desc[0],role.getName());
}else{
System.out.printf(injured_desc[1],role.getName());
}
System.out.println();
}
public void showRoleInfo(){
System.out.println("姓名为:"+getName());
System.out.println("血量为:"+getBlood());
System.out.println("性别为:"+getGender());
System.out.println("长相为:"+getFace());
}
}
public class TextGameTwo {
public static void main(String[] args) {
FightingGameOptimization r1=new FightingGameOptimization("乔峰",100,'男');
FightingGameOptimization r2=new FightingGameOptimization("鸠摩智",100,'男');
r1.showRoleInfo();
r2.showRoleInfo();
while(true){
r1.attack(r2);
if(r2.getBlood() == 0){
System.out.println(r1.getName()+"OK了"+r2.getName());
break;
}
r2.attack(r2);
if(r1.getBlood() == 0){
System.out.println(r2.getName()+"OK了"+r1.getName());
break;
}
}
}
}
标签:文字,String,r2,getName,System,格斗游戏,public,name
From: https://www.cnblogs.com/jjhdbk/p/18332679