+ 5
Text-Based RPG in Java ?
heyyo fellow solo learners ! been banging my keyboard to get a simple text based RPG coded in Java ... but I've got one problem ... how to link a Enemy health field to a Player attack method so that it affects the Enemy ? also , some resources for learning more about classes , objects and inheritance would be nice 😋
38 Answers
+ 31
i'm not on discord @ph4n7om
+ 5
@ph4
You should be able to fill in the blanks, if you understand getters and setters.
+ 5
Also in your code you declared health outside of the Player and enemy class.
In the main.
You should remove them.
You only need the private variables for the classes.
+ 4
here is a sketch of what I would use
(edited)
int attack(int damage){
int currentHealth;
currentHealth = target.getHealth() - damage;
target.setHealth(currentHealth);
}
would need editing to run properly.
+ 4
lol thanks!
- Changed the comment.😸
+ 4
@Manual still a lot to learn 😹
+ 4
@Manual yes , I understand getters and setters somewhat ... ty for the help 😄
+ 4
You're welcome!
+ 4
Put the code in the playground and link the code here.
+ 4
@Manual @luka here's my try ... can you shorten it ?
https://code.sololearn.com/c0775doC5n2e/?ref=app
+ 4
@ph4n
The player health and enemy health seem swaped.
the enemy health functions are in the player class.
easy fix
+ 3
@luka the code is in a really bad shape ... there's nothing in it but my really crazy tries to get these two to link 😵
+ 3
Java Resources
https://www.sololearn.com/discuss/580291/?ref=app
+ 3
@Manual ty for the thread 😊
+ 3
Try posting the code, so it will be easier for you to get help.
+ 3
@Manual @luka my current code is in a bad shape ... it was a prototype 😅 I'll begin coding it again and upload it in a while ... sorry for the trouble 😊
+ 3
public class Enemy {
private int health;
public int getHealth() {
return this.health;
}
public int setHealth(int localHealth) {
this.health = localHealth;
}
public int healthDown(int decrement) {
this.health -= decrement;
}
public int displayHealth() {
System.out.println(this.health);
}
}
public class Player {
int PlayerHealth;
int PlayerAttack;
public int attack(int damage) {
Enemy.healthDown(damage);
}
}
public class Main
{
public static void main(String[] args) {
Player player = new Player();
Enemy enemy = new Enemy();
enemy.setHealth(100);
enemy.getHealth();
player.attack(10);
enemy.displayHealth();
}
}
^ my try 😋
+ 3
@luka yeah , I noticed it ... it remained from when I was experimenting with my code 😂
+ 3
It will take time,
I will link it when I am able to finish.
+ 3
@Ph4
Made one from scratch as a refresher,
should help you with your code.
https://code.sololearn.com/cvxvcntNlGW8/?ref=app