Dragon Game
Posted in Java, on January 3, 2013 at 16:07
import java.util.Scanner;

public class Home {
	public static void main(String[] args) {
	
		Scanner scan = new Scanner(System.in);

		String userAction;
		int myHealth = 100;
		int dragonHealth = 100;
		
		PlayerClass player = new PlayerClass();
		DragonClass dragon = new DragonClass();
		
		player.health = 100;
		dragon.health = 100;
		
		System.out.println("You have come across a dragon. What do you want to do? (fight or run)");
		
		while(dragon.health > 0 && player.health > 0){
			
			userAction = scan.nextLine();
			
			if(userAction.equals("run")){
				System.out.println("You have run away from the dragon.");
				break;
			}
			
			dragon.health = dragon.health - dragon.takeDamage((int) (Math.random() * 10));
			if(dragon.health <= 0){
				System.out.println("You have defeated the dragon!");
				break;
			}
			System.out.println("Dragon HP: " + dragon.health);
			player.health = player.health - player.takeDamage((int) (Math.random() * 10));
			if(player.health <= 0){
				System.out.println("You have been defeated!");
				break;
			}
			System.out.println("Your HP: " + player.health);
			System.out.println("What do you want to do? (fight or run)");
		}
	}
}



public class PlayerClass {
	
	int health;

	int takeDamage(int damage){
		health = health - damage;
		return damage;
	}
}



public class DragonClass {
	
	int health;
	
	int takeDamage(int damage){
		health = health - damage;
		return damage;
	}
}

Share this code

| More

Use the link below to share the code:
HTML
BBCode