java格⽃代码_java学习2打架代码编写在同⼀个包中的Player类
package work;
public class Player {
private String name;
private int hp;
private int attack;
public Player(String name,int hp,int attack){//构造函数,初始化值
this.name=name;
this.hp=hp;
this.attack=attack;
}
public void setName(String name) {//获取值
this.name=name;
}
public String getName(){
return name;
}
public void setHp(int hp){
this.hp=hp;
}
public int getHp(){
return hp;
}
public void setAttack(int attack){
this.attack=attack;
}
public int getAttack(){
return attack;
}/**
*
*攻击⾏为、
*player 为攻击对象
***/
public void hit(Player player){
//被打的对象(⾎量=⾎量-本⼈攻击⼒)
player.hp=player.hp-this.attack;
if(player.hp<0){
player.hp=0;
System.out.println(this.name+"攻击了"+player.name+"打出了"+attack+"点伤害."+player.name+"还剩下多沙"+player.hp+"⾎量"); }
}
}
在同⼀个包中另⼀个Game类
package work;
import java.util.Scanner;
/**
* @author 游戏
*
*/
public class Game {
简单的java游戏代码Scanner sc=new Scanner(System.in);
// 从控制台接受数据并且封装成⼀个玩家对象
public Player enter(String player) {//封装玩家信息
System.out.println("输⼊"+player+"姓名");
String ();
System.out.println("输⼊"+player+"⾎量");
int Int();
System.out.println("输⼊"+player+"攻击⼒");
int Int();
Player backPlayer=new Player(name, hp,attack);
return backPlayer;
}
public void start() {
//从封装好的函数中输⼊玩家的信息
Player ("玩家⼀");
Player ("玩家⼆");
boolean go=true;
while(go){
//玩家⼀打了玩家⼆
player1.hit(player2);
Hp()<0){
System.out.Name()+"ko了"+Name()+"游戏结束!fighting"); break;
}
//玩家⼆打了玩家⼀
player2.hit(player1);
Hp()<0){
System.out.Name()+"ko了"+Name()+"游戏结束!fighting"); go=false;
}
}
}
public static void main(String[] args) {
Game game=new Game();
game.start();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。