Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  New to Java Programming - Text Based RPG  (Read 414 times)
0 Members and 1 Guest are viewing this topic.
Offline civarc

Junior Newbie





« Posted 2013-10-06 21:07:40 »

I just started taking a Java Programming Class at my school, and am definitely not the strongest programming. I'm trying to lay out the ground work for a VERY basic, and linear, text based rpg. I got stuck when I got to the battle sequences.. I can't get the code to produce what I want it too, and am looking for some advice/tips on this stuff. When I type "1" as my answer to attack, nothing happens but the program keeps running. I want to damage the goblin. Thanks!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
package videogameenemy;
import java.util.Scanner;
public class VideoGameEnemy {
   
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      int answer = 0;
     
      boolean enemyAlive;
      boolean playerAlive;
     
      Enemy Goblin = new Enemy();
      Goblin.setName("Goblin");
      Goblin.setHp(100);
      Enemy Wolf = new Enemy();
      Wolf.setName("Wolf");
      Wolf.setHp(80);
     
      Player user = new Player();
      user.setDamage(15);
      user.setHp(150);
     
     
      Enemy[] enemies = new Enemy[2];
      enemies[1] = new Enemy();
      enemies[1] = Goblin;
      enemies[1].getHp();
      enemies[0] = new Enemy();
      enemies[0] = Wolf;
      enemies[0].getHp();
   
     
     
     
     
     
      System.out.println("A " + enemies[1].getName() + " has appeared!");
     
      System.out.println(enemies[1].getName() +  " has " + enemies[1].getHp() + " HP remaining.");
      System.out.println("Will you attack the " + enemies[1].getName() + "?");
      System.out.println("Enter 1 to attack, or 2 to flee!");
      System.out.println("\t[1 - Attack]");
      System.out.println("\t[2 - Flee]");
     
      answer = input.nextInt();
     
      if(answer == 1)
      {
         
      while(enemies[1].getHp() > 0)
      {
         enemyAlive = true;
         playerAlive = true;
         int d;
         d = enemies[1].getHp() - user.getDamage();
         
         if (enemyAlive = false || playerAlive == false)
         {
            break;
         }
      }
      }
      else if (answer == 2)
      {
         System.out.println("You run and fall.");
     
      }
     
   }
}






/////////
class Enemy
{
   private String name;
   private int Hp;
   
   public Enemy()
   // A Enemy is created with name set to an empty string
  {
     
   }
   
   public Enemy(String name, int Hp)
   {
      name = "";
      Hp = 0;
   }
   
   
   
   public void setName(String newName)
   // Pre:  Assigned(name)
  // Post: class member name has been set to name
  {
      name = newName;
   }
   
   
   public String getName()
   // Post: Returns the name
  {
      return name;
   }
   
   public void setHp(int newHp)
   // Pre:  Assigned(hp amount)
  // Post: class member hp has been set to newHp
  {
      Hp = newHp;
   }
   
   public int getHp()
   {
      return Hp;
   }
   

   
   
}

class Player extends Enemy
{
   private int damage;
   private int hp;
   
   public Player()
   // Default constructor
  {
      damage = 0;
      hp = 0;
     
   }
   
   public void setDamage(int newDamage)
   {
      damage = newDamage;
   }
   
   public int getDamage()
   {
      return damage;
   }
   
   public void setHp(int newHp)
   {
      hp = newHp;  
   }
   
   
   public int getHp()
   {
      return hp;
   }
   
}
Offline Troncoso

JGO Coder


Medals: 20



« Reply #1 - Posted 2013-10-06 21:21:17 »

You never update the enemy Hp. You have this:

1  
2  
int d;
d = enemies[1].getHp() - user.getDamage();


Which does calculate the new Hp, but you never assign it to the enemy. You'll need something like

1  
enemies[1].setHp(d);
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-10-06 21:23:18 »

Whats up with this on line 28 (i think)
1  
enemies[1].getHp();

Is this a get method that does not actually return a value? I would assume that it is being used wrong here.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline civarc

Junior Newbie





« Reply #3 - Posted 2013-10-06 21:27:40 »

You never update the enemy Hp. You have this:

1  
2  
int d;
d = enemies[1].getHp() - user.getDamage();


Which does calculate the new Hp, but you never assign it to the enemy. You'll need something like

1  
enemies[1].setHp(d);


That makes sense. So I would do that in the while loop, correct? Like this:

1  
2  
3  
4  
5  
6  
7  
while(enemies[1].getHp() > 0)
      {
         enemyAlive = true;
         playerAlive = true;
         int d;
         d = enemies[1].getHp() - user.getDamage();
         enemies[1].setHp(d);



Will enemies[1].getHp() in the loop declaration get the new Hp value then (which is d now) ? And also I think something may be wrong with my if statement because when I type 1, nothing happens and the program now stops.

@wreed12345

Now that I look at it, I don't think it was necessary at all, but I also don't think it harms anything?
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-10-06 21:28:34 »

Just doing
1  
enemies[1].getHp();
doesn't do anything. It returns the hp, so you have to do something with that value, like
1  
hp = enemies[1].getHp();


Also, don't use a while, use an if - and instead of setting enemyAlive and playerAlive to true over and over again, just detect if the enemies[1].getHp() is <0 and then set them to false.
Offline Troncoso

JGO Coder


Medals: 20



« Reply #5 - Posted 2013-10-06 21:32:34 »

You never update the enemy Hp. You have this:

1  
2  
int d;
d = enemies[1].getHp() - user.getDamage();


Which does calculate the new Hp, but you never assign it to the enemy. You'll need something like

1  
enemies[1].setHp(d);


That makes sense. So I would do that in the while loop, correct? Like this:

1  
2  
3  
4  
5  
6  
7  
while(enemies[1].getHp() > 0)
      {
         enemyAlive = true;
         playerAlive = true;
         int d;
         d = enemies[1].getHp() - user.getDamage();
         enemies[1].setHp(d);



Will enemies[1].getHp() in the loop declaration get the new Hp value then (which is d now) ? And also I think something may be wrong with my if statement because when I type 1, nothing happens and the program now stops.

@wreed12345

Now that I look at it, I don't think it was necessary at all, but I also don't think it harms anything?


Yes. That's what you should do. Your program stops because you put this code in a while loop. So, it's going to keep reducing the enemy health until it is below 0, and then the program is done. If you put a System.out.println() right after the while loop, you will see this.

As Jimmt said, use an if statement instead of a while statement. Even then, though, your program will stop after you do the input.
Offline civarc

Junior Newbie





« Reply #6 - Posted 2013-10-06 21:38:23 »

Just doing
1  
enemies[1].getHp();
doesn't do anything. It returns the hp, so you have to do something with that value, like
1  
hp = enemies[1].getHp();


Also, don't use a while, use an if - and instead of setting enemyAlive and playerAlive to true over and over again, just detect if the enemies[1].getHp() is <0 and then set them to false.

I understand the enemies[1].getHp(); now (I think). Thanks for clearing that up.

EDIT: Just saw your post. Let me try that.

Ok so it worked (kinda). I have this now:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
answer = input.nextInt();
     
      if(answer == 1)
      {
         
         if(enemies[1].getHp() >= 0)
         {
         enemyAlive = true;
         playerAlive = true;
         int d;
         d = enemies[1].getHp() - user.getDamage();
         enemies[1].setHp(d);
         
         System.out.println(enemies[1].getName() +  " has " + enemies[1].getHp() + " HP remaining.");
            if (enemies[1].getHp() <= 0)
            {
               System.out.println("enemy died");
            }
         }
      }
     
      else if (answer == 2)
      {
         System.out.println("You try to run, but fall into a pit of spikes and die!");
     
      }


It does the math and prints out
"The Goblin has 85 HP remaining."

So now I have 2 questions. Now if I prompt the user again if they want to attack again or not and they choose to attack, do I need to put all that code (pretty much same as above) in again for the next attack?

And also, what if I wanted the entire fight to happen right there? Like my character keeps attacking the goblin.
Offline Troncoso

JGO Coder


Medals: 20



« Reply #7 - Posted 2013-10-06 21:57:23 »

Make it a while loop again, and check for input inside the while loop. IF they select 2, just use break;
Offline civarc

Junior Newbie





« Reply #8 - Posted 2013-10-06 22:01:45 »

It worked! Thank you! But I still have some questions.. Say I wanted to choose if I want to attack again (or possiblty use a spell attack, etc).. Would I use If's then, and would I need to continually copy and paste that battle sequence (with some minor changes) or could I make a method that I could call?
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #9 - Posted 2013-10-06 22:03:48 »

The reason i suggested an if statement is because i thought you had a game loop - you really should read up on them.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline civarc

Junior Newbie





« Reply #10 - Posted 2013-10-06 22:04:47 »

The reason i suggested an if statement is because i thought you had a game loop - you really should read up on them.

I will do that right now! Thank you.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (17 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!