+ 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 😋

23rd Oct 2017, 4:33 AM
ph4n70m
ph4n70m - avatar
38 Answers
+ 31
i'm not on discord @ph4n7om
24th Oct 2017, 6:54 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 5
@ph4 You should be able to fill in the blanks, if you understand getters and setters.
23rd Oct 2017, 5:04 AM
Manual
Manual - avatar
+ 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.
25th Oct 2017, 10:50 AM
Manual
Manual - avatar
+ 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.
23rd Oct 2017, 4:50 AM
Manual
Manual - avatar
+ 4
lol thanks! - Changed the comment.😸
23rd Oct 2017, 4:58 AM
Manual
Manual - avatar
+ 4
@Manual still a lot to learn 😹
23rd Oct 2017, 5:13 AM
ph4n70m
ph4n70m - avatar
+ 4
@Manual yes , I understand getters and setters somewhat ... ty for the help 😄
23rd Oct 2017, 5:23 AM
ph4n70m
ph4n70m - avatar
+ 4
You're welcome!
23rd Oct 2017, 10:06 AM
Manual
Manual - avatar
+ 4
Put the code in the playground and link the code here.
23rd Oct 2017, 11:15 AM
Manual
Manual - avatar
+ 4
@Manual @luka here's my try ... can you shorten it ? https://code.sololearn.com/c0775doC5n2e/?ref=app
24th Oct 2017, 10:59 AM
ph4n70m
ph4n70m - avatar
+ 4
@ph4n The player health and enemy health seem swaped. the enemy health functions are in the player class. easy fix
24th Oct 2017, 8:51 PM
Manual
Manual - avatar
+ 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 😵
23rd Oct 2017, 4:37 AM
ph4n70m
ph4n70m - avatar
23rd Oct 2017, 4:40 AM
Manual
Manual - avatar
+ 3
@Manual ty for the thread 😊
23rd Oct 2017, 4:41 AM
ph4n70m
ph4n70m - avatar
+ 3
Try posting the code, so it will be easier for you to get help.
23rd Oct 2017, 4:44 AM
Manual
Manual - avatar
+ 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 😊
23rd Oct 2017, 4:47 AM
ph4n70m
ph4n70m - avatar
+ 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 😋
23rd Oct 2017, 8:36 AM
ph4n70m
ph4n70m - avatar
+ 3
@luka yeah , I noticed it ... it remained from when I was experimenting with my code 😂
24th Oct 2017, 7:36 PM
ph4n70m
ph4n70m - avatar
+ 3
It will take time, I will link it when I am able to finish.
24th Oct 2017, 9:04 PM
Manual
Manual - avatar
+ 3
@Ph4 Made one from scratch as a refresher, should help you with your code. https://code.sololearn.com/cvxvcntNlGW8/?ref=app
25th Oct 2017, 2:43 AM
Manual
Manual - avatar