Coding Fun Java Java For Beginners: Text-based Adventure Game Project (10/10)

Java For Beginners: Text-based Adventure Game Project (10/10)





Want more? Explore the library at

Official site

Twitter

Tags: , , , , , , , , , , , , , , , , , , , , , , ,

36 thoughts on “Java For Beginners: Text-based Adventure Game Project (10/10)”

  1. Great explanation thank u 🙂

  2. Mr.dawdles says:

    I know im 5 years late but… How do I get the system to "println" to my html page?

    Thanks

  3. Can I make customizable enemies, so some that have a pre-set amount of health when they are randomly encountered?

  4. JTB Sentinel says:

    Any one have any tips on how to add a restart option if the player dies?

  5. danielant10 says:

    GREAT EXPLANATION, THANKS!

  6. Thanks for the video, but what I don't understand is why you only use one class??? Mi teacher gets mad if I only use one class.

  7. Kommentator says:

    the best video in this topic.

  8. Diego Fuchs says:

    what if i want to know how many enemies i defeated? how would i do it? thanks

  9. Nicholas Han says:

    I copied this code and ran it but it keeps giving me this error for the scanner:"java.util.NoSuchElementException: No line found". Can anyone help me?

  10. Rvec Icane says:

    can I run this using jcreator?

  11. Nouman Khan says:

    so much helpful series, thank you sir.

  12. dude this was super un organized and confusing and I got lost and my code didnt work. Definitely not something for "Beginners" this is very complex.

  13. Ethan Love says:

    Can we substitute some of this code and put it into methods?

  14. I know its late but can I use classes and methods here so I can make the code systematically?

  15. for some reason I cant get the ".lenght" to work

  16. Crypto Fox says:

    Can anyone help me to add a scoring system to this ? Thanks

  17. Stefan says:

    can this also be done in javascript?

  18. jkgaming101 says:

    how can i find java on mac osx? it doesnt go into applications.

  19. Hey! I like the tutorial, it helped me a lot. Although, there are a few problems with this. For example, in the while loop,
    (enemyHealth > 0)
    should not be the only condition, it should instead be
    (enemyHealth > 0 || health > 0)
    so that when you die the loop ends.
    also, i suggest using switch statements as much as possible since it makes the code a lot less complicated!
    I had a lot of fun with this, i added gold and other items, thanks a lot!

  20. iSkillerz says:

    accedentaly programs undertale OOPS!

  21. Nik Haney says:

    Hey developers, there is a cool little underground text-based game floating around the web. It's the kind of game programmers play at work while on their coffee breaks. It's a little known secret only the hardcore strategists know about. To play, go Google "CodeElf Grid". (Also contains a graphic mini-game adventure game called Golden Sword) Enjoy!

  22. How can i save the data in my jtable

  23. thx for an amazing online course ,,, personally i found it really helpful .

  24. Smuge316 says:

    i have a problem with mine even when i drop below 0 health it doesn't exit me out of the game

  25. I thought of a really good idea for a text based game so came on the internet to see if I can find a tutorial for absolute beginners and learn the basics. I tried 3 other YouTubers before landing on this series and every time I was lost straight away. After watching this I'm fairly confident I'll be able to code a very basic game after maybe rewatching and taking a few notes so thank you 🙂 Will definitely recommend this channel to anyone else interesting in learning coding.

  26. Help my main method stopped working for an unknown reason!

  27. Idolismo says:

    An easy and fun tutorial to follow. Thank you (I know I am late to the party but not really though).

  28. I was wondering if there was an easy way to use the "GAME" while statement as instead just being for the dungeon crawling part of the game and then have a second while statement be something like a town that the player can go to and buy/sell things from shops. Then when like the player leaves the dungeon instead of just getting the option to either quit or keep fighting, they also get an option to go to town. I'm like super new to java/programming so I don't how well this would work with this game system. My main worry is whether or not the number of potions/ "gold" that the player has will be saved each time the enter and exit the dungeon since their values are defined in the main method. Any help would be great, thanks!

  29. Hi, so i have this code:

    public boolean playAsBarb()
    {
    System.out.println("You have chosen to play as the Barbarian and your stats are as follows: ");
    System.out.println("t Health: 100");
    System.out.println("t Thirst: 100");
    System.out.println("t Hunger: 100");
    System.out.println("t Intellect: 40");
    System.out.println("t Strength: 80");
    System.out.println("t Magic: 20");
    System.out.println("t Please confirm your selection by pressing: y/n");

    String confirm = scanner.nextLine();

    while(!confirm.equals("y") || !confirm.equals("n"))
    {
    System.out.println("while loop: You have entered an incorrect command, please confirm with y/n");
    confirm = scanner.nextLine();
    }

    if(confirm.equals("y"))
    {
    System.out.println("You are now the Barbarian, good luck adventurer!");
    return true;
    }
    else if(confirm.equals("n"))
    {
    System.out.println("You have decided to play as another adventurer!");
    return false;
    }

    return false;
    }

    As you'll prob be able to see the while loop with validation and the if/else if after it are similar to yours at the end of your code, except of course ive got letters and you had numbers, of course there is other code there as well as you can see but that is relevant to the game im creating, my problem right off the bat is im stuck in my while loop no matter what letter i type when asked for y/n, my expected results are that if user input(confirm) IS NOT a 'y' or a 'n' then then you enter the while loop getting the incorrect command message and prompted for a y/n again, therefore if the user input(confirm) IS a 'y' or an 'n' then the while validation ultimately returns false thereby not executing the code block containing the error message and moving onto the if statement with user input(confirm) now set to a value of 'y' or 'n' and checking the conditional statements of the if/else if in their respective parenthesis, clearly this is not happening though otherwise i wouldn't be writing this, bottom line is im stuck in the while and the while loop validation also doesnt seem to work either as even if i type a 'y' or an 'n' i still end up in the bloody while loop.

    Hope you or someone reading this can help.

    Thanks.

    D.

    Edit:

    Figured it out for some reason the operator in the while validation must be && not || (and not or) for this to work not exactly sure why as in my eyes because were never asking for user to input more than one number then the input should never be a 1 AND a 2, anyway just thought id update this.

  30. Jay Babcock says:

    This is awesome. I was so exited when I was able to see the code turn into something useful and fun.

  31. I hope to get a reply on this, Would i actually be able to correct an Array of some type to define each enemies attack and health ect..?

  32. RyanMcFryin says:

    Here you go, this looks nicer..

    package tutorials;

    import java.util.Random;
    import java.util.Scanner;

    public class HelloWorld {

    private static class Actor {

    protected String name;
    protected int health;
    protected int damage;

    public Actor(final String name, final int health, final int damage) {
    this.name = name;
    this.health = health;
    this.damage = damage;
    }

    public void attack(final Actor actor) {
    final int damage = getDamage();
    if (this instanceof Player) {
    System.out.println("t> You strike the " + actor + " for " + damage + " damage.");
    } else {
    System.out.println("t> The " + this + " hits you for " + damage + ".");
    }
    actor.damage(damage);
    }

    public void damage(final int damage) {
    health -= damage;
    }

    public int getDamage() {
    return rand.nextInt(damage);
    }

    public boolean isAlive() {
    return health > 0;
    }

    public boolean isDying() {
    return !isAlive();
    }

    @Override
    public String toString() {
    return name;
    }

    }

    private static class Enemy extends Actor {

    private static final String[] ENEMIES = { "Skeleton", "Zombie", "Warrior", "Assassin" };
    private static final int MAX_HEALTH = 50;
    private static final int DAMAGE = 25;

    public static Enemy spawnRandomEnemy() {
    return new Enemy(ENEMIES[rand.nextInt(ENEMIES.length)], rand.nextInt(MAX_HEALTH), DAMAGE);
    }

    public Enemy(final String name, final int health, final int damage) {
    super(name, health, damage);
    }

    public void dropPotionFor(final Player player) {
    if (rand.nextInt(100) < HEALTH_POTION_DROP_PERCENTAGE) {
    System.out.println(" # The " + this + " dropped a health potion! #");
    System.out.println(" # You have " + (++player.healthPots) + " health potion(s). #");
    }
    }
    }

    private final static class Player extends Actor {

    int healthPots;

    public Player(final String name, final int health, final int damage) {
    super(name, health, damage);
    healthPots = 3;
    }

    public void consumePotion() {
    if (hasPotions()) {
    health += HEALTH_POTION_HEAL_AMOUNT;
    healthPots–;
    System.out
    .println("t> You drink a health potion, healing yourself for " + HEALTH_POTION_HEAL_AMOUNT + ".");
    System.out.println("t> You now have " + health + "HP.");
    System.out.println("t> You have " + healthPots + " health potions remaining. n");
    } else {
    System.out.println("t> You have no health potions left! Defeat enemies for a chance to get more! n");
    }
    }

    public void defeat(final Enemy enemy) {
    System.out.println("-—————————————–");
    System.out.println(" # " + enemy + " was defeated! # ");
    System.out.println(" # You have " + health + " HP left. #");
    enemy.dropPotionFor(this);
    }

    public boolean hasPotions() {
    return healthPots > 0;
    }
    }

    private static final int HEALTH_POTION_HEAL_AMOUNT = 30;
    private static final int HEALTH_POTION_DROP_PERCENTAGE = 50;
    private static final String ATTACK = "1";
    private static final String USE_POTION = "2";
    private static final String RUN = "3";

    private static final Random rand = new Random();

    public static void main(final String[] args) {
    final Player player = new Player("Player", 125, 50);
    try (final Scanner in = new Scanner(System.in)) {

    System.out.println("Welcome to the Dungeon");

    while (player.isAlive()) {
    System.out.println("-—————————————–");
    final Enemy enemy = Enemy.spawnRandomEnemy();
    System.out.println("t# " + enemy + " appeared! #n");

    while (enemy.isAlive()) {
    System.out.println("tYour HP: " + player.health);
    System.out.println("t" + enemy + "'s HP: " + enemy.health);
    System.out.println("ntWhat would you like to do?");
    System.out.println("t1. Attack");
    System.out.println("t2. Drink Health Potion");
    System.out.println("t3. Run!");
    final String input = in.nextLine();
    if (input.equals(ATTACK)) {
    player.attack(enemy);
    enemy.attack(player);
    if (player.isDying()) {
    System.out.println("t> You have taken too much damage, you are too weak to go on!");
    break;
    }
    } else if (input.equals(USE_POTION)) {
    player.consumePotion();
    } else if (input.equals(RUN)) {
    System.out.println("t> You run away from the " + enemy + "!");
    continue;
    } else {
    System.out.println("t>Invalid command");
    }
    }
    player.defeat(enemy);
    System.out.println("-—————————————–");
    System.out.println("What would you like to do now?");
    System.out.println("1. Continue fighting");
    System.out.println("2. Exit dungeon");
    String input = in.nextLine();
    while (!input.equals("1") && !input.equals("2")) {
    System.out.println("Invalid command!");
    input = in.nextLine();
    }
    if (input.equals("1")) {
    System.out.println("Continue on your adventure!");
    } else if (input.equals("2")) {
    System.out.println("You exited the dungeon successfully");
    }
    }
    }
    if (player.isDying()) {
    System.out.println("You limp out of the dungeon, weak from battle.");
    }
    System.out.println("#######################");
    System.out.println("# THANKS FOR PLAYING! #n");

    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *