Simple text-based RPG game JS

Simple text-based RPG game JS
0

Good day!

Firstly, I kindly thank you for spending your time on reading this.

I am complete beginner in HTML/CSS/JS, I only started it 2 months ago. Currently I’m on JS ES6 section of FCC curriculum.

I want to make a very simple text-based RPG game using JavaScript (allowing multiple players on one PC, maybe in future extend it to run it on local server).

Main goal will be to defeat monsters and gradually level up to gain better statistics and gain gold for better equipment in order to defeat final boss.

I just started working on my design.

  • First goal is to implement multiple player creation.

Problems I encountered:

  • I tried to base everything on typing in console to access object and function, but after 3 days of bashing my head against the keyboard I understood that it is not the best idea.
    (Today I changed design to use DOM and input from HTML side and it seems to be working ok, but I’m still not sure if this is the correct way to do it)

  • I wasn’t able to globally declare const p1 and const p2 (which I want to be a “save” for a user’s hero) and assign to them value returned from functions cp1 and cp2 (create player). Those are executed only if user will press Confirm button which is sending input from text fields. I couldn’t declare it outside the function as there is no input from user when the website is loaded. Maybe async/await would be good option?
    Finally, I declared it as a global variable inside the function like this:

p1 = new proff(name);

but that means p1 can be reassigned at any point and that’s exactly what I want to avoid.

Please, if you can:

  • Tell me if there is a point to continue with working on this project right now, as I feel that I lack knowledge and skills

  • Point me to the right resources which can help me

  • Show me what mistakes I made in design and execution of the code (how could I improve functionality and readability)

Once again thank you for your time.

If any explanation is needed, I’m at your disposal.
Please be ruthless, I want to know the truth :slight_smile:

My code on Codepen.io

2 Likes

Function I created for dodge mechanic and different output depending on dodge and hp value.
Excessive console.logs are for sake of testing if its working properly

// Attack and Dodge mechanic


function attacking (att, def){
    console.log(att.proff + " attack is " + att.attack);
    console.log(def.proff + " has " + def.hp + " hp");
    let dodging = Math.floor(Math.random() * 100 + def.dodge);
    console.log(dodging);
    if (dodging >= 100){
        console.log(def.proff + " has " + def.hp + " hp");
        return def.proff + " dodged attack of " + att.proff;
    } else {
        def.hp = def.hp - att.attack;
        if (def.hp < 0){
            console.log(def.proff + " got hit by " + att.proff + " and lost " + att.attack + " hp");
            return def.proff + " has 0 hp and is dead";
        }
        console.log(def.proff + " has " + def.hp + " hp");
        return def.proff + " got hit by " + att.proff + " and lost " + att.attack + " hp \nNow he has " + def.hp + " hp";
    }
}
1 Like

Doesn’t sound simple to me. Maybe consider scaling it back to a much simpler game, than gradually adding features and complexity? Good luck.

3 Likes

If I want to prototype something quickly in Javascript, my approach will be similar to this.

  1. Prototype in Javascript and only in the browser console. I’ll ignore dealing with
    CSS or HTML and others for that phase. More often that not, they will distract you from implementing the logics. You can always use console, alert, and, and prompt for debugging and simulating player input.
1 Like

Thank you for your response.

Yes, I am currently working on making it simpler.

It’s currently allowing only one player not multiple.

Seems like it is the case for me.

I got distracted, by dynamically creating DOM elements by using JS.

They are such a headache.

The thing is I don’t quite know how to simulate user input… without using HTML and CSS.

You can certainly just use prompt() to get player data when the game starts.

// all your other code goes here

const name = prompt('What is your name');

// do something with name datum

...