SIMPLE FIGHTING GAME WITH JAVA
(PlAYER !1)
public class player1 {
private String name;
private String weapon;
private int health;
public player1(String name, String weapon, int health){
this.name=name;
this.weapon=weapon;
if (health < 0 || health > 100){
this.health = 100;
}else this.health =health;
}
public void damageGun1() {
this.health -=30;
if (this.health <=0){
this.health=0;
}
System.out.println("GOT HIT BY GUN1. HEALTH IS REDUCED BY 30" +
"NEW HEALTH IS "+this.health );
if (this.health ==0){
System.out.println(getName()+" is dead");
}
}
public void damageGun2(){
this.health -=50;
if (this.health <=0){
this.health=0;
}
System.out.println("GOT HIT BY GUN1. HEALTH IS REDUCED BY 50" +
"NEW HEALTH IS "+this.health );
if (this.health ==0) {
System.out.println(getName()+ "is dead");
}
}
public void heal(){
if (this.health <= 0) System.out.println("player is dead .heal is not possible");
else this.health =100;
System.out.println("health is "+this.health);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getweapon(){
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
}
(PLAYER 2)
public class player2 extends player1{
private int health;
private boolean armour;
public player2(String name,String weapon, int health,boolean armour){
super(name, weapon, health);
this.health=health;
this.armour=armour;
}
@Override
public void damageGun1() {
if (armour) {
this.health -= 20;
if (this.health <= 0) this.health = 0;
System.out.println("ARMOUR IS ON: GOT HIT BY GUN1:HEALTH IS REDUCED BY 20: NEW HEALTH IS " + this.health);
}
if (!armour) {
this.health -= 30;
if (this.health <= 0) this.health = 0;
System.out.println("ARMOUR IS OFF: GOT HIT BY GUN1:HEALTH IS REDUCED BY 30: NEW HEALTH IS "
+ this.health);
}
if (this.health == 0) {
System.out.println(getName() + "is dead");
}
}
@Override
public void damageGun2() {
if (armour) {
this.health -= 40;
if (this.health <= 0) this.health = 0;
System.out.println("ARMOUR IS ON: GOT HIT BY GUN1:HEALTH IS REDUCED BY 40: NEW HEALTH IS " + this.health);
}
if (!armour) {
this.health -= 50;
if (this.health <= 0) this.health = 0;
System.out.println("ARMOUR IS OFF: GOT HIT BY GUN1:HEALTH IS REDUCED BY 50: NEW HEALTH IS "
+ this.health);
}
if (this.health == 0) {
System.out.println(getName() + "is dead");
}
}
@Override
public void heal(){
super.heal();
}
}
(MAIN METHOD)
public class main {
public static void main(String[] args) {
System.out.println(" SAMPLE GAME PROJECT OF 2PLAYERS FIGHTING TO EACH OTHER WITH WEAPONS");
player1 player =new player1("hammad","sword",500);
System.out.println(player.getName());
System.out.println(player.getHealth());
System.out.println(player.getweapon());
player.damageGun1();
player.damageGun1();
player.damageGun2();
player.heal();
player2 betterplayer=new player2("subhan","machine gun",80,true);
System.out.println(betterplayer.getName());
System.out.println(betterplayer.getHealth());
System.out.println(betterplayer.getweapon());
// betterplayer.damageGun1();
// betterplayer.damageGun1();
betterplayer.damageGun2();
betterplayer.heal();
}
}
Comments
Post a Comment