今天尝试了下最简单的文字格斗类型游戏制作
package com; import java.util.Random; import java.util.*; public class Bean { private String name; private int blood; public Bean() { } public Bean(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(Bean role) { //造成伤害1~20 Random r=new Random(); int hurt=r.nextInt(20)+1; //减去多少血量 int syblood=role.getBlood()-hurt; role.blood=syblood<0?0:syblood; System.out.println(this.name+"打了"+role.name+hurt+"滴血"+role.name+"剩余"+syblood+"点血量"); } }Bean.java
//游戏进攻 package com; public class GameTest { public static void main(String[] args) { //创建第一个角色 Bean r1= new Bean("sb",100); //创建第二个角色 Bean r2= new Bean("2b",100); //开始格斗 回合制游戏 while(true) { //r1开始攻击r2 r1.attack(r2); //判断r2的剩余血量 if(r2.getBlood()==0) { System.out.println(r1.getName()+"KO了"+r2.getName()); break; } //r2开始进攻 r2.attack(r1); if(r1.getBlood()==0) { System.out.println(r2.getName()+"ko了"+r1.getName()); break; } } } }GameTest.java
标签:总结,r1,r2,Bean,blood,今日,public,name From: https://www.cnblogs.com/zhaoyueheng/p/17148850.html